Hello, @Roger Roger,
Welcome to the Microsoft Q&A platform!
Based on your requirements, exporting such data from both on-premises Exchange and Exchange Online involves using the Exchange Management Shell (EMS) for on-premises Exchange and the Exchange Online PowerShell module for Exchange Online.
Below are the steps and scripts to help you export the required information:
- Export Distribution Lists and Mail-Enabled Security Groups from Exchange On-Premises by using Exchange Management Shell
Get-DistributionGroup -ResultSize Unlimited | Select-Object Name, DisplayName, PrimarySmtpAddress, Alias, Description | Export-Csv -Path "C:\temp\OnPrem_DistributionGroups.csv" -NoTypeInformation
>> Get-DistributionGroup -ResultSize Unlimited -RecipientTypeDetails MailUniversalSecurityGroup | Select-Object Name, DisplayName, PrimarySmtpAddress, Alias, Description | Export-Csv -Path "C:\temp\OnPrem_MailEnabledSecurityGroups.csv" -NoTypeInformation
- Export Distribution Lists, Mail-Enabled Security Groups, and Unified Groups from Exchange Online by using Windows PowerShell
Connect-ExchangeOnline
Get-DistributionGroup -ResultSize Unlimited | Select-Object Name, DisplayName, PrimarySmtpAddress, Alias, Description, @{Name="SyncStatus";Expression={if ($_.IsDirSynced) {"Synced"} else {"Cloud"}}} | Export-Csv -Path "C:\temp\Online_DistributionGroups.csv" -NoTypeInformation
>> Get-DistributionGroup -ResultSize Unlimited -RecipientTypeDetails MailUniversalSecurityGroup | Select-Object Name, DisplayName, PrimarySmtpAddress, Alias, Description | Export-Csv -Path "C:\temp\Online_MailEnabledSecurityGroups.csv" -NoTypeInformation
Get-UnifiedGroup -ResultSize Unlimited | Select-Object Name, DisplayName, PrimarySmtpAddress, Alias, Description | Export-Csv -Path "C:\temp\Online_UnifiedGroups.csv" -NoTypeInformation
Disconnect-ExchangeOnline -Confirm:$false
- Combine the CSV Files by using Windows PowerShell
$OnPremDGs = Import-Csv -Path "C:\temp\OnPrem_DistributionGroups.csv"
$OnPremMESGs = Import-Csv -Path "C:\temp\OnPrem_MailEnabledSecurityGroups.csv"
$OnlineDGs = Import-Csv -Path "C:\temp\Online_DistributionGroups.csv"
$OnlineMESGs = Import-Csv -Path "C:\temp\Online_MailEnabledSecurityGroups.csv"
$OnlineUGs = Import-Csv -Path "C:\temp\Online_UnifiedGroups.csv"
$AllGroups = $OnPremDGs + $OnPremMESGs + $OnlineDGs + $OnlineMESGs + $OnlineUGs
$AllGroups | Export-Csv -Path "C:\temp\All_Groups.csv" -NoTypeInformation
Command test results:
This will give you a single CSV file containing all the required information from both Exchange Online and Exchange on-premises.
Should you need more help on this, you can feel free to post back.
If the answer is helpful, please click on “Accept answer” as it could help other members of the Microsoft Q&A community who have similar questions and are looking for solutions.
Thank you for your support and understanding.
Best Wishes,
Alex Zhang