Skip to content

Export distribution group and members with PowerShell

You can export the distribution group information from the Microsoft 365 admin center, but it does not show all the details. With PowerShell, it’s faster to export detailed data of distribution groups to a CSV file. In this article, you will learn how to export a distribution group and members with PowerShell.

Connect to Exchange Online

To be able to run PowerShell commands, you must Connect to Exchange Online PowerShell. Open Windows PowerShell as administrator, run the below cmdlet, and sign in with your admin credentials.

Connect-ExchangeOnline

View distribution group list and members

We will show how to display information about distribution groups in PowerShell output. Remember that all the information you can display on the PowerShell console you can also export to a CSV file.

Display distribution group list

Let’s get a list of all the existing (security) distribution groups with information. To view existing distribution groups or mail-enabled security groups, use the Get-DistributionGroup cmdlet.

Get-DistributionGroup -ResultSize Unlimited

The PowerShell output example:

Name            DisplayName         GroupType PrimarySmtpAddress               
----            -----------         --------- ------------------               
Sales Employees Sales Employees     Universal Sales.Employees@m365info.com     
Sales UK        Sales UK            Universal SalesUK@m365info.com
Sales USA       Sales USA mail list Universal SalesUSAnew@m365info.com

Display distribution group members

You can display the members of all the existing distribution groups.

See the below PowerShell command syntax.

Get-DistributionGroupMember -ResultSize Unlimited "Distribution Group"

Run the below PowerShell command example.

Get-DistributionGroupMember -ResultSize Unlimited "SalesUK@m365info.com"

The PowerShell output shows the identity of the members.

Name                                 RecipientType
----                                 -------------
41377e9c-dc47-46c0-b4a5-1d5bbdcb5cc5 UserMailbox  

Display distribution group members details

To get extended information about the members of a specific distribution group, we need to use the PowerShell cmdlet Get-DistributionGroupMember. We will sort the result and display the following information about the members DisplayName, Alias, and Department.

PowerShell command example:

Get-DistributionGroupMember -ResultSize Unlimited "SalesUSA@m365info.com" | Sort -Property DisplayName | Select-Object DisplayName, Alias, Department

PowerShell output results:

DisplayName  Alias        Department
-----------  -----        ----------
Brenda Smith Brenda.Smith           
David Kent   David.Kent   Sales     

Count the number of distribution group members

You can also view the number of members a specific distribution group has.

See the below PowerShell command syntax.

(Get-DistributionGroupMember -ResultSize Unlimited "Distribution Group").Count

Run the below PowerShell command example.

(Get-DistributionGroupMember -ResultSize Unlimited "SalesUSA@m365info.com").Count

It will show the number of members in the PowerShell output.

Get distribution group membership of specific user

You can also find out how many memberships a specific user has.

  1. Type the Identity of the user in the first line
  2. Run the below PowerShell script
$User = "Brenda.Smith@m365info.com"
$UserDisplayName = (Get-User $User).Name
"The user " + $User + " is a member of the following Distribution Groups:"

foreach ($DistributionGroup in Get-DistributionGroup -ResultSize Unlimited) {
    if ((Get-DistributionGroupMember $DistributionGroup.PrimarySmtpAddress | Select-Object -ExpandProperty Name) -contains $UserDisplayName) {
        $DistributionGroup.PrimarySmtpAddress
    }
}

The PowerShell output result shows to which distribution groups they are a member.

The User Brenda.Smith@m365info.com is a member of the following Distribution Groups:
SalesEmployees@m365info.com
SalesUSA@m365info.com

In our example, the user Brenda is a member of two distribution groups.

Display distribution group list with specific email domain

View a list of distribution groups of a specific domain. This is useful if you have many distribution groups and want to narrow it down to a specific domain.

See the PowerShell command syntax.

Get-DistributionGroup -ResultSize Unlimited  -Filter {EmailAddresses -like "*Domain Name"} | ft -Property Name, Alias, EmailAddresses -Autosize

In our example, we want to display a list of distribution groups whose email address includes the domain name m365info.com

Run the PowerShell command example.

Get-DistributionGroup -ResultSize Unlimited  -Filter {EmailAddresses -like "*m365info.com*"} | ft -Property Name, Alias, EmailAddresses -Autosize

