Author Posts

October 13, 2015 at 2:16 pm

Hello, I'm trying to script a count of all members for each distribution group. This task is for troubleshooting purposes in which our AD azure sync isn't syncing all members of our AD distro groups to Office 365 – members are missing in Exchange online.

So, for now, I like to script a way to give me the count of all members for each distribution group.

This works fine but its only one group at a time:
$GN = read-host "Enter distro group name"

$Count = (Get-DistributionGroupMember -Identity $GN | select name).count
Write-host -ForegroundColor Cyan $GN has $Count members

I tried running this but it failed:
$GN = gc d:\distros.txt

foreach($name in $GN) {

$Count = (Get-DistributionGroupMember -Identity $name | select name).count
Write-host -ForegroundColor Cyan $GN has $Count members
}

I'll appreciate the help in showing me where I'm going wrong with enumerating all distribution groups via a text file of all distribution group names.

I'm looking for an output on console or export to file that gives this information:
Group A has 5 members
Group B has 7 members
Group C has 10 members
etc.....

Thank you!

October 13, 2015 at 2:37 pm

There are a couple of problems with your Write-Host.
1) You are using $GN, which is your list of groups instead of $name which is your current group.
2) You are not using " around your string to write to host

This should do it:

$GN = gc d:\distros.txt
foreach($name in $GN) {
    $Count = (Get-DistributionGroupMember -Identity $name | select name).count
    Write-host -ForegroundColor Cyan "$name has $Count members"
}