What It Does
The script is read-only. It lists enabled user and computer accounts that look abandoned, one row per account, so you can review them before disabling anything. disable-stale-ad-objects uses the same selection. An account is stale when its lastLogonTimestamp is older than InactiveDays (Reason Inactive), or when it has no lastLogonTimestamp at all and was created more than InactiveDays ago (Reason NeverLoggedOn). Accounts that are already disabled are not listed.
lastLogonTimestamp is refreshed only when it is more than about 14 days old, so use an InactiveDays of 30 or more for anything you will act on. The default is 90.
Never listed, whatever the inputs: the built-in accounts (Administrator, Guest, krbtgt and DefaultAccount), anything Active Directory marks as a critical system object, domain controllers, the computer running the script, managed service accounts, trust accounts, and every account named in ExcludeAccounts. Put your service accounts in ExcludeAccounts: the script cannot tell a service account that only authenticates by Kerberos from an abandoned one.
Run it on a domain controller or a member server with the ActiveDirectory module (RSAT). If the module is missing it ends ERROR; on a computer that is not in a domain it ends SKIPPED.
Before You Run It
- Use an InactiveDays of 30 or more. A shorter number can list accounts that signed in recently.
- Put every service account in ExcludeAccounts before you act on the list.
- Check the list with the client. A person on leave looks idle too.
- Run this first, then run disable-stale-ad-objects as a dry run, before you disable anything.
Type and Risk
- Type: Report. It collects facts into a table and never alerts. It exits 0, or 2 when it could not run.
- Risk: Low. It reads and changes nothing.
- Restart: It never restarts the machine.
- Typical run: Under 1 minute for 1,000 accounts.
- Example answer: "Found 14 stale account(s) idle 90+ days: 9 user(s), 5 computer(s)"
- Tested before listing on a Windows Server 2025 domain controller (VM) with Windows PowerShell 5.1, run as SYSTEM, against its own test cases.
Inputs
Pass an input as an argument (-Name value), as an environment variable with the same name, which is how most RMMs pass script variables, or change its default in the param() block at the top of the script. An argument wins over an environment variable, which wins over the default.
- InactiveDays: Days without a sign-in before an account is stale. Whole number, 0 to 3650. Default 90. (0 lists every account that has not signed in this moment; meant for testing, with SearchBase.)
- SearchBase: Only look under this OU or container, as a distinguished name, e.g. "OU=Staff,DC=contoso,DC=com". Default empty: the whole domain. It must exist.
- ExcludeAccounts: Sign-in names (SamAccountName) to leave out, separated by commas or semicolons, e.g. "svc-backup, svc-sql, PRINTSRV01". A computer may be written with or without its trailing $. * and ? are wildcards, but a wildcard needs at least 3 literal characters. Default empty.
Every script also takes DryRun, OutputFormat (Text, Answer, Json or Csv), ExportPath and ResultField, which writes the answer to an RMM custom field.
License
Provided "as is" under the MIT License, without warranty of any kind. Test before production use. MSP Outpost and User First Digital Inc. accept no liability for any damage or loss.