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
  16. Notes

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 into 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
    $Notes = (Get-Group $GroupDN)

    # 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
        Notes                              = $Notes.Notes
    }

# 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 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

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 as 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 39 Comments

  1. Hi Ali dank voor het delen van dit geweldige script.
    Het is me gelukt alle gegevens te exporteren. Zit nu alleen met een uitdaging de gegevens ook in Exchange 365 te importeren maar wat ik ook probeer het lukt me alleen de DL groep, e-mail, owner en members te importeren en niet de andere gegevens die in geëxporteerde CSV staan. Kan jij ons bij staan . Alvast enorm bedankt voor je flexibiliteit.

    Wij zijn bezig te migreren vanuit Exchange 2016 on prem naar Exchange 365

    1. Get the OU distinguishedName and follow the below steps:

      Change lines 4-5 in the script:

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

      With the below lines:

      # Fill in OU
      $OUScope = "OU=Finance,OU=Company,DC=exoip,DC=local"
      
      # Get all distribution groups in OU
      $Groups = Get-DistributionGroup -OrganizationalUnit $OUScope -ResultSize Unlimited
      1. Hi Ali
        it says it can find the OU, are the path different on exchange online and on-premise?
        what can i have been doing wrong?

  2. Hi. This script is awesome. Thank you for sharing.

    I’ve been trying to get it to also pull the UPN of the members of each DL without success. Is that possible?

    Maybe you could point me in the right direction?

  3. Really appreciate the work you did on this script, worked a gem for me.
    I performed this script on exchange 2013 OnPrem and now was to use this CSV file to create and import into Exchange Online.

    Is it just a matter of changing get-distribution to new-distribution?

    Or do you have to start from scratch?

    1. The RequireSenderAuthenticationEnabled parameter specifies whether to accept messages only from authenticated (internal) senders.

      Valid values are:

      True: Messages are accepted only from authenticated (internal) senders. Messages from unauthenticated (external) senders are rejected.
      False: Messages are accepted from authenticated (internal) and unauthenticated (external) senders.

  4. Dear Ali,

    Thank you for your script. It inspired mine by adding lines with the members of the group so that with my .csv edited in excel, I can play around with column filters.

  5. This script is exactly what i needed. thank you! i was doing manual scripts with seperate steps.

    is there a way to use the results to create distribution lists on Exchange Online using the same attributes exported initially?

    we are trying to remove the on-prem DL’s, and recreating them on Exchange Online. i am struggling especially with adding the members to the groups in bulk when it exports with seperation char “,” .

    the exported results are exactly what i want created aswell when creating the DL’s on Exchange online

  6. Hi ALI TAJRAN. thx for the PS script… is there a way to add the description of the DG’s in the ps script. thx

  7. Great script!
    Any advice on how you would go about using something like this to add a batch of staff to new distribution lists?

  8. The scripts works like a charm, but the ManagedBy parameter is blank, using Exchange Online.

    ¿Can you help me?

  9. How can I add more attributes?
    I want to extract with member, Title, Owner, and count of the people added in DL.
    Please help me with this.

  10. I have a similar question as Stanley Dmello up above has, do you have a similar script for importing it? I am wanting to Export from O365 and import it to Exchange On Prem – Exchange Admin Center.

  11. Hi Ali,
    Elegant script! Works great, thank you.
    Any chance you can help me with a modification?
    I’d like to run this against a csv containing a curated list of distros, instead of against every distro in the domain.
    I can set the csv up anyway that makes this easy.
    Thanks,
    Ken

  12. really great script and it worked for me but I didnt get all the other sub domains groups. What it show only the exchange domain only. what I need to have the groups for all the subdomains also … Thanks.

  13. Hi,
    Incase if I need to get report of the user’s account status, where should I include the field in the script.

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

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

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

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

  18. 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 @{

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

Leave a Reply

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