PowerShell/Office 365 - Get an Exchange Online user’s distribution groups efficiently

2015/08/29 | 2 minute read |

In my previous post I showed how to retrieve the membership of one or multiple groups. In today’s post I want to find the Distribution groups a user belongs to.

There are multiple approaches you could be using to gather this information but as an example I’ll re-use the same one-liner from my previous article and simply filter with Where-Object to find a particular User.

This is probably the route PowerShell beginners would take and it is working ‘Fine’ in a small environment but not very efficient, you’ll see in the following example.

In this environment, there are a bit more than 1800 Distribution Groups. I want to know which Distribution Group I’m member of.

Retrieving the count of Distribution Groups and an User account in Office 365:

Retrieving the count of Distribution Groups and an User account in Office 365

Requirement

You need to be connected to Exchange Online. This can be done using on of the following functions:

Find User’s distribution groups with the Where-Object cmdlet

This retrieve all the members of each groups, then narrow down the output to only the user I’m looking for. This looks good with the only problem than this one-liner took… 15 minutes to query all the groups memberships, Not really efficient…

Find User’s distribution groups with the Filter parameter

What you can do instead is to filter as much as you can on the left side of your command line. This is one of the best practice in Powershell because the output of your first function is much smaller compare to the 1800 Distribution Groups we get in the previous example.

Here I’m using the Filter parameter from the Get-DistributionGroup to only find groups where my distinguished name appears.

Get-DistributionGroup -Filter "Members -like $($user.distinguishedName)"

Exchange Cmdlet Server-Side Opath Filtering

I don’t know if you notice but there is a trick here in the previous example. In the Filter I specified the Members property…. but there is no Members property when you check the Get-DistributionGroup members.

Get-DistributionGroup -ResultSize 5 | Get-Member -Name "*Member*"

No signs of ‘Members’ property. If you look at the help of Get-DistributionGroup, you see something talking about Opath filtering used to filter recipients.

Get-Help on Get-DistributionGroup - FILTER Parameter

The reason is that you can use Exchange Opath filters inside many Exchange Online cmdlets.

Exchange Opath filters OPATH is basis for the filtering syntax used by PowerShell, and is therefore the filtering syntax used by Exchange 2007. It replaces the complicated syntax of LDAP used in Exchange 2003, and will allow for filters which are easier to create and interpret. For native PowerShell filters, all work is done client-side in the Powershell host. In Exchange 2007, however, various cmdlets provide “server-side” filters using the same syntax as their client-side counterparts. These server-side filters provide higher performance and added scenarios that are specific to Exchange Server. http://blogs.technet.com/b/exchange/archive/2007/01/10/3397707.aspx

More information on the properties that you can use with Exchange Cmdlets in the Filter Parameter : https://technet.microsoft.com/en-us/library/bb738155(v=exchg.150).aspx

Additional Information

Leave a Comment