Subroutine that searches Active Directory to see if a user account with the name testew already exists.
CheckForUser("testew")Sub CheckForUser(samAccountName)dtStart = TimeValue(Now())strUserName = samAccountNameSet objConnection = CreateObject("ADODB.Connection")objConnection.Open "Provider=ADsDSOObject;"Set objCommand = CreateObject("ADODB.Command")objCommand.ActiveConnection = objConnectionobjCommand.CommandText = _"<LDAP://dc=fabrikam,dc=com>;(&(objectCategory=User)" & _"(samAccountName=" & strUserName & "));samAccountName;subtree"Set objRecordSet = objCommand.ExecuteIf objRecordset.RecordCount = 0 ThenWScript.Echo "sAMAccountName: " & strUserName & " does not exist."ElseWScript.Echo strUserName & " exists."End IfobjConnection.CloseWScript.Echo "Script completed in " & _Second(TimeValue(now()) - dtStart) & _" second or less."End Sub
This is a VB Script, this can be used by saving the file in .vbs file