Export Users From Multiple Organization Units
This is another simple PowerShell script we had to create to export users from multiple organization units so we can import them into the active directory in a different vlan in our QualTechCloud environment.
We are in the middle of creating a new crm environment with Dynamics CRM 2015 to host our customers using our shared crm environment. Each of our customers is a OU in the active directory in order to keep things as organized as possible.
We can’t just create each user one at a time so we need to be able to export them to a .csv file so we can use the file with our import script which you can find here.
$searchOU = "OU=First OU,DC=test-ad,DC=com","OU=Second OU,DC=test-ad,DC=com","OU=Third OU,DC=test-ad,DC=com","OU=Fourth OU,DC=test-ad,DC=com"
ForEach ($OU in $searchOU)
$users += Get-ADUser -Filter * -Searchbase $OU |Get-ADObject -Properties *
$users | Export-Csv -Path D:\Data\ADUsers.csv -Delimiter “,”
Explaining the script above and starting with $searchOU this variable contains the list of OU in the active directory we want to export. Each OU definition is enclosed in double quotes and separated by a coma (,).
With the list of OU defined that way we are now able to loop through the list with the ForEach statement. This statement reads one OU from the list into the $OU variable and uses it with the -Searchbase. All the users of each OU are now added to the $users variable.
The -Properties * indicates to the Get-ADUser scriplet to obtain all the active directory properties for each user.
In the last line we pipe the content of the $users variable into the ADUsers.csv file with the Export-Csv scriplet.
Hope this will make someone else’s life a bit easier.
Leave your comments on this