Remove groups from a user with an exception
Hey,
I am trying to remove a number of users from all of their group memberships, with the exception of one group. I’ve been working on this for an hour or so and hit a stumbling block:
$username=”User01″
$groupdntoexclude=(get-adgroup “group 1”).distuishedname
get-aduser -identity $username -properties MemberOf | where-object -ne $groupdntoexclude | for-eachObject { $_.MemberOf | remove-adgroupmember -members $username -confirm:$true}
So far all I seem to be able to do is remove the user for every group. I’ve tried playing around with the Where-Object conditions but can’t quite get it right. Any suggestions would be greatly appreciated!
Thanks,
Matt
Hey, I am trying to remove a number of users from all of their group memberships, with the exception of one group. I’ve been working on this for an hour or so and hit a stumbling block: $username=”User01″
$groupdntoexclude=(get-adgroup “group 1”).distuishedname
get-aduser -identity $username -properties MemberOf | where-object -ne $groupdntoexclude | for-eachObject { $_.MemberOf | remove-adgroupmember -members $username -confirm:$true} So far all I seem to be able to do is remove the user for every group. I’ve tried playing around with the Where-Object conditions but can’t quite get it right. Any suggestions would be greatly appreciated! Thanks,Matt Read More