I’ve been busy the past bit manipulating my QA environment to better match the production environment. One of the things I’ve needed to do was pull a list of Active Directory (AD) groups from certain Organizational Units (OUs) and put them into a CSV file where I can then use it to do things like change the email domain, descriptions, etc.
The command you need is as follows, note you need to run this from a machine with the Active Directory PowerShell module installed on it.
$filename = ".\ExportedGroups.csv"
Get-ADGroup -Filter '*' | select-object * | where-object {$_.distinguishedname -like "*,OU=Container,*"} |Export-Csv -Path $filename
What the above script is doing using Get-ADGroup to grab the list of groups, selecting all the fields in the group, using a where-object to figure out which OU we want to use. The OU in this example is container but can be whatever you want or if you have OUs with the same name, then use more of the OU structure like “*,OU=Container,OU=Unique Parent Container,*”.
Finally we export the results to a file named ExportedGroups.csv which is located in the same file as the script.