skip to Main Content

Export distribution group members to CSV with PowerShell

How to export distribution group members to CSV file? We want to have a list of all distribution groups and all members of the group. In this article, you will learn how to bulk export distribution group members to CSV file with PowerShell script.

Introduction

A distribution group, or distribution list, is a collection of two or more people that appears in your organization’s address book. When an email message is sent to a distribution group, it goes to all group members.

Get distribution group members with PowerShell script

You want to see which users are members of the distribution groups, also known as distribution lists. The Export-DistributionGroups.ps1 PowerShell script will run against every distribution group.

These groups are:

  • Distribution group
  • Security group (mail-enabled security groups)

The script will gather the following information per distribution group:

  • Name
  • DisplayName
  • PrimarySmtpAddress
  • SecondarySmtpAddress
  • Alias
  • GroupType
  • RecipientType
  • Members
  • MembersPrimarySmtpAddress
  • ManagedBy
  • HiddenFromAddressLists
  • MemberJoinRestriction
  • MemberDepartRestriction
  • RequireSenderAuthenticationEnabled
  • AcceptMessagesOnlyFrom
  • GrantSendOnBehalfTo

Prepare export distribution groups PowerShell script

Download and place Export-DistributionGroups.ps1 PowerShell script on the Exchange Server C:\scripts folder. If you don’t have a scripts folder, create one. Make sure to check if the file is unblocked to prevent any errors when running the script. Read more in the article Not digitally signed error when running PowerShell script.

Another option is to copy and paste the below code in Notepad. Give it the name Export-DistributionGroups.ps1 and place it in the C:\scripts folder.

# CSV file export path
$Csvfile = "C:\scripts\ExportDGs.csv"

# Get all distribution groups
$Groups = Get-DistributionGroup -ResultSize Unlimited

# Loop through distribution groups
$Groups | ForEach-Object {

    $Group = $_.Name
    $DisplayName = $_.DisplayName
    $PrimarySmtpAddress = $_.PrimarySmtpAddress
    $SecondarySmtpAddress = $_.EmailAddresses | Where-Object {$_ -clike "smtp*"} | ForEach-Object {$_ -replace "smtp:",""}
    $GroupType = $_.GroupType
    $RecipientType = $_.RecipientType
    $Members = Get-DistributionGroupMember $group
    $ManagedBy = $_.ManagedBy
    $Alias = $_.Alias
    $HiddenFromAddressLists = $_.HiddenFromAddressListsEnabled
    $MemberJoinRestriction = $_.MemberJoinRestriction 
    $MemberDepartRestriction = $_.MemberDepartRestriction
    $RequireSenderAuthenticationEnabled = $_.RequireSenderAuthenticationEnabled
    $AcceptMessagesOnlyFrom = $_.AcceptMessagesOnlyFrom
    $GrantSendOnBehalfTo = $_.GrantSendOnBehalfTo

    # Create objects
    [PSCustomObject]@{
        Name                               = $Group
        DisplayName                        = $DisplayName
        PrimarySmtpAddress                 = $PrimarySmtpAddress
        SecondaryStmpAddress               = ($SecondarySmtpAddress -join ',')
        Alias                              = $Alias
        GroupType                          = $GroupType
        RecipientType                      = $RecipientType
        Members                            = ($Members.Name -join ',')
        MembersPrimarySmtpAddress          = ($Members.PrimarySmtpAddress -join ',')
        ManagedBy                          = $ManagedBy.Name
        HiddenFromAddressLists             = $HiddenFromAddressLists
        MemberJoinRestriction              = $MemberJoinRestriction 
        MemberDepartRestriction            = $MemberDepartRestriction
        RequireSenderAuthenticationEnabled = $RequireSenderAuthenticationEnabled
        AcceptMessagesOnlyFrom             = ($AcceptMessagesOnlyFrom.Name -join ',')
        GrantSendOnBehalfTo                = $GrantSendOnBehalfTo.Name
    }

# Export report to CSV file
} | Export-CSV -Path $Csvfile -NoTypeInformation -Encoding UTF8 #-Delimiter ";"
  • Line 2: Edit the CSV file path if you want.

Run export distribution groups PowerShell script

Run Exchange Management Shell as administrator. Change the path to the scripts folder. Run the PowerShell script to export all distribution groups and members to CSV file.

[PS] C:\>cd C:\scripts

[PS] C:\scripts>.\Export-DistributionGroups.ps1

The Export-DistributionGroups script starts scanning the distribution groups in the organization. This can take some time. When done, a list is created and exported in the scripts folder with the name ExportDGs.csv.

Result distribution group export CSV file

Let’s have a look at the CSV export file by going to the following path: C:\scripts\. You should see the ExportDGs.csv file.

Export distribution group members to CSV export file

Open ExportDGs.csv with your favorite application. For example, with Microsoft Excel. If you don’t need all the information, you can remove those columns. You can sort and filter the columns by Name, GroupType, HiddenFromAddressLists, or any other type. Tweak the information to your needs.

Export distribution group members to CSV open file

Did this help you to export distribution group members to CSV file with PowerShell?

Read more: Create distribution group in Exchange Hybrid »

Conclusion

In this article, you learned how to export distribution group members to CSV with PowerShell. The export to CSV file in Exchange admin center will not give you that much information as PowerShell does. With PowerShell, you can have a custom distribution group report. Use the Export-DistributionGroups PowerShell script.

Did you enjoy this article? You may also like Remove users from group with PowerShell. Don’t forget to follow us and share this article.

ALI TAJRAN

ALI TAJRAN

ALI TAJRAN is a passionate IT Architect, IT Consultant, and Microsoft Certified Trainer. He started Information Technology at a very young age, and his goal is to teach and inspire others. Read more »

This Post Has 5 Comments

  1. Does this work on Exchange online aswell?, i am having issues export all DL members for groups seems to only do half

  2. Thank you !, changing that line to “New-Object -TypeName PSObject -Property @{” fixed it. However, for some reason the members column is blank and I’m not sure what the issue could be.

  3. Thank you for sharing this script, whoever, it doesn’t appear to be working for me on Exchange 2010. I ran it in the Exchange Management Shell and it completes without error, however, when I open the CSV file all I see is:

    False;”False”;”False”;”System.Collections.Hashtable+KeyCollection”;”System.Collections.Hashtable+ValueCollection”;”System.Object”;”15″

    What am I doing wrong?

    1. You get this because you are using an older PowerShell version. The [PSCustomObject] was added in PowerShell 3.0. I used it in the script because it will enumerate the data faster.

      Change line 27 in the script

      from:
      [PSCustomObject]@{

      to:
      New-Object -TypeName PSObject -Property @{

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top