Skip to content
Menu

Disable Stale AD Users and Computers and Move Them to a Holding OU

Disables and moves the accounts get-stale-ad-objects lists, saving their original location first. Nothing is deleted. High risk: needs DryRun=false and Approve=disable-stale-ad-objects.

Disable stale AD users and computers and move them to a holding OU is free for members.

Enter your email to create your free account and get the file.

We'll email you the script and the weekly MSP Outpost email. Unsubscribe anytime. Privacy policy.

Already a member? Sign in.

Be the first to review Disable stale AD users and computers and move them to a holding OU. Tell other MSPs how it worked for you.

Write a Review

LanguagePowerShell
Runs onWindows Server 2016+ (domain controller or domain member); runs as SYSTEM
NeedsPowerShell 5.1, admin rights, ActiveDirectory module (RSAT-AD-PowerShell), rights to disable and move AD accounts
Changes thingsChanges what it runs on

What It Does

This script changes nothing until you set DryRun=false and Approve=disable-stale-ad-objects. It refuses to touch more accounts than MaxChanges, 25 by default, in one run. First run get-stale-ad-objects, or this script as a dry run, read the list, then run it for real.

It applies the same selection rules as get-stale-ad-objects: enabled users and computers whose lastLogonTimestamp is older than InactiveDays, or that never signed in and were created more than InactiveDays ago. For each one it writes Disabled by MSP Outpost, the date and the days idle in front of the description, keeping the old description after it, moves the account to TargetOu, and then disables it. The disable is last on purpose: a failed move leaves the account enabled. If the move succeeds but disabling fails, check that the new location is in scope before rerunning with SearchBase.

Nothing is deleted. The account keeps its SID and its group memberships, so enabling it and moving it back restores it. Before any change it saves a CSV of every account's original location, description and last logon in the MSPOutpost backup folder. A real run then reads each account back and ends in an error naming any that are not disabled, moved and stamped.

Never touched, whatever the inputs: the built-in accounts, anything marked as a critical system object, domain controllers, the computer running the script, managed service accounts, trust accounts, and every account named in ExcludeAccounts. TargetOu is required, must already exist as an OU or container, and is never created. InactiveDays 0 is refused unless SearchBase limits the run. A second run finds nothing and says Already clean.

Before You Run It

  • Run get-stale-ad-objects first and read every account on it.
  • Put every service account, and anything that only signs in by Kerberos, in ExcludeAccounts first.
  • Create the holding OU first. The script never creates it.
  • Run it as a dry run and check the count against MaxChanges before you approve it.
  • Use an InactiveDays of 30 or more. lastLogonTimestamp can be up to 14 days behind.
  • To undo, re-enable the account and restore its location and description from the saved CSV.

Type and Risk

  • Type: Action. It makes its change, checks the change worked and says what changed. It exits 0, or 2 when it could not do the job.
  • Risk: High. It is a dry run unless you set both DryRun=false and Approve=disable-stale-ad-objects, and it stops before making more than 25 changes in one run unless you raise MaxChanges. Run it once as a dry run and read the list first.
  • What it changes: Disables stale user and computer accounts, moves them to TargetOu and stamps their description; saves their original location first.
  • Restart: It never restarts the machine.
  • Typical run: Under 1 minute.
  • Example answer: "Disabled and moved 6 stale account(s) (4 user(s), 2 computer(s)) to OU=Disabled Accounts,DC=contoso,DC=com"
  • 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.

  • TargetOu: Where disabled accounts go, as a distinguished name, e.g. "OU=Disabled Accounts,DC=contoso,DC=com". Required. It must exist.
  • InactiveDays: Days without a sign-in before an account is stale. Whole number, 0 to 3650. Default 90. 0 needs SearchBase.
  • SearchBase: Only look under this OU or container (distinguished name). Default empty: the whole domain. It must exist.
  • ExcludeAccounts: Sign-in names (SamAccountName) never to touch, separated by commas or semicolons, e.g. "svc-backup, svc-sql, PRINTSRV01". * and ? are wildcards with 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.

The Script

disable-stale-ad-objects.ps1 · 981 lines · 50.8 KB

Free for Members

Create a free account or sign in to read, copy and download this script.

Welcome Back

Sign in to your MSP Outpost account.