Welcome to MSDN Blogs Sign in | Join | Help

Decrypt my World

Cryptography, Security, Debugging and more!
How to get a list of all users in an OU (VBScript)

Hi all, welcome back,

Today I'll post a very straight forward sample which gets a list of all users in an Organizational Unit (OU) in Active Directory (AD) using VBScript:

' 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

 

I hope this helps.

Regards,

 

Alex (Alejandro Campos Magencio)

Posted: Wednesday, April 23, 2008 4:44 PM by alejacma
Filed under: ,

Comments

tonyr said:

just curious why your starting these vbscript samples why not powershell?

# April 23, 2008 1:00 PM

alejacma said:

That's a good question... Many, many people are still using VBScript out there. Additionally, PowerShell is not supported on Windows 2000, for instance, and I have many customers using that OS.

But you are right and I should post something on PowerShell. Right now I'm posting real issues or questions that customers raised to me in the past few weeks, and nobody has ever requested a PowerShell sample to me, for instance. So I have none.

I should start offering them PowerShell as an alternative. This way I may get some samples to post ;-)

Thx a lot for your comment! Appreciated!

Cheers,

Alex

# April 23, 2008 1:49 PM

tonyr said:

its true that its not supported on w2k but you don't have to run it on a w2k device run it on xp\vista\2003 against a remote w2k device.. but I do understand what your saying otherwise!

tr

# April 24, 2008 10:13 AM

alejacma said:

And additionally, some customers use these VBScript samples in their ASP applications. Unfortunately for them, not everybody is using ASP.NET yet...

# April 24, 2008 10:42 AM

PWRick said:

Thank you for posting this script I have found it helpful. I do have a question though, I am using this as an ASP script to retrieve the list of users from our Active Directory. I found a listing of AD fields but when I substitue "givenname" or "dispalyname" for "distinguishedname" I get an error "Item cannot be found in the collection corresponding to the requested name or ordinal." Am I using the wrong field names? Thanks

# June 5, 2008 10:34 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker