Export Members of Multiple Distribution Lists to Separate CSV Files Using PowerShell

Export Distribution List Members to Separate CSV Files Using PowerShell – TrulyBlogs
PowerShell guide to export members of multiple distribution lists into separate CSV files – By TrulyBlogs

Introduction

If you’re managing a Microsoft 365 environment, you might need to export the members of multiple distribution lists (DLs) for auditing, reporting, or administrative purposes. Doing this manually for each list can be time-consuming. Fortunately, with PowerShell, you can automate this task efficiently. In this blog post, we’ll walk through a PowerShell script that exports the members of several distribution lists to separate CSV files. We’ll break down the script line-by-line in plain, human-friendly language and include examples to make it super easy to follow—even if you’re not a scripting expert.

Prerequisites

– Exchange Online PowerShell module installed.
– Connect to Exchange Online using: Connect-ExchangeOnline.
– Your CSV should contain at least two columns: ‘name’ and ’emailaddress’.

Overview of the Script

Here’s the PowerShell script to export all members of multiple distribution lists into separate files:

PowerShell
$Mailboxes = import-csv "C:\Users\Desktop\DistributionListsInformation.csv"

foreach ($mailbox in $mailboxes) {
    $dl = $mailbox.name
    Get-DistributionGroupMember -Identity $mailbox.emailaddress |
        Select PrimarySMTPAddress, DisplayName |
        Export-CSV "C:\Users\Desktop\Information\$dl.csv" -Append -NoTypeInformation
}

Line-by-Line Explanation of the Script

PowerShell
$Mailboxes = import-csv "C:\Users\Desktop\DistributionListsInformation.csv"

Loads the list of distribution groups from a CSV file. Each row should contain the name and email address of a DL.

PowerShell
foreach ($mailbox in $mailboxes)

Loops through each row in the CSV file to process one distribution list at a time.

PowerShell
$dl = $mailbox.name

Stores the name of the current DL in a variable called `$dl`. This will be used as the filename.

PowerShell
Get-DistributionGroupMember -Identity $mailbox.emailaddress

Fetches all members of the current distribution list using its email address.

PowerShell
Select PrimarySMTPAddress, DisplayName

Selects only the Primary SMTP address and Display Name of each DL member.

PowerShell
Export-CSV "C:\Users\Desktop\Information\$dl.csv" -Append -NoTypeInformation

Saves the selected information into a CSV file named after the DL. The `-Append` flag ensures it doesn’t overwrite existing content.

Sample Input CSV Format

Ensure your input CSV (DistributionListsInformation.csv) is structured like this:

name,emailaddress
Sales,sales@domain.com
HR,hr@domain.com

Script Output Example

After running the script, you will find separate CSV files for each distribution list in the specified folder. For example:
– C:\Users\Desktop\Information\Sales.csv
– C:\Users\Desktop\Information\HR.csv

Each file will contain:
– PrimarySMTPAddress: The email address of the member.
– DisplayName: The name shown in the address book.

Common Use Cases

– Auditing group membership.
– Sharing membership lists with department heads.
– Migrating DL members to Microsoft 365 groups.

Conclusion

Exporting distribution list members using PowerShell is fast, scalable, and highly effective—especially in enterprise environments. With this script, you can generate detailed membership reports in just a few seconds. Customize the CSV output and folder path as needed, and you’re all set to automate like a pro!

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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