Remove only DL from user memberof

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Wes Stahler Wes Stahler 6 months, 2 weeks ago.

  • Author
    Posts
  • #40261
    Profile photo of nidhin ck
    nidhin ck
    Participant

    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)
    	}
    }
    
  • #40338
    Profile photo of Wes Stahler
    Wes Stahler
    Participant

    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)
            }		
        }
    }
    

You must be logged in to reply to this topic.