Laeeq Qazi's Exchange Server Blog

Exchange Server blog

How to remove all group memberships for one exchange mailbox user

There is often a need to remove a mailbox user membership from all exchange distribution groups.

Here is a powershell script for this purpose.

e.g your user’s primary smtp address is user@domain.com, then this script will remove the membership of this user from each mail enabled exchange distribution groups:

########### Script Start #####################

$DGs= Get-DistributionGroup | where { (Get-DistributionGroupMember $_ | foreach {$_.PrimarySmtpAddress}) -contains “user@domain.com” }

foreach( $dg in $DGs){

Remove-DistributionGroupMember $dg -Member user@domain.com
}

########### Script End #####################

Note: Put above lines of code in a text file and save it as .ps1 file and then execute it in EMS or powershell console.

If you save this file in Exchange Scripts folder “C:\Program Files\Microsoft\Exchange Server\Scripts” OR “C:\Program Files\Microsoft\Exchange Server\V14\Scripts” then, in EMS, you will just need to enter the .ps1 file name and then press enter.
You might also need to set the powershell execution policy as RemoteSigned in EMS:

set-executionPolicy RemoteSigned

Regards,
Laeeq Qazi

Advertisements

June 18, 2010 Posted by | Exchange 2007, Exchange 2010 | | Leave a comment