← script library

Disable an Active Directory User with PowerShell

Active DirectoryAugust 17, 2026

Disables one account by sAMAccountName, checks first whether it is already disabled, and reads the state back afterwards to prove the change landed. Written to be safe under repetition, which is what you want from the script somebody runs at 2am during an incident.

powershell
# Disable an AD User Account
# Single use case: disable one user account (offboarding, security incident)
#-----------------------------------------------------------------

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 Enabled -ErrorAction SilentlyContinue
if ($null -eq $user) {
    Write-Log "User not found: $SamAccountName"
    return
}

if (-not $user.Enabled) {
    Write-Log "User $SamAccountName is already disabled."
    return
}

Disable-ADAccount -Identity $user.DistinguishedName
$user = Get-ADUser -Identity $user.DistinguishedName -Properties Enabled

if (-not $user.Enabled) {
    Write-Log "User $SamAccountName disabled successfully."
} else {
    Write-Log "WARNING: User $SamAccountName is still enabled!"
}

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.