How to Export Selected Exchange Online Distribution Lists via PowerShell
Question details
The user needs a PowerShell script to read specific Exchange Online distribution list display names from a CSV file and export their details (such as SMTP addresses and aliases) to a new CSV, avoiding the retrieval of all distribution groups.

- Product
- Exchange Online / PowerShell
- Device & OS
- not provided
- Scenario
- Exporting specific distribution list properties based on a predefined CSV list of display names.
- Observed behavior
- The user wants to ensure the script iterates through each CSV row and outputs all matched lists correctly, preventing an output that only contains the final list or unnecessarily queries every group in the organization.
Ensure you have the ExchangeOnlineManagement PowerShell module installed, and that you are connected to your Exchange Online tenant with the appropriate administrator permissions.
Use a ForEach Loop to Export Specific Distribution Lists
This method reads the display names from an input CSV file and loops through them to fetch and export only the targeted distribution groups.
By utilizing a ForEach loop in PowerShell, you can process each row in your CSV individually. This prevents the script from accidentally pulling the entire directory of distribution lists, significantly improving execution speed and reducing server load.
Create a CSV file named 'input.csv' with a header column named 'DisplayName'. List the names of the specific distribution groups you want to export underneath the header.
Open your PowerShell console as an administrator and run the command 'Connect-ExchangeOnline'. Follow the prompts to authenticate with your Microsoft 365 administrator credentials.
Run the following script to import the CSV, retrieve the data, and build an array: $lists = Import-Csv -Path 'C:\input.csv' $results = foreach ($list in $lists) { Get-DistributionGroup -Identity $list.DisplayName | Select-Object DisplayName, PrimarySmtpAddress, Alias, GroupType }
Output the collected data into a final CSV file by running: $results | Export-Csv -Path 'C:\output.csv' -NoTypeInformation.

Use Get-Recipient for Faster Basic Retrieval
If you only need basic properties like email and alias, using Get-Recipient is significantly faster than Get-DistributionGroup.
Analyze Your Exported CSV Files Easily with WPS Spreadsheet
After exporting your Exchange Online distribution lists using PowerShell, you need a reliable spreadsheet tool to view, filter, and analyze the resulting CSV data. WPS Office is a free, lightweight, and highly compatible alternative to Microsoft Office that handles CSV and Excel files effortlessly.
- 1. Download and Install WPS Office: Get the free WPS Office suite from the official website and complete the installation process.
- 2. Open Your CSV Export: Launch WPS Spreadsheet and navigate to Menu > Open. Select your exported 'output.csv' file generated by the PowerShell script.
- 3. Analyze and Format the Data: Use WPS Spreadsheet's built-in filtering, sorting, and conditional formatting tools to easily review your Exchange Online distribution list properties.

Frequently Asked Questions
Why does my PowerShell script only export the last distribution list?
This usually happens if you overwrite the export file in each iteration of the loop (using Export-Csv without the -Append parameter), or if you assign the output to a standard variable inside the loop instead of capturing the entire loop's output. Assign the foreach block to an array variable or use the -Append flag.
Do I need special permissions to run Get-DistributionGroup in Exchange Online?
Yes, you must be assigned the appropriate Exchange Online administrator roles, such as the Exchange Recipient Administrator or Global Administrator role, to retrieve and manage distribution group details.
How can I format the CSV file before importing it into PowerShell?
Ensure the CSV file is saved with UTF-8 encoding and contains a clear header row (e.g., 'DisplayName'). You can create, format, and review this file easily using spreadsheet software like WPS Spreadsheet or Microsoft Excel to avoid trailing spaces that might break the PowerShell script.




