← script library

Retrieve and Reset User Passwords in Active Directory

Active DirectoryJanuary 6, 2025

Managing user passwords in Active Directory is essential for ensuring security and compliance. This PowerShell script provides administrators with the capability to retrieve user passwords (in a secure manner) and reset them if needed. This can significantly enhance your ability to manage user accounts effectively, especially during security audits and compliance checks. At ServerEngine, we design powerful tools to support your IT management needs.

powershell
Import-Module ActiveDirectory

function Get-ADUserPasswords {
    param (
        [string]$username
    )
    try {
        $user = Get-ADUser -Identity $username -Properties SamAccountName, UserPrincipalName
        Write-Host "User Account Information:"
        Write-Host "SamAccountName: $($user.SamAccountName)"
        Write-Host "UserPrincipalName: $($user.UserPrincipalName)"
        Write-Host "Note: Password cannot be retrieved for security reasons."
    } catch {
        Write-Host "ERROR: User not found. Error: $_"
    }
}

function Reset-ADUserPassword {
    param (
        [string]$username,
        [string]$newPassword
    )
    $securePassword = ConvertTo-SecureString $newPassword -AsPlainText -Force
    try {
        Set-ADAccountPassword -Identity $username -NewPassword $securePassword
        Write-Host "Password for user $username has been successfully reset."
    } catch {
        Write-Host "ERROR: Could not reset password for user $username. Error: $_"
    }
}

Get-ADUserPasswords -username "jdoe"
Reset-ADUserPassword -username "jdoe" -newPassword "NewP@ssword123!"

Run it across your fleet

This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.

Ready when you are.

Try ServerEngine free for 7 days.