Solved

DSQUERY to list all users in OU including AD attributes

Posted on 2011-09-15
5
1,694 Views
Last Modified: 2012-11-20
I'm looking to generate a list of users in a given OU (and all sub-OUs) which includes the following attributes:

cn, legacyExchangeDN, mail

I would like to dump this to a file (CSV or text). I'm guessing this could be done using a combination dsquery/dsget command, but I'm having trouble putting together the correct syntax.
0
Comment
Question by:TWCMIL
  • 3
  • 2
5 Comments
 
LVL 10

Accepted Solution

by:
wdurrett earned 500 total points
Comment Utility
Here is a pwershell script I use.  You can change the fields to get the info you need.

Change the $file name and the $SearchRoot to suit your needs.

 
# Basic information to run the script
$File = 'some_output_filecsv'
$SearchRoot = "LDAP://OU=someOU,DC=domain,DC=com"
 
## Filter to find the things we want
$Filter = "(objectClass=User)"
 
# Connect to the search root
$SearchRootDE = New-Object System.DirectoryServices.DirectoryEntry($SearchRoot)
 
# Set up the search
$Searcher = New-Object System.DirectoryServices.DirectorySearcher($SearchRootDE, $Filter)
$intProp = $Searcher.PropertiesToLoad.Add("name")
$intProp = $Searcher.PropertiesToLoad.Add("mail")
$intProp = $Searcher.PropertiesToLoad.Add("physicalDeliveryOfficeName")
$intProp = $Searcher.PropertiesToLoad.Add("title")
 
 
# Get the results
$Results = $Searcher.FindAll()
 
# Sort through the results and save them in an array
$Users = @()
ForEach ($Result in $Results) {
  $Users += $Result.Properties | Select-Object `
        @{n="name";e={$_.name}}, `
        @{n="mail";e={$_.mail}}, `
        @{n="physicalDeliveryOfficeName";e={$_.physicaldeliveryofficename}}, `
	@{n="title";e={$_.title}}
}
 
# Write the array to a file
$Users |  Export-CSV -Path $File

Open in new window

0
 
LVL 1

Author Comment

by:TWCMIL
Comment Utility
I got half of what I was looking for, the cn and mail fields were returned, but the legacyExchangeDN and displayName fields come back blank. Here's how I modified it:

# Basic information to run the script
$File = 'user_extract.csv'
$SearchRoot = "LDAP://OU=Users,DC=domain,DC=com"
 
## Filter to find the things we want
$Filter = "(objectClass=User)"
 
# Connect to the search root
$SearchRootDE = New-Object System.DirectoryServices.DirectoryEntry($SearchRoot)
 
# Set up the search
$Searcher = New-Object System.DirectoryServices.DirectorySearcher($SearchRootDE, $Filter)
$intProp = $Searcher.PropertiesToLoad.Add("cn")
$intProp = $Searcher.PropertiesToLoad.Add("legacyExchangeDN")
$intProp = $Searcher.PropertiesToLoad.Add("mail")
$intProp = $Searcher.PropertiesToLoad.Add("displayName")
 
 
# Get the results
$Results = $Searcher.FindAll()
 
# Sort through the results and save them in an array
$Users = @()
ForEach ($Result in $Results) {
  $Users += $Result.Properties | Select-Object `
        @{n="cn";e={$_.cn}}, `
        @{n="legacyExchangeDN";e={$_.legacyExchangeDN}}, `
        @{n="mail";e={$_.mail}}, `
	@{n="displayName";e={$_.displayName}}
}
 
# Write the array to a file
$Users |  Export-CSV -Path $File

Open in new window


I double-checked the field names (using the Attribute Editor in ADUC) and they look right.  Am I missing something obvious?
0
 
LVL 10

Assisted Solution

by:wdurrett
wdurrett earned 500 total points
Comment Utility
Try just "name" for Display Name.

Try "legacydn" for the excahnge dn.



0
 
LVL 10

Expert Comment

by:wdurrett
Comment Utility
You mauy also want to check out this article:

http://www.sapien.com/forums/scriptinganswers/forum_posts.asp?TID=2061

0
 
LVL 1

Author Closing Comment

by:TWCMIL
Comment Utility
Sorry, forgot I had this question out there -- thanks for the help!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Synchronize a new Active Directory domain with an existing Office 365 tenant
Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

763 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

14 Experts available now in Live!

Get 1:1 Help Now