Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if a user exists in AD.

Status
Not open for further replies.

JimHolland

Programmer
Feb 5, 2003
6
0
0
GB
I currently use a script to create and delete user account in AD - using a txt file as input. But what I would like to do is check first to see if a user account exists first -and to flag it if it does.

Could anyone point me any code which checks to see if a user account already exists?

Cheers.
 
This one looks if the SAMAccountname "PeterW" is in use in the "Users" OU in the domain "domain".
If it doesnt find it it will tell you that and if it finds it then you'll see the dn path and so on.

//Peter

Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"

Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection

objCommand.CommandText = _
&quot;<GC://ou=Users,dc=domain,dc=com>;&quot; & _
&quot;(&(objectCategory=person)(objectClass=user)&quot; & _
&quot;(sAMAccountName=PeterW));&quot; & _
&quot;sAMAccountName, distinguishedName;subtree&quot;

Set objRecordSet = objCommand.Execute

If objRecordSet.RecordCount = 0 Then
Wscript.Echo &quot;The sAMAccountName is not in use.&quot;
Else
While Not objRecordset.EOF
Wscript.Echo &quot;sAMAccountName = &quot; & _
objRecordset.Fields(&quot;sAMAccountName&quot;)
Wscript.Echo &quot;distinguishedName = &quot; & _
objRecordset.Fields(&quot;distinguishedName&quot;)
objRecordset.MoveNext
Wend
End If

objConnection.Close
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top