Search code examples
powershellactive-directoryoffice365exchange-serverwindows-scripting

Using Powershell to list Email distribution group name, member count and email address then export to .CSV


I need to get the Exchange Distribution group that has member less than or equal to 1.

The output that I need is as .CSV:

Distributionlistname , membercount , EmailAddress
DLName1, 1, [email protected]
DLName2, 0, [email protected]
DLName3, 0, [email protected]
...

This is the script that I have found but doesn't give me the output like the above:

Get-DistributionGroup –ResultSize Unlimited | Where-Object { (Get-DistributionGroupMember –identity $_.Name –ResultSize Unlimited).Count -lt 1 } | Select-Object Name -ExpandProperty EmailAddresses | Export-Csv C:\Result.csv

Solution

  • Another way (keeps the idea of an one-liner):

    Get-DistributionGroup –ResultSize Unlimited | Where-Object { (Get-DistributionGroupMember –identity $_.Name –ResultSize Unlimited).Count -lt 1 } | Select-Object Name,@{Name="EmailAddress";Expression={(Get-DistributionGroupMember –identity $_.Name –ResultSize Unlimited).Count}},PrimarySmtpAddress | Export-Csv C:\Result.csv
    

    Though, this is problay not super efficient because you run Get-DistributionGroupMember twice.