Others

How to Export Selected Exchange Online Distribution Lists via PowerShell

Rana GarciaRana Garcia Oct 29, 2026 869 views

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.

How to Export Selected Exchange Online Distribution Lists Using PowerShell
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.
Before you start

Ensure you have the ExchangeOnlineManagement PowerShell module installed, and that you are connected to your Exchange Online tenant with the appropriate administrator permissions.

Solution 1Recommended

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.

1
Prepare the Input CSV File

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.

2
Connect to Exchange Online

Open your PowerShell console as an administrator and run the command 'Connect-ExchangeOnline'. Follow the prompts to authenticate with your Microsoft 365 administrator credentials.

3
Execute the Loop Script

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 }

4
Export the Results to a New CSV

Output the collected data into a final CSV file by running: $results | Export-Csv -Path 'C:\output.csv' -NoTypeInformation.

Use a ForEach Loop to Export Specific Distribution Lists
Script Efficiency: Assigning the output of the foreach loop directly to the '$results' variable ensures that all iterations are properly captured, preventing the common issue where only the last processed list is exported.
Free Microsoft Office alternative

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. 1. Download and Install WPS Office: Get the free WPS Office suite from the official website and complete the installation process.
  2. 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. 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.
Fully compatible with Microsoft Excel formats (.xlsx, .xls, .csv)Lightweight and fast, opening large CSV export files instantlyFree to use with a familiar, intuitive user interfaceSeamless migration for users accustomed to the Microsoft Office ecosystem
microsoft office alternative - wps office

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.