Export all users for each Distribution Group I own into a csv

Export all users for each Distribution Group I own into a csv

The below commands will extract all the Distribution groups I own.

Get-DistributionGroup -ManagedBy [email protected] | Export-Csv -Path c:\temp\test.csv -NoTypeInformation

Question: How can I expand this so that it it outputs all the members for each group that I own like the examples listed below.

Group1

  • member 1 - email address
  • member 2 - email address

Group 2

  • member 1 - email address
  • member 2 - email address

and so forth!

I checked online and going bat crazy, and help appreciated

答案1

The PowerShell #1 solution will dump the name of the member and name of the group into one csv file and more accurately matches the example logic you provided.

This solution uses select-object to add a calculated property providing the distribution group name field and value into the csv file. The variable is used to set the group name per the Get-DistributionGroup output, but before the Get-DistributionGroupMember is run and piped.

PowerShell #1

Get-DistributionGroup -ManagedBy [email protected]  | ForEach-Object { 
   $DGName = $_.Name;
   Get-DistributionGroupMember -Identity $_.Name } |  
       Select Name, RecipientType, @{ N='Group Name'; E={$DGName};} |
        Export-Csv -Path "c:\temp\test.csv" -NoTypeInformation;

The PowerShell #2 solution that will dump the name of the member into a csv file with the name of the group as the base name of the csv file specified.

This solution works similar to setting the name of the distribution group as a variable as in the first solution, but it will use that to set the base name of the exported csv file to the value. So you'll get one file per group with this solution.

PowerShell #2

Get-DistributionGroup -ManagedBy [email protected] | ForEach-Object {
    $DGName = $_.Name;
    Get-DistributionGroupMember -Identity $_.Name } | 
        Export-Csv -Path "c:\temp\$DGName.csv" -NoTypeInformation;

Supporting Resources

相关内容