How to check User’s Distribution List Membership Using PowerShell

How to check User’s Distribution List Membership Using PowerShell

Managing user memberships in distribution lists (DLs) is a common task for IT administrators. Whether you’re troubleshooting mail flow or auditing access, knowing whether a specific user belongs to all the required distribution lists is essential. In this blog post, we’ll walk through a PowerShell script that helps you check if a user is part of various DLs by reading from a CSV file and exporting the results.

We’ll break down the script line by line, so even if you’re not a scripting expert, you’ll understand how it works.

Introduction

PowerShell is a powerful tool that allows IT professionals to automate tasks efficiently. In this tutorial, we are using PowerShell to check if a user is a member of multiple distribution lists. The script reads a list of DL email addresses from a CSV file, checks each one, and logs whether the user is a member or not.

Prerequisites

Before running this script, make sure you:
• Have the Exchange Online PowerShell module installed.
• Are connected to Exchange Online using PowerShell.
• Have a CSV file with a column named “DLemailaddress” that lists all your distribution lists.

PowerShell Script Breakdown

Here is the complete script:

# Script to check whether the user is a member of all distribution lists or not

# Email address you want to check
$userEmail = "useremailaddress"

# Import your DL list from CSV
$dlList = Import-Csv -Path "C:\Users\Desktop\distributionlistemailaddress.csv" # Update the correct path

# Empty array for output
$output = @()

# Assume the input CSV has a column called "DLemailaddress"
foreach ($dl in $dlList) {
    $dlEmail = $dl.DLemailaddress
    try {
        # Get members of the DL
        $members = Get-DistributionGroupMember -Identity $dlEmail -ResultSize Unlimited

        # Check if user is in the member list
        $isMember = if ($members.PrimarySmtpAddress -contains $userEmail) { "Yes" } else { "No" }

        # Add result to output
        $output += [PSCustomObject]@{
            DistributionList = $dlEmail
            UserIsMember     = $isMember
        }
    }
    catch {
        # If error (like DL not found), capture error
        $output += [PSCustomObject]@{
            DistributionList = $dlEmail
            UserIsMember     = "Error: $_"
        }
    }
}

# Export the results to CSV
$output | Export-Csv -Path "C:\Users\Desktop\Information.csv" -NoTypeInformation -Encoding UTF8

Write-Output "Done! Output saved to C:\Users\Desktop\Information.csv"

Explanation of the Script

  • **$userEmail = “useremailaddress”** – This is the email address of the user you want to check.
  • **$dlList = Import-Csv** – This line reads the CSV file which contains all DL email addresses.
  • **$output = @()** – An empty array where the results (DL name and membership status) will be stored.
  • **foreach ($dl in $dlList)** – Loops through each distribution list from the CSV.
  • **$dlEmail = $dl.DLemailaddress** – Extracts the email address of the DL.
  • **$members = Get-DistributionGroupMember** – Gets all members of the current DL.
  • **$members.PrimarySmtpAddress -contains $userEmail** – Checks if the user is listed as a member.
  • **$output += [PSCustomObject]** – Stores the result as an object to later export to CSV.
  • **Export-Csv** – Saves the final results in a CSV file on your desktop.

Understanding the Output

After running the script, a file named “Information.csv” will be created on your desktop. It contains two columns:
• DistributionList – The email address of the DL.
• UserIsMember – Indicates whether the user is a member (“Yes”), not a member (“No”), or if an error occurred (“Error: …”).

Example Output:

| DistributionList           | UserIsMember |
|—————————|————–|
| hr@company.com            | Yes          |
| it-support@company.com    | No           |
| finance@company.com       | Error: …   |

Real-World Use Cases

• Onboarding Audits: Quickly verify if a new employee has been added to all required mailing lists.
• Access Reviews: Ensure a user doesn’t have unnecessary access.
• Migration Validation: Confirm DL memberships post-migration.

Conclusion

This PowerShell script is a time-saver for system administrators. It’s easy to run, customizable, and provides clear results in a CSV format. With proper formatting and error handling, it’s ready to be used in production environments. Always make sure your CSV input is accurate, and test the script in a non-production environment if possible.

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 *