Posted On March 29, 2019

Add Local Windows User

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

Related Post

HTML: Basic Anatomy of A Page

Modern browsers discern the type of documents being loaded; thus, it is necessary to tag…

PowerShell: Disable Forticlient Web Filtering

# disableForticlientWebfiltering.ps1 # Note: untested script - use as sample as this snippet is incomplete…

Basic CSS: Change the Color of Text

<h2>CatPhotoApp</h2><main> <p>Click here to view more <a href="#">cat photos</a>.</p> <a href="#"><img src="https://bit.ly/fcc-relaxing-cat" alt="A cute orange…
Other project: DragonCoin.com