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

PowerShell: Stream Reader

Have you ran into scripting situations where opening a file would result in locking from…

PowerShell: Obtain Domain Admin Credential

function obtainDomainAdminCredentials{ # Legacy domain binding function function isValidCred($u,$p){ # Get current domain using logged-on…

Basic HTML and HTML5: Turn an Image into a Link

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