Powershell – How to Remove user from all groups except domain users

First step – get groups that domain user is in…

Get-ADPrincipalGroupMembership -Identity  user | Format-Table -Property name

Second step – remove member from all groups accept domain users

$ADgroups = Get-ADPrincipalGroupMembership -Identity  user | where {$_.Name -ne “Domain Users”}
Remove-ADPrincipalGroupMembership -Identity  user -MemberOf $ADgroups -Confirm:$false