Add Usergroup To Local Administrators Group

under Windows PowerShell Gist

 

The follow will prompt to add a given domain user/group to local admins on multiple boxes

<#
######################################################################
# AddToLocalAdmin - Adds the specified $strDomain\$strUser to the local
#                   admins group on $strComputer
######################################################################
#>
function AddToLocalAdmin($strComputer, $strDomain, $strUser)
{
    $computer = [ADSI]("WinNT://" + $strComputer + ",computer")
    $group = $computer.psbase.children.find("Administrators")
    $members= $Group.psbase.invoke("Members") | %{$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
    $members = $members | Where-Object {$_ -eq $strUser}
    if (!$members)
    {
        Write-Host ("{0} - Adding {1}\{2} to the local admins group" -f $strComputer, $strDomain, $strUser)
        $group.Add("WinNT://" + $strDomain + "/" + $strUser)
    }
    else
    {
        Write-Host ("{0} - {1}\{2} is already in local admins group" -f $strComputer, $strDomain, $strUser)
    }
}
$strDomain = Read-Host "Enter Domain"
$strUser = Read-Host "Enter Username or Groupname"
$quit = $false
do
{
    $strComputer = Read-Host "Enter System Name (or press enter to quit)"
    if ($strComputer.length -gt 0)
    {
        AddToLocalAdmin $strComputer $strDomain $strUser
    }
    else
    {
        $quit = $true
    }
}
while ($quit -ne $true)