← script library

Unlock an Active Directory Account with PowerShell

Active DirectoryAugust 17, 2026

Unlocks one account that lockout policy has locked after failed logons, reporting when there was nothing to unlock rather than pretending to have done something. The single most delegated task in the library — pair it with the failed logon audit to find out why the account keeps locking.

powershell
# Unlock an AD User Account
# Single use case: unlock one user locked out by failed logon attempts
#-----------------------------------------------------------------

function Write-Log {
    param($Message)
    Write-Host "<WRITE-LOG = `"*$Message*`">"
}

# --- Parameters (replace via ServerEngine API parameters if needed) ---
$SamAccountName = "j.doe"

if (-not (Get-Module -ListAvailable -Name ActiveDirectory)) {
    Write-Log "ActiveDirectory module not available on this host!"
    return
}
Import-Module ActiveDirectory

$user = Get-ADUser -Filter "SamAccountName -eq '$SamAccountName'" -Properties LockedOut, LockoutTime -ErrorAction SilentlyContinue
if ($null -eq $user) {
    Write-Log "User not found: $SamAccountName"
    return
}

if (-not $user.LockedOut) {
    Write-Log "User $SamAccountName is not locked out - nothing to do."
    return
}

Unlock-ADAccount -Identity $user.DistinguishedName
$user = Get-ADUser -Identity $user.DistinguishedName -Properties LockedOut

if (-not $user.LockedOut) {
    Write-Log "User $SamAccountName unlocked successfully."
} else {
    Write-Log "WARNING: User $SamAccountName is still locked out!"
}

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.