Category filter
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.
Batch Script
|
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
|
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.