You want to synchronize your on-premises users, for example with Office 365. Before you do…
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.
Table of contents
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:
- DisplayName
- PrimarySmtpAddress
- SecondarySmtpAddress
- Alias
- GroupType
- RecipientType
- Members
- MembersPrimarySmtpAddress
- ManagedBy
- HiddenFromAddressLists
- MemberJoinRestriction
- MemberDepartRestriction
- RequireSenderAuthenticationEnabled
- AcceptMessagesOnlyFrom
- GrantSendOnBehalfTo
- 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:
- 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.
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.
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.
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.
Great script. Thank you for posting!
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
how can i add so it only searches in a specific OU?
Get the OU distinguishedName and follow the below steps:
Change lines 4-5 in the script:
With the below lines:
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?
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?
Dear Mr.Ali, Thank you very much for this script, this made our job quick and helped a lot!!
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?
Can you add the field of whether the DG is open to external senders?
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.
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.
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
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
I added the “Notes” field to the script.
Great script!
Any advice on how you would go about using something like this to add a batch of staff to new distribution lists?
The scripts works like a charm, but the ManagedBy parameter is blank, using Exchange Online.
¿Can you help me?
Change on line 37:
$ManagedBy.Name to $ManagedBy
IMO this is one of the best online resource for exchange admin.
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.
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.
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
Hi Ali, How do I modify the script to get the members of a Distribution List?
How do I add members of to the list? I want to see if a DL is also part of another group/DL.
Thanks
Instead of -join ‘,’ how to I get a new member name to go to a new line?
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.
Hi,
Incase if I need to get report of the user’s account status, where should I include the field in the script.
Great script thank you very much. <3
My requirement is to run against one DL only ???
Hi I follow your instructions and it worked perfectly! Thanks for sharing my friend!
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
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.
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
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.
Does this work on Exchange online aswell?, i am having issues export all DL members for groups seems to only do half
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.
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.
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?
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 @{
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.