Author Posts

May 25, 2016 at 9:09 pm

Hi Experts, Could you please help with a script to remove only Distribution groups from a user member of.

I use below code to remove entire user membership. but here we just need to remove DL's

$users = Get-Content c:\users.txt
foreach ($u in $users){
	$User = [ADSI]"LDAP://$u"
	ForEach ($GroupDN In $User.memberOf){
  		$Group = [ADSI]("LDAP://" + $GroupDN)
  		$Group.Remove($User.ADsPath)
	}
}

May 26, 2016 at 12:58 pm

Other factors come into play here (i.e. Dynamic Distribution Lists)
Following your construct, you could add a check for the GroupType.
Something like....

$users = Get-Content c:\users.txt
foreach ($u in $users){
    $User = [ADSI]"LDAP://$u"
	ForEach ($GroupDN In $User.memberOf){
  	    $Group = [ADSI]("LDAP://" + $GroupDN)
        if (($group.groupType).ToString() -in @(2,4,8)) {
            $Group.Remove($User.ADsPath)
        }		
    }
}