In this post, we will share a useful PowerShell script that audits user accounts for security compliance. This script will help system administrators identify inactive user accounts and report them for further action. This is an important step in maintaining a secure environment by ensuring that only active accounts have access to critical resources.
powershell
$logFile = "C:\UserAccountAudit.log"
$inactiveDaysThreshold = 90
$currentDate = Get-Date
$users = Get-ADUser -Filter * -Property LastLogonDate
$inactiveUsers = $users | Where-Object {
($currentDate - $_.LastLogonDate).Days -gt $inactiveDaysThreshold
}
if ($inactiveUsers) {
$inactiveUsers | ForEach-Object {
Add-Content -Path $logFile -Value "$($_.Name) is inactive since $($_.LastLogonDate)"
}
} else {
Add-Content -Path $logFile -Value "No inactive accounts found."
}
Get-Content -Path $logFile | Write-HostRun 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 Security & Auditing
Enable the Windows Firewall with PowerShell
2026-08-17Check Windows Firewall Status with PowerShell
2026-08-17Disable SMBv1 with PowerShell
2026-08-17Enforce TLS 1.2 in the Registry with PowerShell
2026-08-17Enable RDP Network Level Authentication with PowerShell
2026-08-17Audit the Local Administrators Group with PowerShell
2026-08-17Ready when you are.
Try ServerEngine free for 7 days.