Add Local Windows User



# Dynamic Credential
$who = whoami
if ($who.substring($who.length-2, 2) -eq "-admin"){$username=$who;}
else {$username=$who+"-admin";}
$password = Read-Host -Prompt "Input the password for account $username" -AsSecureString
#$password=convertto-securestring "PASSWORD" -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username,$password



#$servers="SERVER01","SERVER02"
$servers=Read-Host -Prompt "Please paste the server Name(s) here"
$newUser="testAdmin"
$newUserPass='PASSWORD'
$newUserName="Test Admin"
$newUserDesc="Account to Be Deleted After Migration."
$newUserGroup="Administrators"



foreach ($server in $servers) {

Invoke-command -Credential $cred -ComputerName $server -ScriptBlock {
$person=$Args[0]
"Invoked from $person and Running as: "+ (whoami)+" on:"+(hostname)

# Using legacy commands for maximum compatibility
NET USER $Args[1] $Args[2] /ADD /Y
NET LOCALGROUP $Args[5] $Args[1] /ADD /Y
# These lines only work in PowerShell 5.1
#New-LocalUser $Args[1] -Password $Args[2] -FullName $Args[3] -Description $Args[4]
#Add-LocalGroupMember -Group $Args[5] -Member $Args[1]
} -Args $who,$newUser,$newUserPass,$newUserName,$newUserDesc,$newUserGroup
}

Leave a Reply

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