Need to view users in an OU within Active Directory.

Posted on 2007-11-29
Last Modified: 2010-04-30
Anyone know a quick and easy way to view all the users in a OU.  I tried using CSVDE but the txt file came out pretty garbled?

I tried finding step by step instructions, but nothing came up.

Question by:yoyoma788
  • 2
  • 2
LVL 17

Expert Comment

Comment Utility
Sorry if this sounds obvious but why dont you use the Active Directory AD mmc snap in?

Author Comment

Comment Utility
No I'm trying to give a list to our secretary.  So hopefully I could export it into a spreadsheet or something like that.

LVL 17

Accepted Solution

JohnGerhardt earned 250 total points
Comment Utility
Hi yoyoma788,

Below is a quick VB script to do what you want. It just echos out all of the users in the OU that you have selected in the varibales section at the top. If you want some help writing out to a file then give me a shout.

Just create a text file and name it ConnectToTheAD.vbs => copy and paste the script in there and edit the varibles. Run it from the command prompt with cscript.

Tell me how it goes....


Const adUseClient = 3

'Variables Here

OU = "YOUR OU HERE" 'Enter your OU here

strDNC = "DC=YOUR DOMAIN,DC=HERE" ' Enter your AD root here

FindWhat = "user" ' Change to either user or computer to find either users or computer in the AD

'End Variables

	Set objConnection = CreateObject("ADODB.Connection")

    Set objCommand = CreateObject("ADODB.Command")

    objConnection.Provider = "ADsDSOObject"

    objConnection.Open "Active Directory Provider"


    Set objCommand.ActiveConnection = objConnection


	Set objStdOut = WScript.StdOut

'=================================================Main Routine Start ===============================================

Call QueryTheAD 'Get List from the AD

'=================================================Main Routine End ===============================================

Sub QueryTheAD

  objCommand.CommandText = "Select Name, Location from 'LDAP://" &  strDNC & "' " & "Where objectClass='"& FindWhat & "'"

  objCommand.Properties("Page Size") = 1000

  objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

  objCommand.Properties("Sort on") = "Name"

  Set objRecordSet = objCommand.Execute

    Do Until objRecordSet.EOF

    	MachineCount = MachineCount + 1 

    	strComputer = objRecordSet.Fields("Name").Value

    		WScript.Echo strComputer



End Sub

Open in new window


Author Comment

Comment Utility
I'll give it a shot.  Thanks for the post.

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now