How to add users and groups to the local groups on Windows System using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

To add users to the local groups using PowerShell, we need to use the Add-LocalGroupMember command (Module − Microsoft.PowerShell.LocalAccounts).

Add-LocalGroupMember -Group "Administrators" -Member
"NewLocalUser","labdomain\Alpha","Labdomain\ITSecurity"

The above command adds 2 users (NewLocalUser (Local) and Alpha (Domain)) and one Domain Security Group ITSecurity to the Local Administrators group.

You can also use the other local group name instead of Administrators.

To add the new users in the local group on the remote system(s) use the Invoke-Command method. For example,

Invoke-Command -ComputerName Test1-Win2k12, Test1-Win2k16{
   Add-LocalGroupMember -Group "Administrators" -Member
   "NewLocalUser","labdomain\Alpha","Labdomain\ITSecurity"
}

Please note − To run the above command, the remote server must use the PS version 5.1 or advance version.

You can also use the command prompt if you don’t have the PS version 5.1 or higher to add users to the group using the below command syntax.

net localgroup groupname username /add

For example,

net localgroup Administrators "labdomain\alpha" /add

In the above example, we are adding LabDomain user Alpha to the local Administrators group. You can replace the Administrators with the other group name. For the local user/group, we just need to provide the username or group name without specifying the domain. For example,

net localgroup Administrators "NewLocalUser" /add

To add the above commands remotely,

Invoke-Command -ComputerName Test1-Win2k12, Test1-Win2k16 -ScriptBlock{
   Net Localgroup Administrators "LabDomain\Alpha" /add
}
raja
Published on 02-Nov-2020 10:45:03
Advertisements