# Script to change password of a user account on Windows devices

Users forgetting their device password is a common scenario for an IT admin. Hexnode lets admins change the password of users by [deploying scripts](https://www.hexnode.com/mobile-device-management/help/executing-custom-scripts-for-windows/).

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

 

Batch Script
------------

Batch script to change password of a user account on Windows

@echo off REM Configuration set USERNAME=Username set PASSWORD=Password echo Resetting password for: %USERNAME% net user "%USERNAME%" "%PASSWORD%" if %errorlevel%==0 ( echo SUCCESS: Password reset for %USERNAME%) else ( echo ERROR: Failed to reset password - Code %errorlevel% )

   1

2

3

4

5

6

7

8

9

10

11

  @echo off 

REM Configuration 

set USERNAME=Username 

set PASSWORD=Password 

echo Resetting password for: %USERNAME%

net user "%USERNAME%" "%PASSWORD%"

if %errorlevel%==0 (

echo SUCCESS: Password reset for %USERNAME%)

else (

echo ERROR: Failed to reset password - Code %errorlevel%

)

   

 

  

This Batch script resets the password for a specific user. Replace `USERNAME` with the username of the required user and `PASSWORD` with the new password.

The Hexnode UEM output console displays whether the password reset has succeeded or failed.

PowerShell Script
-----------------

PowerShell script to change password of a user account on Windows

\# Mandatory Configurations $USERNAME = "Username" $NEW\_PASSWORD = "NewPassword" #Optional Configurations $FORCE\_PASSWORD\_CHANGE = $true $ENABLE\_ACCOUNT = $false Write-Host "Password Reset Script - $USERNAME on $env:COMPUTERNAME" Write-Host "" # Check Admin if (-not (\[Security.Principal.WindowsPrincipal\]\[Security.Principal.WindowsIdentity\]::GetCurrent()).IsInRole(\[Security.Principal.WindowsBuiltInRole\]::Administrator)) { Write-Host "ERROR: Requires Administrator privileges" exit 1 } try { # Convert password $Password = ConvertTo-SecureString $NEW\_PASSWORD -AsPlainText -Force # Get and update user $User = Get-LocalUser -Name $USERNAME -ErrorAction Stop Write-Host "Found user: $USERNAME" # Reset password $User | Set-LocalUser -Password $Password -ErrorAction Stop Write-Host "SUCCESS: Password reset completed" # Optional: Force password change if ($FORCE\_PASSWORD\_CHANGE) { net user $USERNAME /logonpasswordchg:yes | Out-Null Write-Host "User must change password at next logon" } # Optional: Enable account if ($ENABLE\_ACCOUNT -and -not $User.Enabled) { Enable-LocalUser -Name $USERNAME -ErrorAction Stop Write-Host "Account enabled" } } catch { if ($\_.Exception.GetType().Name -like "\*UserNotFound\*") { Write-Host "ERROR: User '$USERNAME' not found" Write-Host "Available users:" Get-LocalUser | Select-Object Name, Enabled | Format-Table -AutoSize } else { Write-Host "ERROR: $($\_.Exception.Message)" } exit 1 } Write-Host "Done"

   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

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

  \# Mandatory Configurations 

$USERNAME = "Username"

$NEW\_PASSWORD = "NewPassword"

\#Optional Configurations 

$FORCE\_PASSWORD\_CHANGE = $true

$ENABLE\_ACCOUNT = $false

Write-Host "Password Reset Script - $USERNAME on $env:COMPUTERNAME"

Write-Host ""

\# Check Admin 

if (-not (\[Security.Principal.WindowsPrincipal\]\[Security.Principal.WindowsIdentity\]::GetCurrent()).IsInRole(\[Security.Principal.WindowsBuiltInRole\]::Administrator)) {

Write-Host "ERROR: Requires Administrator privileges"

exit 1

}

try {

\# Convert password 

$Password = ConvertTo-SecureString $NEW\_PASSWORD -AsPlainText -Force

\# Get and update user 

$User = Get-LocalUser -Name $USERNAME -ErrorAction Stop 

Write-Host "Found user: $USERNAME"

\# Reset password 

$User | Set-LocalUser -Password $Password -ErrorAction Stop 

Write-Host "SUCCESS: Password reset completed"

\# Optional: Force password change 

if ($FORCE\_PASSWORD\_CHANGE) {

net user $USERNAME /logonpasswordchg:yes | Out-Null

Write-Host "User must change password at next logon"

}

\# Optional: Enable account 

if ($ENABLE\_ACCOUNT -and -not $User.Enabled) {

Enable-LocalUser -Name $USERNAME -ErrorAction Stop 

Write-Host "Account enabled"

}

} catch {

if ($\_.Exception.GetType().Name -like "\*UserNotFound\*") {

Write-Host "ERROR: User '$USERNAME' not found"

Write-Host "Available users:"

Get-LocalUser | Select-Object Name, Enabled | Format-Table -AutoSize

} else {

Write-Host "ERROR: $($\_.Exception.Message)"

}

exit 1

}

Write-Host "Done"

   

 

  

The script given above is a configurable PowerShell script that can change the password for any specific user on a device. Mandatory configurations in the script include replacing the `“Username”` with the Username of the user whose password needs to be changed and the `“NewPassword”` with the required password.

 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.