← script library

PowerShell Script to Audit User Account Security

Security & AuditingJanuary 13, 2025

This PowerShell script audits user accounts in Active Directory to identify accounts that may pose security risks. It checks for accounts that are disabled, expired, or have never been logged into, allowing administrators to take appropriate actions.

powershell
Import-Module ActiveDirectory

$users = Get-ADUser -Filter * -Properties Enabled, LastLogonDate, AccountExpirationDate

$problematicUsers = $users | Where-Object {
    ($_.Enabled -eq $false) -or
    ($_.AccountExpirationDate -lt (Get-Date)) -or
    ($_.LastLogonDate -eq $null)
}

$problematicUsers | Select-Object Name, Enabled, LastLogonDate, AccountExpirationDate | Format-Table -AutoSize

$problematicUsers | Export-Csv -Path 'C:\UserAuditReport.csv' -NoTypeInformation

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.