← script library
Find and Delete Expired Active Directory User Accounts
Active DirectoryJanuary 5, 2025
In Active Directory management, keeping user accounts up to date is crucial for security and efficiency. This PowerShell script helps administrators identify and delete user accounts that have expired, enhancing directory hygiene. At ServerEngine, we provide tools to streamline your server management.
powershell
Import-Module ActiveDirectory
function Remove-ExpiredADUsers {
$expiredUsers = Get-ADUser -Filter { AccountExpirationDate -lt (Get-Date) } -Properties AccountExpirationDate
if ($expiredUsers) {
foreach ($user in $expiredUsers) {
try {
Remove-ADUser -Identity $user SamAccountName -Confirm:$false
Write-Host "Deleted expired user: $($user.SamAccountName)"
} catch {
Write-Host "ERROR: Could not delete user $($user.SamAccountName). Error: $_"
}
}
} else {
Write-Host "No expired accounts found."
}
}
Remove-ExpiredADUsersRun 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-17Disable an Active Directory User 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-17Ready when you are.
Try ServerEngine free for 7 days.