Peter Mellett

Stylised cartoon of me with glasses on.
Articles tagged with 'exchange server'.

List a User's Distribution Group Membership in Exchange 2010 Using PowerShell

Recently I needed a simple way to get a list of all distribution lists a user belonged to. Since we use mail-enabled security groups it is not always obvious which groups have an associated email address at a glance.

I used PowerShell to write a simple script to print a list of a user's distribution groups to the console. The full code is below, but first let's break it down in to the components of the script.

About the script:

 1 # List-DistributionGroups.ps1
 2 # Peter Mellett 2012
 3 # 
 4 # Find all distribution groups of a user and list them to the console
 5 #
 6 # Example:
 7 # .\List-DistributionGroups.ps1 aperson@company.com
 8 #
 9 # aperson@company.com belongs to:
10 # Name
11 # ====
12 # Marketing
13 # Managers
14 # 

The script requires one parameter, the user's email address. This should match the full smtp address for a user in your domain.

1 [CmdletBinding()]
2 Param(
3   [Parameter(Mandatory=$True,Position=1)]
4    [string]$email
5 )

Since this script uses some cmdlets only available in the Exchange Shell, we need to ensure that the exchange shell is available, if it is then we should import it.

Please note: Exchange Shell can have unintended behaviour if imported this way instead of being launched directly. Your mileage may vary, but it's worth noting if you have something weird happen.

First we test the install path of the Exchange 2010 shell. If we find it, import it and connect to the exchange server (using the -auto .flag, it can be configured for your specific environment- see Get-Help Connect-ExchangeServer). If not, we'll print an informational message and exit.

1 if (Test-Path 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1') {
2     . 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'
3     Connect-ExchangeServer -auto
4 
5     # rest of script omitted
6 } else {
7     Wite-Host "This script requires Exchange 2010 Shell to be installed. Either use this script on an Exchange server or install the EMC from the Exchange install media."
8 }

This is the important part of the script, the bit that actually does the work. Here we get all the distribution lists in the domain (warning: slow!) and then test against the members of each comparing it to our entered email address.

If the email is found in the group membership, we'll select the name of that group (which is printed to the console.) Note that the last line has been split across multiple to fit it on the page.

1 Write-Host "$email belongs to: "
2 $lists = Get-DistributionGroup -ResultSize unlimited
3 $lists | where { `
4   (Get-DistributionGroupMember $_ | foreach {$_.PrimarySmtpAddress}) `
5   -contains $email } | select Name

This is a simple script, but it has come in useful a couple of times recently. Definitely better than having to manually check each group.

Cheers,
Pete