The PowerShell output shows the following result.

Name            Alias           EmailAddresses                                                                                      
----            -----           --------------                                                                                      
Sales Employees Sales.Employees {SMTP:Sales.Employees@m365info.com, smtp:Sales.Employees@ms365info.onmicrosoft.com}                 
Sales UK        SalesUK         {SMTP:SalesUK03@m365info.com, smtp:SalesUK@ms365info.onmicrosoft.com}                               
Sales USA       SalesUSA        {SMTP:SalesUSAnew@m365info.com, smtp:SalesUSA@m365info.com, smtp:SalesUSA@ms365info.onmicrosoft.com}

Display distribution groups in specific date range

You can get a list of distribution groups created on a specific date, time, or time range.

  1. We want to add a user to all new distribution groups created in a specific time range (last two weeks)
  2. We want to add a user to all distribution groups created in a specific time range (before the last two weeks)

To know when a specific distribution group was created, we can query the WhenCreated. To find the Distribution Group that belongs to the specific time range, such as the last two weeks or the time before the current two weeks, we need to use the PowerShell logical operators.

In our example, we create two PowerShell queries that will use the following operators -ge and -le, and get the following information:

  • Get a list of distribution groups created in the last 2 weeks. For this purpose, we use the PowerShell operator -ge (Greater than or equal to).
  • Get a list of distribution groups created before the last 2 weeks. For this purpose, we use the PowerShell operator -le (Less than or equal to).

To define the time range of two weeks, we will use the PowerShell cmdlets Get-Date in the following way: (Get-Date).AddDays(-14).

Get distribution groups created in the last 2 weeks

Get a list of distribution groups created in the last 14 days.

Run the below PowerShell example.

Get-DistributionGroup -ResultSize Unlimited -Filter "WhenCreated -ge '$((Get-Date).AddDays(-14))'" | Format-Table DisplayName, WhenCreated

See the PowerShell output result.

DisplayName         WhenCreated        
-----------         -----------        
Sales Employees     25/08/2023 23.07.36
Sales UK            07/09/2023 11.53.47
Sales USA mail list 07/09/2023 10.00.12

Get distribution groups created before the last 2 weeks

Get a list of distribution groups created before the last 14 days.

Run the below PowerShell command example.

Get-DistributionGroup -ResultSize Unlimited -Filter "WhenCreated -le '$((Get-Date).AddDays(-14))'" | Format-Table DisplayName, WhenCreated

The PowerShell output result shows a list of all distribution groups created before the last two weeks.

DisplayName     WhenCreated        
-----------     -----------        
Sales Employees 25/08/2023 23.07.36

Display all distribution groups managed by specific owner

Get a list of all distribution groups managed by a specific name. Only view the distribution groups where this specific name is the owner.

In our example, it will display all the distribution groups with the owner that has the word Amanda.

  • Type the Identity name between the asterisks (*)
  • Run the below PowerShell script
$users = Get-User -Identity *amanda*

foreach ($user in $users) {
    $userPrincipalName = $user.UserPrincipalName
    $displayName = $user.DisplayName

    Get-DistributionGroup -ResultSize Unlimited | Where-Object { $_.ManagedBy -like "*$($user.Identity)*" } | 
    Select-Object DisplayName, PrimarySmtpAddress,
    @{Name = 'UserPrincipalName'; Expression = { $userPrincipalName } },
    @{Name = 'UserDisplayName'; Expression = { $displayName } }
}

The PowerShell output shows all the distribution groups that belong to Amanda.

DisplayName         PrimarySmtpAddress          UserPrincipalName          UserDisplayName
-----------         ------------------          -----------------          ---------------
Sales Germany       SalesGermany@m365info.com   Amanda.Hansen@m365info.com Amanda Hansen
Sales Germany       SalesGermany1@m365info.com  Amanda.Hansen@m365info.com Amanda Hansen
Sales France        SalesFrance@m365info.com    Amanda.Morgan@m365info.com Amanda Morgan

Display all owners of a single distribution group

You can display which owners a specific distribution group has.

Run the below PowerShell command to display owners of a specific distribution group.

$group = Get-DistributionGroup -ResultSize Unlimited -Filter "PrimarySmtpAddress -eq 'SalesGermany@m365info.com'"
$managedBy = $group.ManagedBy | Get-User
$managedBy | Select-Object DisplayName, Identity

