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: SMB Shares Migration

$object='\\fileserver\sharename' $identity='domain\account' $permissions='Full' function importNtfsSecurity{ try{ set-executionpolicy unrestricted -force # Include the required NTFSSecurity library…

How To Modify Collectors of Windows Exporter

Update: here's a generalized version of this function that can be applied to other services…

Java Virtual Machine Optimal Memory Tuning

Overview: There are five available garbage collectors (GC) for Java Virtual Machines (JVM). Here are…