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.
More in Active Directory
Active Directory User Onboarding with PowerShell
2026-08-17Active Directory User Offboarding with PowerShell
2026-08-17Enable an Active Directory User with PowerShell
2026-08-17Unlock an Active Directory Account with PowerShell
2026-08-17Reset an Active Directory Password with PowerShell
2026-08-17Move AD Users to an OU by Department with PowerShell
2026-08-17Ready when you are.
Try ServerEngine free for 7 days.