← script library

Finding Inactive Active Directory Accounts

Active DirectoryJanuary 5, 2025

Maintaining an up-to-date Active Directory is crucial for ensuring both security and efficiency. In this post, we will provide a PowerShell script that helps you identify inactive user accounts in Active Directory. By locating accounts that havent been used for a specified number of days, you can take appropriate actions, such as disabling or deleting these accounts to improve security. At ServerEngine, we offer robust tools that enhance server management.

powershell
Import-Module ActiveDirectory

function Find-InactiveADAccounts {
    param (
        [int]$DaysInactive
    )
    $dateThreshold = (Get-Date).AddDays(-$DaysInactive)
    $inactiveUsers = Get-ADUser -Filter { LastLogonDate -lt $dateThreshold } -Properties LastLogonDate | Select-Object Name, LastLogonDate, SamAccountName
    if ($inactiveUsers) {
        $inactiveUsers | Format-Table -AutoSize
    } else {
        Write-Host "No inactive user accounts found for the last $DaysInactive days."
    }
}

Find-InactiveADAccounts -DaysInactive 90

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.