Quick & Efficient Removal of Users From Distro Groups - Exchange Online Environment

Hi,

I am new to Exchange online and learning as I use it. One thing I have noticed is that Microsoft does not have any type of function to "Remove / Remove All" when looking at the Exchange properties of a user account and then under "Member Of" for distro groups. Now I have a Powershell script that allows me to do so but I am having difficulty running in. I've seen it used before and it was a much easier / quicker method to remove a user from all distro groups (which is what I need to do).

As for the easiest method (delete the mailbox / user account in Exchange), that's not an option for me even though these accounts are not in use anymore. Here is the code, maybe I have something wrong.

$user = $args[0]
if (!$args[0]) {

}
$mailbox=get-mailbox $user

$dgs= Get-DistributionGroup
 
foreach($dg in $dgs){
    
    $DGMs = Get-DistributionGroupMember -identity $dg.Identity
    foreach ($dgm in $DGMs){
        if ($dgm.name -eq $mailbox.name){
       
            write-host 'User Found In Group' $dg.identity
              Remove-DistributionGroupMember $dg.Name -Member $user
        }
    }
}


May 29th, 2015 4:28pm

That looks like it should work fine. You getting any errors? Also try this, a little different but basically the same thing

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


You can also try putting your question in the O365 forum

http://community.office365.com/en-us/f/158.aspx

Free Windows Admin Tool Kit Click here and download it now
May 29th, 2015 5:24pm

Hi,

Heres an similar thread about your question, however this script is related to On-premise Exchange server, for your reference:
https://social.technet.microsoft.com/Forums/exchange/en-US/c03c0c1d-a1a0-46a9-9b41-8fa2c446ba8f/how-to-remove-disabled-users-from-all-distribution-groups-error?forum=ITCG

For Office 365, please refer to Office 365 Forum as DJ mentioned. Thank you for your cooperation.

Thanks

June 1st, 2015 4:18am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics