PowerShell: Add Accounts into Local Administrators Group

# addAccountToLocalAdmins.ps1
# Requires: PowerShell Version 4.0+

$accountToAdd='ServerAdmins'
$groupName="Administrators"
$servers=@(
"CONCU1",
'CONCU2',
'CONCU100',
'CONCU80665',
'CONCU6547354',
'CONWHAT989',
'CONCU3'
)

$servers|%{
$session=new-pssession $_
invoke-command -session $session -scriptblock{
param($principleName,$groupName)
write-host "Adding $principleName into $groupName";
Add-LocalGroupMember -Group $groupName -Member $principleName;
write-host "$env:computername group $groupName now has these members:`r`n";
get-localgroupmember $groupName|ft -autosize
} -args $accountToAdd,$groupName
remove-pssession $session
}

sample output

ObjectClass Name                          PrincipalSource
----------- ---- ---------------
User SERVERX\ServerAdmins Local
User SERVERX\Johnito Local

Leave a Reply

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