Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to remove user from all groups in an Active Directory using PowerShell script

I'm trying to use a PowerShell script to accept input from the user based on what user they want removed from all groups. Is my syntax wrong? Here's what I have so far.

$User1 = Read-Host -Prompt 'Enter the username of the employee you wish to change'

Get-ADUser -Identity $User1 -Properties memberof |
    Select-Object -ExpandProperty memberof |
    Remove-ADGroupMember -Identity CISCOVPN, FS-001

Where CISCOVPN and FS-001 are two of the groups I want $User1 removed from. Is there a way to just say remove from all groups?

like image 894
DanC12 Avatar asked Sep 19 '25 12:09

DanC12


1 Answers

Pipe the groups into Remove-ADGroupMember in a ForEach-Object loop:

Get-ADUser -Identity $User1 -Properties MemberOf | ForEach-Object {
  $_.MemberOf | Remove-ADGroupMember -Members $_.DistinguishedName -Confirm:$false
}
like image 84
Ansgar Wiechers Avatar answered Sep 22 '25 05:09

Ansgar Wiechers