Posted On February 7, 2020

PowerShell: Add Accounts into Local Administrators Group

kimconnect 0 comments
blog.KimConnect.com >> Codes , Windows >> 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 *

Related Post

How to Move C:\Users to Another Volume

Warning: this process will effectively re-route all file pointers from C:\Users\* to another volume. Windows…

PowerShell: Add Root Domain to Trusted Sites

Windows Servers often have 'hard admin' modes, where accesses to external websites are painstakingly restricted.…

Excel Challenge: Update Records with a New Email Domain if It Matches Deprecated Domain

PrimaryEmail (Cell B2) EmailUsername Domain [email protected] =LEFT(B2,FIND("@",B2)-1) =RIGHT(B2,LEN(B2)-FIND("@",B2)) FixedPrimaryEmail =IF(D2="olddomain.org",C2&"@newdomain.org",B2)
Other project: DragonCoin.com