# Scripts to create users on Windows devices

Some corporate devices might be used by more than one employee. As a result, an administrator might have to create separate user accounts on each device. Here, we discuss one of the easiest ways to create users remotely. Hexnode lets admins [deploy scripts](https://www.hexnode.com/mobile-device-management/help/executing-custom-scripts-for-windows/) to get the job done.

 Note: 
**Supported Versions**:

The scripts given below will be supported on the following versions:

- Windows 10 v1607+ (Pro, Enterprise, Education)
- Windows 11 (Pro, Enterprise, Education)

 

 Disclaimer:The sample scripts provided below are adapted from third-party open-source sites.

 

Batch script
------------

### Create user

Batch script to create users on Windows

net user "username" "password" /add 

   1

  net user "username" "password" /add 

   

 

  

E.g., To create a user Josh,

`Net user "Josh" "Josh123" /add`

On deploying the above script, a new user account with username `Josh` will be added to the device, and the user can log in to the account using the password `Josh123`.

To create a user account with no password, do not provide any value for **password** in the above script.

E.g., `Net user "Josh" "" /add`

### Change username

You can change the name of the created user account by running the script given below.

Batch script to change username on Windows

@echo off REM === Batch script to rename a local user account === REM Run this script as Administrator set "OldUser=old user name" set "NewUser=new user name" REM Call PowerShell to perform the rename powershell -Command "Rename-LocalUser -Name '%OldUser%' -NewName '%NewUser%'" REM If the PowerShell command succeeds, return success exit code if %errorlevel%==0 ( echo User account renamed from %OldUser% to %NewUser%. exit /b 0 ) else ( echo Failed to rename user account %OldUser%. exit /b 1 )

   1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

  @echo off

REM === Batch script to rename a local user account ===

REM Run this script as Administrator

set "OldUser=old user name"

set "NewUser=new user name"

REM Call PowerShell to perform the rename

powershell -Command "Rename-LocalUser -Name '%OldUser%' -NewName '%NewUser%'"

REM If the PowerShell command succeeds, return success exit code

if %errorlevel%==0 (

 echo User account renamed from %OldUser% to %NewUser%.

 exit /b 0

) else (

 echo Failed to rename user account %OldUser%.

 exit /b 1

)

   

 

  

*set “OldUser=old user name”*

*set “NewUser=new user name”*

In the above command, replace the terms “old user name” and “new user name” with the actual names.

[![Script to change username remotely executed successfully from Hexnode.](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2021/09/Username-changed-remotely-with-scripts-300x148.png "Username changed remotely with scripts")](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2021/09/Username-changed-remotely-with-scripts-300x148.png)

PowerShell script
-----------------

### Create user

To create a user, you can run the script given below.

PowerShell script to create users on Windows

\# Run this script with Administrator privileges param( \[string\]$UserName = "username", \[string\]$PasswordPlain = "password" ) try { # Convert plain text password to SecureString $Password = ConvertTo-SecureString $PasswordPlain -AsPlainText -Force # Create the new local user New-LocalUser -Name $UserName -Password $Password # Enable the account Enable-LocalUser -Name $UserName # Add to Users group so it can log in Add-LocalGroupMember -Group "Users" -Member $UserName Write-Host "Local user account '$UserName' created and enabled successfully." exit 0 # Explicit success code for Hexnode } catch { Write-Host "Failed to create local user account '$UserName'. Error: $\_" exit 1 # Explicit failure code for Hexnode }

   1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

  \# Run this script with Administrator privileges

param(

 \[string\]$UserName = "username",

 \[string\]$PasswordPlain = "password"

)

try {

 \# Convert plain text password to SecureString

 $Password = ConvertTo-SecureString $PasswordPlain -AsPlainText -Force

 \# Create the new local user

 New-LocalUser -Name $UserName -Password $Password

 \# Enable the account

 Enable-LocalUser -Name $UserName

 \# Add to Users group so it can log in

 Add-LocalGroupMember -Group "Users" -Member $UserName

 Write-Host "Local user account '$UserName' created and enabled successfully."

 exit 0 \# Explicit success code for Hexnode

}

catch {

 Write-Host "Failed to create local user account '$UserName'. Error: $\_"

 exit 1 \# Explicit failure code for Hexnode

}

   

 

  

*\[string\]$UserName = “username”,*

*\[string\]$PasswordPlain = “password”*

In the above command, replace the terms “username” and “password” with the actual names. For example, to create a user “josh” with password “josh123”, then the command should be:

*\[string\]$UserName = “josh”,*

*\[string\]$PasswordPlain = “josh123”*

[![Script to create user in Windows returns the output in the Action History tab of Hexnode.](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2021/09/Script-to-create-users-in-Windows-to-add-user-accounts-securely-portal-output-300x148.png "Script to create users in Windows to add user accounts securely-portal output")](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2021/09/Script-to-create-users-in-Windows-to-add-user-accounts-securely-portal-output-300x148.png)

### Change username

You can change the name of the created user account by running the script given below.

PowerShell script to change username on Windows

Rename-LocalUser -Name "Administrator" -NewName "New Name" 

   1

  Rename-LocalUser -Name "Administrator" -NewName "New Name" 

   

 

  

Replace ‘Administrator’ with the current name of the user and ‘NewName’ with the new name of the user, respectively.

 Notes:- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.