Office 365 is a powerful suite of productivity tools that empowers businesses with seamless communication and collaboration. One of the essential features of Office 365 is the ability to create and manage distribution lists, which allow users to send emails to a group of people with a single email address.
However, there may come a time when you need to export the members of a distribution list for reporting, analysis, or backup purposes.
In this blog post, we will guide you through exporting Office 365 distribution list members using PowerShell.
Table of Contents
Why Export Distribution List Members?
Exporting distribution list members can be helpful for several reasons:
- Backup and Recovery: Having a list of distribution list members can be crucial for disaster recovery scenarios. If a distribution list is accidentally deleted or corrupted, having a backup of its members can help quickly recreate it.
- Auditing and Compliance: Organizations often need to maintain records for auditing and compliance purposes. Exporting distribution list members can provide a historical record of who was a member of a particular group at a specific point in time.
- Reporting and Analysis: Exporting distribution list members allows you to analyze the distribution of emails within your organization. It can provide insights into communication patterns and help you identify the most active members within specific groups.
Requirements
Before we dive into the steps to export distribution list members using PowerShell, let’s lay down the requirements.
- A computer with Windows PowerShell 5.1 or PowerShell 7+ (recommended)
- Administrator access to the Exchange Online organization. The account must be at least a View-Only Organization Management role group member in Exchange Online.
- The Exchange Online PowerShell (EXO V3) module must be installed on your computer. The latest version as of this writing is 3.2.0.
Export Office 365 Distribution List Members (One Group)
Let’s start with exporting one distribution group. This method requires only the Get-DistributionGroupMember cmdlet.
- Connect to Exchange Online PowerShell:
Connect-ExchangeOnline
- Run the below command to retrieve all members of a distribution list. In this example, we’ll store the result in the $groupMembers variable:
$groupMembers = Get-DistributionGroupMember 'Tech Wizards' -ResultSize Unlimited
- To view the results, you can display them on the screen or the grid view like so.
# Display on the screen $groupMembers | Format-Table GUID, Name, DisplayName, PrimarySMTPAddress, RecipientTypeDetails # Display in a grid view $groupMembers | Select-Object GUID, Name, DisplayName, PrimarySMTPAddress, RecipientTypeDetails | Out-GridView
- To export Office 365 distribution list members to a CSV file, pipe the $groupMembers results to the Export-Csv command.
$groupMembers | Select-Object GUID, Name, DisplayName, PrimarySMTPAddress, RecipientTypeDetails | Export-Csv -Path .\groupmembers.csv -NoTypeInformation
And you now have a CSV export of the distribution list members.
Export Office 365 Distribution List Members Script
When you export Office 365 distribution list members, using the Get-DistributionGroupMember cmdlet alone may be sufficient for one group. Not so much for multiple groups.
As you can see in the previous result, the distribution group also has another group as a member. This situation is called a nested group membership. It is not ideal, but it happens and can be challenging to maintain and track.
So let’s take it up a notch and build a script that exports the members of one, multiple, or all distribution lists. Copy the below script and save it to your computer as Export-DLMembers.ps1.
You can also download this script from this Gist – Export Distribution List Members PowerShell Script.
# Export-DLMembers.ps1 [CmdletBinding()] param ( [Parameter()] $DistributionList ) if ($DistributionList) { try { $group = Get-DistributionGroup -Identity $DistributionList -ErrorAction Stop } catch { $_.Exception.Message | Out-Default return $null } } else { try { $group = Get-DistributionGroup -ResultSize Unlimited -ErrorAction Stop | Sort-Object DisplayName } catch { $_.Exception.Message | Out-Default return $null } } if ($group.Count -gt 0) { for ($i = 0; $i -lt $group.Count; $i++) { Write-Progress -Activity "Distribution List: $($group[$i].DisplayName)" -Status "$($i+1) of $($group.Count) " -PercentComplete (($($i + 1) / $($group.Count)) * 100) try { Get-DistributionGroupMember -ResultSize Unlimited -Identity $group[$i] -ErrorAction Stop | Select-Object @{n = "GroupGUID"; e = { $group[$i].GUID } }, @{n = "GroupName"; e = { $group[$i].Name } }, @{n = "GroupEmail"; e = { $group[$i].PrimarySmtpAddress } }, @{n = "MemberGUID"; e = { $_.GUID } }, @{n = "MemberName"; e = { $_.DisplayName } }, @{n = "MemberEmail"; e = { $_.PrimarySmtpAddress } }, @{n = "MemberType"; e = { $_.RecipientTypeDetails } } } catch { $_.Exception.Message | Out-Default } } }
Let’s break down the script step by step to understand its functionality:
- The script has one optional parameter called DistributionList that accepts the following unique group identities.
- Name
- Alias
- Distinguished name (DN)
- Canonical DN
- Email address
- GUIDDo not use this parameter to process all distribution groups instead.
- The script uses conditional logic to determine which distribution group(s) to export members. It checks if the $DistributionList parameter has been provided. If it has, the script attempts to retrieve information about the specific distribution group provided. If not, it retrieves information about all distribution groups on the server.
- If a specific distribution group was provided ($DistributionList is not null), the script attempts to get information about that group using the Get-DistributionGroup cmdlet. If there is an error (e.g., the group does not exist), the error message is displayed, and the script returns $null.
- If no specific distribution group was provided ($DistributionList is null), the script attempts to retrieve information about all distribution groups on the server using the Get-DistributionGroup cmdlet. The result is sorted by the DisplayName.
- The script then checks if any distribution groups were found ($group.Count -gt 0). If at least one group is found, it enters a loop to process each group.
- Inside the loop, the script displays a progress bar using Write-Progress to indicate which distribution group is currently being processed.
- For each distribution group, the script attempts to retrieve its members using the Get-DistributionGroupMember cmdlet. The member information is then formatted and selected using Select-Object to include various properties such as GroupGUID, GroupName, GroupEmail, MemberGUID, MemberName, MemberEmail, and MemberType.
- The error message is displayed if an error occurs while retrieving group members (e.g., due to connectivity or permissions issues).
Example 1: Export Members of One Distribution List
The following example exports the members of one distribution list called Admin Allies. We’re only showing specific properties for better readability.
.\Export-DLMembers.ps1 'Admin Allies' | Format-Table GroupName, GroupEmail, MemberName, MemberEmail, MemberType
Example 2: Export Members of Multiple Distribution Lists
The following example exports the members of two distribution lists; Organization Owls and Documentation Dukes.
$groups = @( 'Organization Owls', 'Documentation Dukes' ) $groups | ForEach-Object { .\Export-DLMembers.ps1 -DistributionList $_ } | Format-Table GroupName, GroupEmail, MemberName, MemberEmail, MemberType
Example 3: Export Office 365 Group Members to CSV (All Distribution Lists)
The following example exports all members of every distribution list in your Exchange Online organization and redirects the output to a CSV file.
.\Export-DLMembers.ps1 | Export-Csv -NoTypeInformation -Path All_DL_Members.csv
You now have the result in a CSV file.
Conclusion
Using PowerShell to export Office 365 distribution list members is crucial for ensuring data integrity, compliance, and efficient organizational communication. This guide outlines a powerful toolset for extracting distribution list member information, providing benefits for email communication management and analysis.
This practice allows for fast reconstitution of distribution lists, minimizing downtime and maintaining operational continuity. Additionally, the exported data assists in auditing and compliance efforts, creating a historical record of distribution group membership that helps meet regulatory requirements.
Administrators can use the provided PowerShell script to export distribution list members for individual groups, multiple groups, or the whole organization. The script can handle nested group memberships, making it useful even in complex scenarios.