The PowerShell output shows the Object ID of all the owners.

DisplayName    Identity
-----------    --------
Brenda Smith   0f38d53f-cbe0-4844-86e9-1032a45ba31b
George Wilson  67962421-00e7-448b-b382-83b7b434e41c
Amanda Hansen  41377e9c-dc47-46c0-b4a5-1d5bbdcb5cc5
Ken Walker     12eefbb2-e5f4-4eec-bd18-df7ca2f1ee6b

Display all distribution groups with moderator

Not all the distribution groups have a moderator because it’s not set by default. A moderator can approve or reject the messages sent to the distribution group.

  1. Show a list of all the existing distribution groups with and without a moderator

Run the below PowerShell command example:

Get-DistributionGroup -ResultSize Unlimited | ft DisplayName, ModeratedBy

PowerShell output result:

DisplayName          ModeratedBy                                                                 
-----------          -----------                                                                 
Sales Employees      {}                                                                          
Sales UK             {41377e9c-dc47-46c0-b4a5-1d5bbdcb5cc5, eec2668a-0773-4947-93ba-2223f6acfe55}
Sales USA mail list  {}                                                                          
  1. Show a list of all the distribution groups that have a moderator

Run the below PowerShell command example.

Get-DistributionGroup -ResultSize Unlimited -Filter 'ModeratedBy -ne $null' | ft DisplayName, ManagedBy

The PowerShell output shows the below result.

DisplayName ManagedBy                             
----------- ---------                             
Sales UK    {41377e9c-dc47-46c0-b4a5-1d5bbdcb5cc5, eec2668a-0773-4947-93ba-2223f6acfe55}

Display all distribution groups that are synchronized from On-Premises Active Directory

Run the below PowerShell command example.

Get-DistributionGroup -ResultSize Unlimited | Where-Object { $_.IsDirSynced -eq $true } | ft DisplayName, IsDirSynced

Display distribution group delivery management settings

The term delivery management is related to the users who can send messages to the distribution group. There are two types of senders:

  • Internal senders (organization recipients), which are described as authenticated recipients
  • External senders (non-organization recipients), which are described as unauthenticated recipients

When you create a new distribution group, the option Only allow messages from people inside my organization is enabled. The parameter that defines this option is -RequireSenderAuthenticationEnabled.

By default, the value of the parameter -RequireSenderAuthenticationEnabled is set to $True. Only people inside your organization can send messages to the distribution group.

You can also allow people outside the organization to send to the distribution group. The value of the parameter -RequireSenderAuthenticationEnabled should be set as $False.

We will display a list of all distribution groups that have enabled or disabled external senders.

Run the PowerShell command example.

Get-DistributionGroup -ResultSize Unlimited | ft DisplayName, RequireSenderAuthenticationEnabled

The PowerShell output result.

DisplayName          RequireSenderAuthenticationEnabled
-----------          ----------------------------------
Mail security                                     False
Sales Employees                                   False
Sales France                                       True
Sales Germany                                      True
Sales Norway                                       True
Sales Spain                                        True
Sales Sweden                                       True
Sales UK                                          False
Sales USA mail list                               False

Display distribution groups that accept email from external recipients

Only view a list of distribution groups that accept external recipients to send emails.

Run the PowerShell command example.

Get-DistributionGroup -ResultSize Unlimited | Where-Object { $_.RequireSenderAuthenticationEnabled -eq $True } | ft DisplayName, RequireSenderAuthenticationEnabled

See the below PowerShell output result.

DisplayName RequireSenderAuthenticationEnabled
----------- ----------------------------------
Sales UK                                  True

Display distribution groups that don’t accept email from external recipients

We will only display a list of distribution groups that don’t allow people outside the organization (external recipients) to send emails to this group.

Run the below PowerShell command:

Get-DistributionGroup -ResultSize Unlimited | Where-Object { $_.RequireSenderAuthenticationEnabled -eq $False } | ft DisplayName, RequireSenderAuthenticationEnabled

The PowerShell output result:

DisplayName         RequireSenderAuthenticationEnabled
-----------         ----------------------------------
Sales Employees                                  False
Sales USA mail list                              False

