question

AlexanderKim-5350 avatar image
0 Votes"
AlexanderKim-5350 asked ·

Export list of users from multiple OUs

Hi guys,

I've come across this script to export Ad users from multiple OUs.

 'OU=1,DC=domain,DC=com','OU=2,DC=domain,DC=com' | ForEach-Object {
    
     Get-ADUser -Filter * -SearchBase $_ -Properties DisplayName,EmailAddress
    
 } | Select Name,GivenName,Surname,DisplayName,SamAccountName,EmailAddress |
         Export-Csv .\userList.csv -NoTypeInformation

However, because I have around 100 OUs ideally I need insert OUs in some other way (may be from txt file with Get-Content)
Also, the outcome of this script doesn't include the information which OU a user is located in.

Is it possible modify this script they way I need?

windows-server-powershell
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered ·

Hi,

You can add the DistinguishedName attribute of the AD user to the output.

 $ous = 'C:\temp\ous.txt'
 $userlist = 'C:\temp\userlist.csv'
 Get-Content -Path $ous | ForEach-Object {    
      Get-ADUser -Filter * -SearchBase $_ -Properties DisplayName,EmailAddress   
 } | Select Name,GivenName,Surname,DisplayName,SamAccountName,EmailAddress,DistinguishedName |
          Export-Csv -Path $userlist -NoTypeInformation

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered ·

The Get-OrganizationalUnit cmdlet will return a list of all your OUs. You can remove the ones you don't want.

OTOH, if you want ALL the users regardless of the OU, just remove the -SearchBase from the Get-ADUser cmdlet.

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.