← script library
Report Inactive Active Directory Users with PowerShell
Active DirectoryAugust 17, 2026
Lists enabled accounts with no logon inside the configured number of days, ignoring accounts created inside that same window so new starters are not reported as dormant. The result is sorted oldest first and handed to the next runbook step as a comma separated list, ready for a bulk disable.
powershell
# Report Inactive AD Users
# Single use case: list enabled users with no logon for N days
#-----------------------------------------------------------------
function Write-Log {
param($Message)
Write-Host "<WRITE-LOG = `"*$Message*`">"
}
# --- Parameters (replace via ServerEngine API parameters if needed) ---
$InactiveDays = 90
if (-not (Get-Module -ListAvailable -Name ActiveDirectory)) {
Write-Log "ActiveDirectory module not available on this host!"
return
}
Import-Module ActiveDirectory
$cutoff = (Get-Date).AddDays(-[int]$InactiveDays)
Write-Log "Searching enabled users with no logon since $($cutoff.ToString('yyyy-MM-dd'))..."
$users = Get-ADUser -Filter { Enabled -eq $true } -Properties LastLogonDate, whenCreated |
Where-Object { ($_.LastLogonDate -lt $cutoff) -and ($_.whenCreated -lt $cutoff) } |
Sort-Object LastLogonDate
if (-not $users) {
Write-Log "No inactive users found - all enabled accounts logged on within $InactiveDays days."
return
}
Write-Log "================ Inactive Users ($($users.Count)) ================"
foreach ($u in $users) {
$last = if ($u.LastLogonDate) { $u.LastLogonDate.ToString('yyyy-MM-dd') } else { "never" }
Write-Log "LastLogon: [$last] $($u.SamAccountName) ($($u.Name))"
}
Write-Log "Inactive user report complete: $($users.Count) account(s) exceeded $InactiveDays days."
# Pass the list to the next runbook script (comma separated)
$store = ($users.SamAccountName -join ",")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-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.