Skip to content

Instantly share code, notes, and snippets.

@milsosa
Created November 19, 2013 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milsosa/7549069 to your computer and use it in GitHub Desktop.
Save milsosa/7549069 to your computer and use it in GitHub Desktop.
Get list of Active Directory Users
' Get OU
'
strOU = "OU=Users,DC=domain,DC=com"
' Create connection to AD
'
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
' Create command
'
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
' Execute command to get all users in OU
'
objCommand.CommandText = _
"<LDAP://" & strOU & ">;" & _
"(&(objectclass=user)(objectcategory=person));" & _
"adspath,distinguishedname,sAMAccountName;subtree"
Set objRecordSet = objCommand.Execute
' Show info for each user in OU
'
Do Until objRecordSet.EOF
' Show required info for a user
'
WScript.Echo objRecordSet.Fields("adspath").Value
WScript.Echo objRecordSet.Fields("distinguishedname").Value
WScript.Echo objRecordSet.Fields("sAMAccountName").Value
' Move to the next user
'
objRecordSet.MoveNext
Loop
' Clean up
'
objRecordSet.Close
Set objRecordSet = Nothing
Set objCommand = Nothing
objConnection.Close
Set objConnection = Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment