Export a list of Distribution Group Members for multiple Distribution Groups into a single file.

Scenario:  You are asked to find all distribution group that starts with  ACCM* and then export the distribution group name and its members of each distribution group into a single file.

Solution: The script below finds each member of each distribution group and exports to a csv the groups name, mbx alias, first name, last name, department, and title on each line.

$final = @()

$1 = Get-DistributionGroup ACCM*

$1 | %{ 
write-host $_.DisplayName
$ListName = $_.Name
$ListDisplayName = $_.DisplayName
$2 = Get-distributionGroupMember $listName
 $2 | %{
 $MbxAlias = $_.Alias
 $MBxFirst = $_.FirstName
 $MBxLast = $_.LastName
 $MBxDept = $_.Department
 $MbxTitle = Get-aduser $_.alias -properties Title | Select Title
 $MbxOffice = Get-aduser $_.alias -properties Office | Select Office
 $returnobj = new-object psobject
 $returnobj |Add-Member -MemberType NoteProperty -Name "ListDisplayName" -Value $ListDisplayName
 $returnobj |Add-Member -MemberType NoteProperty -Name "FirstName" -Value $MbxFirst
 $returnobj |Add-Member -MemberType NoteProperty -Name "LastName" -Value $MbxLast
 $returnobj |Add-Member -MemberType NoteProperty -Name "Department" -Value $MbxDept
 $returnobj |Add-Member -MemberType NoteProperty -Name "Title" -Value $MbxTitle
 $returnobj |Add-Member -MemberType NoteProperty -Name "Office" -Value $MbxOffice
 $final += $returnObj
  }
}

$final | Export-csv C:tempAccM.csv
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: