Hello @Waqkas-Ahmed
Maybe something like this? Uncomment the appropriate section at the end of the script depending if you want to set the password to something you know, or have the user change the password at next logon.
$ErrorActionPreference = 'Stop'
$VerbosePreference = 'Continue'
#User to reset password for
$UserName = Read-Host "Enter username"
$UserAccount = $null
Try {
Write-Verbose "Retrieving $UserName"
$UserAccount = Get-LocalUser $UserName
Write-Verbose "User $UserName found"
}
Catch [Microsoft.PowerShell.Commands.UserNotFoundException] {
"User $UserName was not found" | Write-Warning
}
Catch {
"An unspecified error occured" | Write-Error
Exit
}
If ($UserAccount) {
# To force a local user to reset password on next logon
# net user $UserName /logonpasswordchg:yes
# Write-Verbose "User will have to change password at next logon"
# To reset a local users password to a known value
# $NewPassword = Read-Host "Enter new password" -AsSecureString
# $UserAccount | Set-LocalUser -Password $NewPassword
# Write-Verbose "Password for $UserName was reset"
}
Mike Rodrick
Edutainer, ITProTV
**if the post above has answered the question, please mark the topic as solved.