Question:
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.
1 2 3 4 5 6 |
$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?
Answer:
Pipe the groups into Remove-ADGroupMember
in a ForEach-Object
loop:
1 2 3 4 |
Get-ADUser -Identity $User1 -Properties MemberOf | ForEach-Object { $_.MemberOf | Remove-ADGroupMember -Members $_.DistinguishedName -Confirm:$false } |