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 start, run the following command to connect Exchange Online module.

Connect-ExchangeOnline

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.

$Result=@()
$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
}}
$i++
}
$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

Advertisement

7 thoughts on “Export Office 365 Distribution Group Members to CSV using PowerShell”

  1. 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.

    Reply

Leave a Comment