Add Members to Local Admin group Using PowerShell

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

 

<#Hi Guys, Here's a script i created using ADSI to add users to Local Administrator Groups on a server
Add Members to Local Admin group
Copy the entire following script and run it in an elevated Powershell window.  This will add members to Local admin group on a server.#>

<#---------------------------------------------------------------------------------------------------------#>

function Add-Admin {
[CmdletBinding()]
Param
([Parameter(Mandatory=$true,ValueFromPipeline=$true,HelpMessage="UserName to be added to local Admin Group")]
$usernames
,[Parameter(Mandatory=$true,ValueFromPipeline=$true,HelpMessage="Domain in which the UserName exists")]
[string[]]
$domain
)
foreach ($username in $usernames) {
$strComputer="localHost"
$computer = [ADSI]("WinNT://" + $strComputer + ",computer")
$Group = $computer.psbase.children.find("administrators")
$Group.Add("WinNT://" + $domain + "/" + $username)

}
"Members in Local "+$Group.name+" Group"
$Group.psbase.invoke("Members") | %{$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
}

Below command can be run to invoke the function written above

Add-Admin  -usernames <Specify the Admin DL's separated by comma> -domain <Specify Domain name>

Share this post

Post Comment