Get-ADPrincipalGroupMembership
returns only groups, leading Remove-ADPrincipalGroupMembership
to auto-fill -Identity
with the group name. You'll have to re-use the user object in -Identity
.
Because of the first issue, Remove-ADPrincipalGroupMembership
doesn't accept multiple groups from the pipeline. It should normally, but the [ADGroup]
objects returned by Get-ADPrincipalGroupMembership
seem to trip it up. To fix, use a ForEach
loop, or use a two-step process:
# two steps:
$groups = Get-ADPrincipalGroupMembership $person
Remove-ADPrincipalGroupMembership -Identity $person -MemberOf $groups -WhatIf
# OR foreach loop:
Get-ADPrincipalGroupMembership $person |
Foreach {
Remove-ADPrincipalGroupMembership -Identity $person -MemberOf $_
}
Note that you can't remove an AD user's primary group (usually 'Domain Users'), so you may want to add a filter:
$groups = Get-ADPrincipalGroupMembership $person |
Where Name -notlike 'Domain Users'
Remove-ADPrincipalGroupMembership -Identity $person -MemberOf $groups