PS Script to Get Members of a DL group in Exchange Online

Chandra Mohan S 0 Reputation points
2025-02-04T05:00:23.0066667+00:00

I'm trying to get the list of DL groups user is member of in O365. I'm using below PS Script but I'm getting the error message.

$user = "test1@rheem.com" $dlGroups =Get-DistributionGroup | Get-DistributionGroupMember | Where-Object {$_.PrimarySmtpAddress -eq $userEmail}

foreach($dl in $dlGroups){ Write-Output $dl #Remove-DistributionGroupMember -Identity $dl.Identity -Member $user

}

error:

Write-ErrorMessage : ||The operation couldn't be performed because 'Bus Training School' matches multiple entries.

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,794 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Kavya 490 Reputation points
    2025-02-06T09:06:28.47+00:00

    The error occurs because multiple groups have the same name. In such cases, you need to use a unique property, such as the primary SMTP address, to identify the group. You can use the below script:

    Connect-ExchangeOnline
    $UserEmail = <MemberEmail>
    Get-DistributionGroup -ResultSize Unlimited | Where-Object { 
        (Get-DistributionGroupMember -Identity $_.PrimarySmtpAddress -ResultSize Unlimited).PrimarySmtpAddress -contains $UserEmail 
    } | Select Name, PrimarySmtpAddress
    

    Replace the <MemberEmail> with a respective user.

    The above script will retrieve all DLs a user is memberof. If you want to find for multiple users, you can use the script available here: https://o365reports.com/2022/04/19/list-all-the-distribution-groups-a-user-is-member-of-using-powershell/

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.