Solved: Getting a user’s Distribution Group Memberships

It’s surprisingly hard to get back a listing of all of a particular user’s Exchange Distribution Group Memberships. The strange thing about this is that you can very easily get a list of all of a user’s AD Security Groups using

Get-ADPrincipalGroupMembership

. If this works for your purposes, great, but if what you really need is a report of all of a user’s / mailbox or resource mailbox Distribution Group membership, I’ve come up with the following.

get-distributiongroup | ForEach-Object {
$groupName = $_
Get-DistributionGroupMember -Identity $groupname.Name | ForEach-Object{
    [pscustomObject]@{GroupName=$groupname;groupMember=$_.Name}
    }
} | Group-Object -Property GroupMember | 
    Select-object Name, @{Name=‘Groups‘;Expression={$_.Group.GroupName}}

Whoa! What’s happening there?

Here’s the walkthrough of why this works:

  • We’re getting a big list of all of the distribution groups
  • for each, resolving the full membership of each group
  • For every entity we discover who is a member of this group, we create a custom object of “username,groupname”
  • Once this finishes, we send this to the Group-Object command to let it pick out every unique user
  • Then gather all of their memberships using a calculated property
  • We then can send this on to a CSV file, to get an output like this.
#TYPE Selected.Microsoft.PowerShell.Commands.GroupInfo
"Name","Groups"
"Stephen","Group_1 Group_2 OtherFolks"
"Lenna.Paprocki","Group_2 OtherFolks"
"James.Butt","Group_2 OtherFolks"
"Josephine.Darakjy","OtherFolks"

In my opinion, XML would be the best way to display this info, rather than a CSV. Additionally, it would be very cool to have a lighter weight cmdlet to return just the Distribution Group membership of one user. If I come up with this approach, I’ll be sure to update this.

Hope you enjoy!

Advertisements

Have a code issue? Share your code by going to Gist.github.com and pasting your code there, then post the link here!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s