Distribution groups starting with the same name

Hi ,

I’ve got a script that is removing the users from the distributions groups and 99% this is working like it should.
However when the user is member of one of the below DistributionGroupsLists
the script is not able to remove the user from any of these lists and throws an error that multiple entries where found for *all-engineering and stops completely.
what needs to be modified in the below mentioned code to solve this error?

Import-Module $((Get-ChildItem -Path $($env:LOCALAPPDATA+"\Apps\2.0\") -Filter CreateExoPSSession.ps1 -Recurse ).FullName | Select-Object -Last 1)

Connect-EXOPSSession -UserprincipalName $admin

$users = import-csv C:\Users\Public\Documents\CSV\toRemove.csv

#active users

foreach($user in $users){

write-log -message "Find distributiongroups for user: $($user.email)" -Level Info -Path $logpath

$email = $user.email

$mailbox = Get-Mailbox -Identity $email


$Filter = "Members -like ""$DN"""

$DistributionGroupsList = Get-DistributionGroup -ResultSize Unlimited -Filter $Filter

Write-host `n
Write-host "Listing all Distribution Groups:"
Write-host `n

$distlist = $DistributionGroupsList | ft

$DistributionGroupsList | ft

ForEach ($item in $DistributionGroupsList) {

Remove-DistributionGroupMember -Identity $item.DisplayName –Member $email -Confirm:$false


Write-host `n

Write-host "Successfully removed"

write-log -message "distributiongroups succesfully removed for user: $($user.email)" -Level Info -Path $logpath


write-log -message "Removing assigned phonenumber in Teams please wait...." -Level Info -Path $logpath

write-host -ForegroundColor Cyan "Removing assigned phonenumber in Teams please wait...."

. C:\Users\Public\Documents\PowerShellScripts\RemovePhoneNrTeams.ps1

thanks for your suggestions


what is the error being displayed?

as well, there may be some better approaches just fyi, are you on-premise or in exchange online?

$clouduser = get-user $userid
$clouddistmemberships = Get-DistributionGroup -Filter “Members -like ‘$($clouduser.distinguishedName)’” | where isdirsynced -Notlike “True

this is my approach in term processes to retrieve a users distributiongroup memberships in exchange online

HI David,

thanks for your reaction,
the error message that I get is this

The operation couldn’t be performed because ‘*All-Engineering’ matches multiple entries.

  • CategoryInfo : NotSpecified: (:slight_smile: [Remove-DistributionGroupMember], ManagementObjectAmbiguousException
  • FullyQualifiedErrorId : [Server=LNXP265MB1052,RequestId=8b03b9f5-04aa-41f2-b9c4-b366f4753927,TimeStamp=10/1/2020 5:34:17 AM] [FailureCa
    tegory=Cmdlet-ManagementObjectAmbiguousException] 345C0DE0,Microsoft.Exchange.Management.RecipientTasks.RemoveDistributionGroupMember
  • PSComputerName : outlook.office365.com




try using $item.alias
that should always be unique. you could also try $item.primarysmtpaddress

you need to utilize a supported parameter that is unique.

per get-help:
-Identity <DistributionGroupIdParameter>
The Identity parameter specifies the distribution group or mail-enabled security group that you want to view.
You can use any values that uniquely identifies the group.

    For example:
    * Name
    * Display name
    * Alias
    * Distinguished name (DN)
    * Canonical DN
    * Email address
    * GUID

so you need to pick one of those values, that is distinct. guessing that is whats causing your issue.


the Alias option did solve my problem thanks


Best regards