Introduction
If you’re managing Microsoft Exchange Online, you probably know how important it is to keep track of Distribution Lists (DLs), their owners, and members. Manually pulling this information can be tedious—especially when dealing with multiple DLs. Thankfully, PowerShell can help automate this process. In this blog, we’ll walk through a detailed PowerShell script that fetches the owner(s) and member(s) of each DL and stores everything neatly in a single CSV file.
Full PowerShell Script
Here’s the complete PowerShell script that performs the task:
# Script to get DL's owners and members in same row
# Connect to Exchange Online if not already connected
# Connect-ExchangeOnline
# Import DLs from input CSV
$dlList = Import-Csv -Path "C:\Users\Desktop\DLsEmailAddresses.csv" # Must contain "EmailAddress" column
# Output array
$output = @()
foreach ($dl in $dlList) {
$dlEmail = $dl.EmailAddress
try {
# Get DL
$group = Get-DistributionGroup -Identity $dlEmail -ErrorAction Stop
#### Owners ####
$ownerEmails = @()
if ($group.ManagedBy) {
foreach ($owner in $group.ManagedBy) {
$ownerObj = Get-Recipient -Identity $owner
$ownerEmails += $ownerObj.PrimarySmtpAddress
}
} else {
$ownerEmails = @("No Owners")
}
#### Members ####
try {
$members = Get-DistributionGroupMember -Identity $dlEmail -ResultSize Unlimited -ErrorAction Stop
$memberEmails = $members | Select-Object -ExpandProperty PrimarySmtpAddress
} catch {
$memberEmails = @("Error getting members")
}
#### Add to output ####
$output += [PSCustomObject]@{
DistributionList = $dlEmail
OwnerEmails = $ownerEmails -join ", "
MemberEmails = $memberEmails -join ", "
}
}
catch {
# If DL fails entirely
$output += [PSCustomObject]@{
DistributionList = $dlEmail
OwnerEmails = "Error"
MemberEmails = "Error"
}
}
}
# Export to CSV
$output | Export-Csv -Path "C:\Users\Desktop\information.csv" -NoTypeInformation -Encoding UTF8
Write-Host "✅ Output saved to C:\Users\Desktop\information.csv"
Detailed Script Explanation
Let’s break down the PowerShell script line by line so it’s easy to understand even if you’re new to scripting.
# Connect to Exchange Online
→ This line is a reminder to connect your PowerShell session to Exchange Online. You can do this using the ‘Connect-ExchangeOnline’ command.
$dlList = Import-Csv -Path “C:\Users\Desktop\DLsEmailAddresses.csv”
→ This imports your input file, which should contain a column named `EmailAddress` listing the DLs.
$output = @()
→ This initializes an empty array to store the final output.
foreach ($dl in $dlList)
→ This loop goes through each Distribution List one by one.
$dlEmail = $dl.EmailAddress
→ Grabs the current DL email address from the list.
$group = Get-DistributionGroup -Identity $dlEmail
→ Retrieves the group’s metadata using the DL email.
$ownerEmails = @()
→ Creates an array to store owner email addresses.
foreach ($owner in $group.ManagedBy)
→ Loops through each owner listed in the DL metadata.
$ownerObj = Get-Recipient -Identity $owner
→ Fetches the complete recipient details of the owner.
$ownerEmails += $ownerObj.PrimarySmtpAddress
→ Appends the owner’s primary email address to the list.
else { $ownerEmails = @(“No Owners”) }
→ If no owners exist, records ‘No Owners’.
$members = Get-DistributionGroupMember -Identity $dlEmail
→ Retrieves all members of the Distribution List.
$memberEmails = $members | Select-Object -ExpandProperty PrimarySmtpAddress
→ Extracts only the email addresses of members.
[PSCustomObject]@{…}
→ Creates a structured row with DL, Owners, and Members.
Export-Csv -Path “C:\Users\Desktop\information.csv”
→ Exports the final results into a CSV file.
Write-Host …
→ Prints a confirmation message that the file is saved.
Sample Output
After running the script, a file named `information.csv` is created. Here’s a simplified example of what the CSV might look like:
DistributionList,OwnerEmails,MemberEmails
sales@yourcompany.com,jane@yourcompany.com,alice@yourcompany.com;bob@yourcompany.com
hr@yourcompany.com,No Owners,steve@yourcompany.com
Final Thoughts
This script saves hours of manual work by automating the retrieval of DL owner and member details. It’s especially useful for compliance reporting, auditing, and IT documentation. Make sure your CSV input is accurate, and always test the script in a safe environment before applying changes in production.
If you found this guide helpful, don’t forget to share it or leave a comment. Happy scripting!