Skip to main content

Exporting Group Members to CSV from Multiple Groups

By May 13, 2015June 7th, 2022Blog, Hot Technology Topics

In recently working on an email migration for a customer, I was tasked with documenting the group membership of multiple distribution groups so that they could be created in the new target AD environment.

The quickest way to do this is to export all your groups to a file, then use that file to parse the group members and output that information to a final csv.

So, your first step is to export your groups to a file.  Open powershell as an administrator:

Import-Module ActiveDirectory

Get-ADGroup –Filter ‘GroupCategory –eq “Distribution”’ | sort name | select name | export-csv c:tempGroups.csv -NoTypeInformation

This will give you a listing of all of your distribution groups.  From here I had trouble using the csv as an input source, so I just removed all the quotation marks and commas and renamed it to groups.csv

Next I tried a canned power shell from the internet to export the information:

$groups = Get-Content c:tempgroups.txt

foreach($Group in $Groups) {

Get-ADGroupMember -Id $Group | select @{Expression={$Group};Label=”Group Name”},SamAccountName | Export-CSV -append c:tempGroupInfo.CSV -NoTypeInformation

}

However what I found was that it was not appending the information to the csv file – I was only getting information from the last group in the txt file.  So I decided to create an array to hold all the information, then output it to file:

$groups = Get-Content c:tempdlgroups1.txt

$results = @()

$file = “c:tempgroupmem.csv”

foreach($Group in $Groups) {

$results +=Get-ADGroupMember -Id $Group | select @{Expression={$Group};Label=”Group Name”},SamAccountName

}

$results | export-csv -notypeinformation -path $file

This was the information I was looking for.  From here I was able to give this to the client to use in creating the new AD Groups.

Josef Hanning, PEI

Leave a Reply