Powershell – Add user to local administrators group


Add domain user/group

To see current server name:

$env:COMPUTERNAME

To connect to current server Adminstrators group, type:

$srvgroup = [ADSI]("WinNT://"+$env:COMPUTERNAME+"/administrators, group")

To check server group connected you can query for is name:

$srvgroup.name

To add an Active Directory user or group (example AD name is MYAD), type:

$srvgroup.add("WinNT://MYAD/userorgroupname,user")

To check if new users or group were added correctly you can query group members:

$srvgroup.Invoke("Members") | foreach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}

 

In Depth
http://blogs.technet.com/b/heyscriptingguy/archive/2008/03/11/how-can-i-use-windows-powershell-to-add-a-domain-user-to-a-local-group.aspx
http://www.danielclasson.com/powershell-script-to-add-domain-computers-groups-or-user-accounts-to-the-local-administrator-group/

Tested With
PowerShell 4.0

Advertisements
Categories: Powershell | Leave a comment

Post navigation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: