How to add domain users to the Local Groups using Powershell

In this Article, we will see how to add user or users or Computer using Powershell.

If you want to add users to the specific one Group, you can use the below command..
$domain=”Windowstechpro.com”
$Computer = $env:COMPUTERNAME;
$ADSIComputer = [ADSI](“WinNT://$Computer,computer”);
$SGName = $ADSIComputer.psbase.children.find(‘Administrators’)
function Groupmember($number)
{
$SGName.add(“WinNT://”+$domain+”/”+$number )
}

Groupmember “radhakrishnan.govindan” 

Groupmember “veterivel.Madeshwaran”

 In the above commands, two users are getting added to the Local Group : Administrators

Now, let see you have requirement to add the users two or many groups, How?

Simple, in the above command, just add the groups in the Psbase,

$domain=”Windowstechpro.com”
$Computer = $env:COMPUTERNAME;
$ADSIComputer = [ADSI](“WinNT://$Computer,computer”);
$SGName = $ADSIComputer.psbase.children.find(‘Administrators’ , ‘Domain Users’) 
function Groupmember($number)
{
$SGName.add(“WinNT://”+$domain+”/”+$number )
}

Groupmember “radhakrishnan.govindan” 

Groupmember “veterivel.Madeshwaran”

In case, if you want to add two or more users, just added each user simply in the Groupmember  ” username” in the each line.

The able command remains same if in case you want to add computer accounts. Computer Objects can be added followed with $ Symbol as Computer objects are always ends with Computername$

Groupmember  “Computername$” in the above one instead of user accounts.

It can be saved as PS1 and executed in multiple machines if it is required.

Author Bio

Radhakrishnan Govindan

Leave a Reply

Your email address will not be published. Required fields are marked *