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

CSS: How To Set Color Gradient and Animation to Text and Background

// Static color gradient .colorGradientClass { background-color: #ffffff !important; background-image: linear-gradient(315deg, #ffffff 0%, #d9d9d9 74%)…

PowerShell: Find Duplicate RDP Sessions on All Remote Desktop Servers Discovered Within the Domain

There's this module called 'PSTerminalServices' that has a method to collect active sessions on all…

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