Export distribution group list including members to CSV

We will show you how to export all the information displayed as the PowerShell output results. You can export PowerShell output in the following file formats: Text, CSV, HTML, and XML.

To export PowerShell command output to a CSV (Comma Separated Value) file format, we can add additional parameters such as:

  • -NoTypeInformation: It prevents PowerShell from adding unnecessary information to the CSV file
  • -Encoding UTF8: It can export all non-English characters, such as Chinese or Arabic language

You need to add a path to export the data, meaning you must create a specific folder (temp) and save it in the (C:) drive. No PowerShell command will create a specific folder specified in the path.

Once that is set, we will show you how to export distribution group list information to various file types.

  1. Export information to CSV file PowerShell command example:
Get-DistributionGroup -ResultSize Unlimited | Export-CSV "C:\temp\DistributionGroups.csv" -NoTypeInformation -Encoding UTF8
  1. Export information to Text file PowerShell command example:
Get-DistributionGroup -ResultSize Unlimited | Out-File "C:\temp\DistributionGroups.txt"
  1. Export information to HTML file PowerShell command example:
Get-DistributionGroup -ResultSize Unlimited | ConvertTo-Html | Out-File "C:\temp\DistributionGroups.html"

Export single distribution group list including members to CSV

You can export information of all members of a specific distribution group.

In our example, we want to export all the members of the distribution group Sales UK.

Get-DistributionGroupMember -ResultSize Unlimited -Identity "Sales UK" | Select-Object DisplayName, PrimarySmtpAddress, ExternalEmailAddress, Alias | Export-Csv "C:\temp\DistributionGroupMembers.csv" -NoTypeInformation -Encoding UTF8

It will export the CSV file to the C:\temp folder. Open the CSV file with Microsoft Excel to see the results.

Export information Distribution Group list members

The below picture shows an example of a CSV file with all the group members (including contacts) of the distribution group Sales UK.

How to export distribution group and members with PowerShell to CSV

Export list of all distribution groups including members to CSV

You can export a list of all the distribution group members to a single CSV file.

Run the PowerShell script.

$Groups = Get-DistributionGroup -ResultSize Unlimited

# Initialize an array to store the data
$data = @()

foreach ($Group in $Groups) {
    $Members = Get-DistributionGroupMember -ResultSize Unlimited -Identity $Group.PrimarySmtpAddress

    foreach ($Member in $Members) {
        if ($Member.PrimarySmtpAddress) {
            $Recipient = Get-Recipient -Identity $Member.PrimarySmtpAddress

            $data += [PSCustomObject]@{
                GroupName = $Group.DisplayName
                GroupPrimarySmtpAddress = $Group.PrimarySmtpAddress
                MemberDisplayName = $Member.DisplayName
                MemberPrimarySmtpAddress = $Member.PrimarySmtpAddress
                RecipientType = $Recipient.RecipientType
                MemberAlias = $Recipient.Alias
            }
        } else {
            $data += [PSCustomObject]@{
                GroupName = $Group.DisplayName
                GroupPrimarySmtpAddress = $Group.PrimarySmtpAddress
                MemberDisplayName = $Member.DisplayName
                MemberPrimarySmtpAddress = "-"
                RecipientType = "-"
                MemberAlias = "-"
            }
        }
    }
}

# Export the data to a CSV file
$data | Export-Csv -Path "C:\temp\AllDistributionGroupMembers.csv" -NoTypeInformation -Encoding UTF8

It will export the CSV file to the C:\temp folder. Open the CSV file with Microsoft Excel to see the results.

Export information Distribution Group list all members

The final result shows a list of all the distribution groups, including their members and contacts in your organization.

How to export all distribution group members with PowerShell to CSV file

Read more: Export Microsoft 365 mailbox to PST file »

Conclusion

You learned how to export distribution groups and members with PowerShell. Get a detailed list of all the distribution groups, including their members, and export it to a CSV file. With PowerShell, you can customize and create your own detailed report about all distribution groups.

Did you enjoy this article? You may also like Export Microsoft 365 mailbox size report with PowerShell. Don’t forget to follow us and share this article.

o365info Team

o365info Team

This article was written by our team of experienced IT architects, consultants, and engineers.

This Post Has 0 Comments

Leave a Reply

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