How to Get Distribution List Owners in the Same Row using PowerShell

How to Get Distribution List Owners in the Same Row using PowerShell

Introduction

Managing Distribution Lists (DLs) in Microsoft Exchange Online can be a challenge, especially when you’re trying to find out who owns what. This blog walks you through a practical PowerShell script that helps you extract the owners of multiple Distribution Lists and presents them neatly in a single row. Perfect for IT admins and support engineers, this guide will make your DL management more efficient.

Why This Script is Useful

Knowing who owns a Distribution List is essential for governance, accountability, and smooth communication. This script helps you quickly generate a report that lists DLs alongside their owners. It’s especially handy when you’re dealing with a large number of DLs and want to avoid manually checking each one.

Knowing who owns a Distribution List is essential for governance, accountability, and smooth communication. This script helps you quickly generate a report that lists DLs alongside their owners. It’s especially handy when you’re dealing with a large number of DLs and want to avoid manually checking each one.

Prerequisites

Before running the script, make sure you have the following:
– Access to Exchange Online PowerShell.
– The ExchangeOnlineManagement module installed.
– A CSV file with a column named ‘EmailAddress’ containing the DLs.

Full PowerShell Script

# Connect to Exchange Online if not already connected
# Connect-ExchangeOnline

# Import list of DLs from CSV
$dlList = Import-Csv -Path "C:\Users\Desktop\DLsEmailAddresses.csv" # Make sure it has a column "EmailAddress"

# Array for output
$output = @()

foreach ($dl in $dlList) {
    $dlEmail = $dl.EmailAddress
    try {
        # Get DL object
        $group = Get-DistributionGroup -Identity $dlEmail -ErrorAction Stop

        # Get owner emails
        $ownerEmails = @()
        if ($group.ManagedBy) {
            foreach ($owner in $group.ManagedBy) {
                $recipient = Get-Recipient -Identity $owner
                $ownerEmails += $recipient.PrimarySmtpAddress
            }
        } else {
            $ownerEmails = @("No Owners")
        }

        # Add to output
        $output += [PSCustomObject]@{
            DistributionList = $dlEmail
            OwnerEmails      = ($ownerEmails -join ", ")
        }
    }
    catch {
        # Handle errors per DL
        $output += [PSCustomObject]@{
            DistributionList = $dlEmail
            OwnerEmails      = "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"

Script Breakdown and Explanation

  • **# Connect to Exchange Online**

This line (commented out) is where you’d establish a session with Exchange Online using the `Connect-ExchangeOnline` command. It’s required if you haven’t already connected to your tenant.

  • **$dlList = Import-Csv -Path “C:\Users\Desktop\DLsEmailAddresses.csv”**

This line loads a CSV file containing a list of Distribution Lists (DLs). The file must have a column called `EmailAddress`, which lists each DL you want to check.

  • **$output = @()**

This creates an empty array that will store the final output — each item being a custom object with the DL email and its owners.

  • **foreach ($dl in $dlList) {**

This loop goes through each row (DL) in the imported CSV.

  • **$dlEmail = $dl.EmailAddress**

This extracts the actual email address of the Distribution List from the current row.

  • **$group = Get-DistributionGroup -Identity $dlEmail -ErrorAction Stop**

Attempts to fetch the distribution group object for the given email. If the DL doesn’t exist or there’s another issue, the `-ErrorAction Stop` ensures it jumps to the catch block.

  • **$ownerEmails = @()**

Initializes a temporary array to hold all owner email addresses for the current DL.

  • **if ($group.ManagedBy)**

Checks if the DL has any assigned owners using the `ManagedBy` property.

  • **foreach ($owner in $group.ManagedBy) { … }**

Loops through each owner, retrieves their recipient object using `Get-Recipient`, and adds their primary SMTP address to the list.

  • **$ownerEmails = @(“No Owners”)**

If no owners are found, this sets a default message ‘No Owners’.

  • **$output += [PSCustomObject]@{ … }**

Creates a custom object containing the DL and a string of comma-separated owner emails, and adds it to the `$output` array.

  • **catch { … }**

Catches any errors (e.g., DL not found), and stores an error message in the output.

  • **Export-Csv -Path “C:\Users\Desktop\information.csv” -NoTypeInformation -Encoding UTF8**

Exports the `$output` array to a CSV file on the desktop, encoded in UTF-8.

  • **Write-Host “✅ Output saved to …”**

Prints a success message to the screen once the export is complete.

Sample Output and What It Means

After running the script, you’ll get a CSV file (`information.csv`) like this:

DistributionListOwnerEmails
team@company.comjohn.doe@company.com, jane.smith@company.com
sales@company.comNo Owners
hr@company.comError: The term “Get-DistributionGroup” is not recognized

– The first row shows a DL with two owners.
– The second row has no owners set.
– The third row shows an error—possibly due to a typo or missing module.

Common Errors and How to Fix Them

– **Get-DistributionGroup not recognized**: Ensure the ExchangeOnlineManagement module is installed and imported.

– **Access Denied**: Run PowerShell as administrator and ensure you have the required permissions.

– **No owners found**: This is normal if no owners are assigned to that DL.

Final Thoughts

This script is a powerful tool for Exchange admins managing large environments. It saves time and brings visibility into who owns what. You can further enhance it to include more DL properties or schedule it to run periodically.

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 *