Thursday, 26 April 2018

Export Office 365 Distribution Group Members to CSV using PowerShell

This post will help you to find and export distribution group members in Office 365 by using powershell script. We can list all the office 365 distribution lists by using the Exchange online powershell cmdlet Get-DistributionGroup and its group members by Get-DistributionGroupMember cmdlet.

Before proceed run the following command to connect Exchange Online powershell module.
$365Logon = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $365Logon -Authentication Basic -AllowRedirection
Import-PSSession $Session
Run the following command to list all the distribution groups.
Get-DistributionGroup -ResultSize Unlimited
The following command lists all the members for the given distribution list.
Get-DistributionGroupMember -Identity "<group name>" -ResultSize Unlimited

Export Members of a Single Distribution List to CSV:

The following powershell script gets members of a given distribution group and export members list to CSV file. You can replace the parameter <group name> with your own group name in the below script.
$DGName = "<group name>"
Get-DistributionGroupMember -Identity $DGName -ResultSize Unlimited | Select Name, PrimarySMTPAddress, RecipientType |
Export-CSV "C:\\Distribution-Group-Members.csv" -NoTypeInformation -Encoding UTF8

Export All Distribution Groups and Members List to CSV:

First we can get all the distribution groups by using Get-DistributionGroup cmdlet and retrieve its members by iterating every group with Get-DistributionGroupMember cmdlet. The following script exports all the distribution lists and their memberships to CSV file.
$groups = Get-DistributionGroup -ResultSize Unlimited
$totalmbx = $groups.Count
$i = 1 
$groups | ForEach-Object {
Write-Progress -activity "Processing $_.DisplayName" -status "$i out of $totalmbx completed"
$group = $_
Get-DistributionGroupMember -Identity $group.Name -ResultSize Unlimited | ForEach-Object {
$member = $_
$Result += New-Object PSObject -property @{ 
GroupName = $group.DisplayName
Member = $member.Name
EmailAddress = $member.PrimarySMTPAddress
RecipientType= $member.RecipientType
$Result | Export-CSV "C:\\All-Distribution-Group-Members.csv" -NoTypeInformation -Encoding UTF8

CSV output of O365 Distribution Groups and Members:

Export Office 365 Distribution Groups and their Memberships to CSV



  1. Perfect ! Thank you ! Helped me alot.

  2. Could you help me how to run script "Export All Distribution Groups and Members List to CSV"?

    1. Sorry I think the above script will do the same task : export both groups and members to csv.

      Can you explain your need clearly?

  3. Thanks mate, this is very helpful!

  4. Thanks! This post helped me a bunch! Tweaked a bit to:
    * target a subset of DLs based on a specific string value in the DL name (example "PARK")
    * sort the DLs by Name
    $groups = Get-DistributionGroup –Anr PARK -ResultSize Unlimited | Sort Name

    * remove ".DisplayName" from the Write-Progress -Activity (it was appending onto the group name as a string)
    Write-Progress -Activity "Processing $_" -status "$i out of $totalmbx completed"

    * sort members by DisplayName
    Get-DistributionGroupMember -Identity $group.Name -ResultSize Unlimited | Sort DisplayName | ForEach-Object {

    * include member DisplayName (instead of Name).
    Member = $member.DisplayName

    I ran it in the Windows PowerShell ISE on my Win7 desktop. Maybe that's what CC is asking? Launch Win PS ISE, click File, New, copy and paste the connection lines, copy and paste the "Export All Distribution..." lines, run.

  5. Just used this and it worked great. Thanks!!