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