How to remove a member from the local group using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

To remove a member from the local group using PowerShell, we can use the RemoveLocalGroupMember command. This command is available in the module Microsoft.PowerShell.LocalAccounts in and above PowerShell version 5.1.

To use this command, we need to provide two parameter values. One is the -Group (Local Group Name) and the second is -Member (Name of the Member to remove). For example,

Remove-LocalGroupMember -Group Administrators -Member TestUser

The above command will remove TestUser from the local group Administrators.

To use the above command on the remote computer, we need to use Invoke-Command. For example,

Invoke-Command -ComputerName Test1-Win2k12,Test1-Win2k16 -ScriptBlock{
   Remove-LocalGroupMember -Group "Administrators" -Member "LabDomain\Alpha"
}

The above command will remove LabDomain\Alpha user from the local Administrators group on remote computers.

If you don’t have the LocalAccounts module available or the PowerShell version is below 5.1 then you can use the cmd command as shown below.

net localgroup Administrators labdomain\alpha /delete

In the above example, labdomain\alpha will be removed from the local group Administrators using cmd command. You can execute this command remotely using Invoke-Command method,

Invoke-Command -ComputerName Test1-Win2k12,Test1-Win2k16 -ScriptBlock{
   net localgroup Administrators labdomain\alpha /delete
}


raja
Published on 02-Nov-2020 15:19:43
Advertisements