PowerShell Tips: Retrieving The Distributions Lists A User Is A Member Of

Lets say you have a user and want to get all the distribution lists this user is a member of. Well the following PowerShell script can help you with that. First you will require a remote Exchange shell for this as detailed during the 20:38 minute of the following video:

Next, utilize the following PowerShell script:

1 2 $DN = 'CN=ThmsRynr,OU=BestUsers,DC=lab,DC=workingsysadmin,DC=com' Get-DistributionGroup -filter "members -eq '$DN'" | Select-Object Name,@{l='Members';e={(Get-DistributionGroupMember $_.SamAccountName -ResultSize Unlimited | % { $_.Name }) -join '; ' }}

Line 1 declares the variable to hold the DistinguishedName attribute for the user of interest. Line 2 competes the grunt work of searching. This gets all the distribution groups which have a member equal to the DN of the user of interest.

The above script positions the Distribution Groups return into a Select-Object cmdlet to report the Name property and then a custom column. The label is Members and the content is going to just be a string of all the Distribution Group members’ names separated by semicolons. The expression for my custom column is a Get-DistributionGroupMember command for the Distribution Group piped into a Foreach-Object (alias is “%”) which returns an array of all the names of the members in the Distribution Group. I use the  -join command to take the array and convert it into a string separated by semicolons.

When Get-DistributionGroup is utilized, you do not get the members of that group back with it. Here are the properties that come back that contain the string mem in the name.

1 2 3 4 5 6 7 8 9 10 11 12 PS C:\> Get-DistributionGroup -filter "members -eq '$DN'" | Select-Object -First 1 | Get-Member | Where-Object Name -match 'mem' | Select-Object Name   Name ---- AcceptMessagesOnlyFromDLMembers AcceptMessagesOnlyFromSendersOrMembers AddressListMembership BypassModerationFromSendersOrMembers MemberDepartRestriction MemberJoinRestriction RejectMessagesFromDLMembers RejectMessagesFromSendersOrMembers

Nothing in there contains the members and so the initial script provided in this post is utilized.