Exchange: Get member list recursively from a DL and child DLs


Here is the script I wrote to gather all members from the DL and all the child DLs in it. Script requires you pass Exchange DL name/email and save the returning member list in a variable. Then you may generate a report (export to CSV) or do other things with it.

<#
The script accepts the name or email of an Exchange distribution list (GroupIdentity) 
as a parameter and recursively retrieves all members, including those in any nested 
distribution lists.

Script: Gather-DLMembers.ps1
Parameter: GroupIdenity (DL's name or email address)
Returns: Members list objects
Written by: Anand, the Awesome
#>
param ($GroupIdentity)

function Get-GroupMembersRecursive {
    param (
        [string]$GroupName
    )
    $members = Get-DistributionGroupMember -Identity $GroupName | `
                Where-Object { $PSItem.RecipientType -ne 'User'}
    foreach ($member in $members) {
        if ($member.RecipientType -eq 'MailContact' `
            -or $member.RecipientType -eq 'UserMailbox') 
        {
            $MemberList.Add($member) | Out-Null
        } 
        else 
        {
            Get-GroupMembersRecursive -GroupName $member.Name
        }
    }
}

<#
* * * Script Starts Here * * * 
#>

# Initialize an ArrayList to store member objects
$MemberList = [System.Collections.ArrayList]::new()
Get-GroupMembersRecursive -GroupName $GroupIdentity
return $MemberList

# * * * End of the Script * * * 

Here is some examples:

This displays all members from the DL recursively:

c:\> Gather-DLMembers -GroupIdentity DL-Name

This will export the member list information (Name,Email) to a CSV file.

c:\> Gather-DLMembers -GroupIdentity DL-Name | Select DisplayName,PrimarySMTPAddress | Export-CSV -Path .\DL-Name-Members.csv

This will store the member list in a variable:

c:\> $Members = Gather-DLMembers -GroupIdentity DL-Name

Leave a comment