How to create local user accounts by PowerShell on remote computers

108 Views Asked by At

I am trying to create a simple script that prompts a local admin user to create a local user account on 6 remote computers in a Windows Workgroup. The script should prompt for the name of the user, password, and the group to which it will be a member of, and repeats the execution of the commands in all 6 computers while the script initiated only once. All 6 computers have the admin account with the same username/password.

I know the PS commands that can be used are - New-LocalUser, Add-LocalGroupMember and PSSession, but I just can’t make it automated enough to run with the minimal input by a local admin.

Thanks.

I tried the following script and saved it on PC01 to run from Powershell. However it gives an error that account already exists whenever it tries to create the account on PC02 and PC03 after it successfully creates the account on PC01. Also, it does not Read host when remoting in to PCs.

Enter-PSSession -ComputerName PC01 
New-LocalUser 
Add-LocalGroupMember -Group Users 
Exit-PSSession 

Enter-PSSession -ComputerName PC02 
New-LocalUser 
Add-LocalGroupMember -Group Users 
Exit-PSSession

Enter-PSSession -ComputerName PC03 
New-LocalUser 
Add-LocalGroupMember -Group Users 
Exit-PSSession

1

There are 1 best solutions below

0
Re1ter On

This is because your script is trying to create a user on your PC, not on a remote one. Try to use:

$cred = Get-Credential
Invoke-Command -ComputerName PC01,PC02,PC03,PC04,PC05,PC06 -Credential $cred -ScriptBlock {New-LocalUser -Name “John Doe” -Password (ConvertTo-SecureString -AsPlainText “Password” -Force);Add-LocalGroupMember -Group "Users" -Member "John Doe"}

In this example, when prompted, you need to enter your login/password once to access the remote PC and edit the name/password in the ScriptBlock.