简体   繁体   中英

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, DL1@domain.com
DLName2, 0, DL2@domain.com
DLName3, 0, DL3@domain.com
...

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

Something like this:

$result = @()
Get-DistributionGroup –ResultSize Unlimited | 
    ForEach-Object {
        $memberCount = @(Get-DistributionGroupMember –Identity $_.DistinguishedName –ResultSize Unlimited).Count
        if ($memberCount -le 1) {
            $result += New-Object -TypeName PSObject -Property @{
                'DistributionlistName' = $_.Name
                'MemberCount'          = $memberCount
                'EmailAddress'         = $_.PrimarySmtpAddress
            }
        }
    }

$result | Export-Csv C:\Result.csv -NoTypeInformation -Force

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM