Enumerating count for each distribution group not working.

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Curtis Smith Curtis Smith 1 year, 3 months ago.

  • Author
    Posts
  • #30752
    Profile photo of Rocky Cabral
    Rocky Cabral
    Participant

    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!

  • #30753
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    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"
    }
    

You must be logged in to reply to this topic.