简体   繁体   中英

Powershell script to remove users from distribution groups

I'm trying to find a solution to remove a user from all the distribution groups they are in. I found this script but am running into issues:

$email = Read-Host "Please provide a user's email address to remove from all distribution groups"
$mailbox = Get-Mailbox -Identity $email
$DN=$mailbox.DistinguishedName
$Filter = "Members -like ""$DN"""
$DistributionGroupsList = Get-DistributionGroup -ResultSize Unlimited -Filter $Filter
Write-host `n
Write-host "Listing all Distribution Groups:"
Write-host `n
$DistributionGroupsList | ft
$answer = Read-Host "Would you like to proceed and remove $email from all distribution groups ( y / n )?" 
While ("y","n" -notcontains $answer) {
    $answer = Read-Host "Would you like to proceed and remove $email from all distribution groups ( y / n )?"
    }
If ($answer -eq 'y') {
    ForEach ($item in $DistributionGroupsList) {
        Remove-DistributionGroupMember -Identity $item.DisplayName –Member $email –BypassSecurityGroupManagerCheck -Confirm:$false
    }
    
    Write-host `n
    Write-host "Successfully removed"
    Remove-Variable * -ErrorAction SilentlyContinue
    }
Else
    {
    Remove-Variable * -ErrorAction SilentlyContinue
    }

It will get to the stage where it lists all the groups a user is in and asks whether or not to remove them, however it seems to get stuck on the –BypassSecurityGroupManagerCheck advising that there is an issue with this parameter.

I found this article on Microsoft TechCenter forum... obviously you'll need to expand it to meet your needs. - https://social.technet.microsoft.com/Forums/exchange/en-US/99c1f07b-12fa-4e06-95bd-246a757bb00f/powershellscript-to-remove-all-group-memberships-for-one-user

$DGs= Get-DistributionGroup | where { (Get-DistributionGroupMember $_ | foreach {$_.PrimarySmtpAddress}) -contains "user@domain.com"}
 
foreach( $dg in $DGs){
Remove-DistributionGroupMember $dg -Member user@domain.com
}

Based on the comments on the fact the - are not the same you could just open the script in PowerShell ISE or VisualStudio Code and then next to each command as you write it out you can press Tab on your keyboard and it would correct would if the parameters work with that command. But yes I would say based on your last comment

The operation couldn't be performed because object 'rebecca.edge@greensqaureaccord.co.uk' couldn't be found on 'DCWESTBROM01.accord.local'. + CategoryInfo: NotSpecified: (:) [Get-Mailbox], ManagementObjectNotFoundException + FullyQualifiedErrorId: [Server=EXCH2016-01,RequestId=ae3c7f93-e204-4245-aa5f-8678ff68aa63,[FailureCategory=Cmdlet-ManagementObjectNotFoundException] 4EA0476A,Microsoft.Exchange.Management.RecipientTasks.GetMailbox

That the email doesn't exist. I tested the script replacing all the dashes with correct ones (best done in Notepad to avoid formatting) and seems to work without error for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM