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.

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)

Get distribution group members with PowerShell script

The Export-DistributionGroups.ps1 PowerShell script works for:

  • Exchange on-premises
  • Exchange hybrid
  • Exchange Online

The script will gather the following information per distribution group:

  1. DisplayName
  2. PrimarySmtpAddress
  3. SecondarySmtpAddress
  4. Alias
  5. GroupType
  6. RecipientType
  7. Members
  8. MembersPrimarySmtpAddress
  9. ManagedBy
  10. HiddenFromAddressLists
  11. MemberJoinRestriction
  12. MemberDepartRestriction
  13. RequireSenderAuthenticationEnabled
  14. AcceptMessagesOnlyFrom
  15. GrantSendOnBehalfTo

Prepare export distribution groups PowerShell script

Download and place Export-DistributionGroups.ps1 PowerShell script in the 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 {

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

    # Create objects
    [PSCustomObject]@{
        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
} | Sort-Object DisplayName | Export-CSV -Path $Csvfile -NoTypeInformation -Encoding UTF8 #-Delimiter ";"
  • Line 2: Edit the CSV file path if you want.

Run export distribution groups PowerShell script

The script works for Exchange on-premises, Exchange hybrid, and Exchange Online. You must connect with the proper tools before you run the script:

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 DisplayName, 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 10 Comments

  1. getting error as ‘Get-DistributionGroupMember’ is invalid parameter

    Get-DistributionGroupMember : The term ‘Get-DistributionGroupMember’ is not recognized as the name of a cmdlet,
    function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the
    path is correct and try again.
    At line:9 char:16
    + $Members = Get-DistributionGroupMember $GroupDN -ResultSize Unlim …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (Get-DistributionGroupMember:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    1. This happens when you run the script in Windows PowerShell without loading the Exchange Management module. The Get-DistributionGroup and Get-DistributionGroupMember cmdlets are Exchange cmdlets.

      You must connect with the proper tools before you run the script:

      – Exchange on-premises / Exchange hybrid: Run Exchange Management Shell as administrator.
      – Exchange Online (Microsoft 365/Office 365): Run PowerShell as administrator and connect to Exchange Online PowerShell.

  2. I have error:
    The operation couldn’t be performed because ‘support’ matches multiple entries.
    + CategoryInfo : InvalidData: (:) [Get-DistributionGroupMember], ManagementObjectNotFoundException
    + FullyQualifiedErrorId : [Server=XXX,RequestId=2b25e926-9888-467b-8438-124f552b1ccd,TimeStamp=03.08.2021 08:55:20] [FailureCategory=Cmdlet-Ma
    nagementObjectNotFoundException] F7410DD6,Microsoft.Exchange.Management.RecipientTasks.GetDistributionGroupMember
    + PSComputerName : EE.xx.local

    1. This happens when you have an identical group name. In your case, it’s the group with the name ‘support’.

      I edited the script to look for a unique group value (DistinguishedName). The script should now work for you without the error.

      PS: It’s not recommended to have the same group name.

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

    1. I added the -ResultSize parameter with the Unlimited value to the Get-DistributionGroupMember cmdlet. This will return all requests.

      The script works for Exchange on-premises and Exchange Online.

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

  5. Hi Ali,
    Thanks for sharing this script. Do you have similar script to import all the Distribution groups using this exported data with all the information like members, managedby, SMTP address, Sender restrictions etc.

  6. 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 *