Find Stale Computer Accounts in Active Directory with PowerShell, and Disable Them Before You Delete
List computer accounts that have not logged on for 90 days, record them, disable and quarantine them in stages, and keep a clear way back.

Underlined words are defined in the KBY Lexicon. Tap or click one to see what it means.
To find stale computer accounts in Active Directory, run Search-ADAccount -ComputersOnly -AccountInactive -TimeSpan 90.00:00:00 against a named OU, enrich the result with Get-ADComputer, and save it to a CSV. Do not delete anything from that list. Disable each account, move it to a quarantine OU, wait out a holding period, and make deletion a separate, approved change. The key constraint is that the logon date Active Directory keeps is deliberately approximate, so a 90-day threshold is a starting point for a human check, not proof that a machine is gone.
#Why this matters before you touch anything
Computer accounts pile up after rebuilds, hardware refreshes and cancelled projects. Each one is an object that still authenticates if someone powers the machine on, and an entry that clutters inventory, licensing and group membership reviews. Removing them is worthwhile housekeeping. Removing the wrong one takes a working server or a laptop that has been on long leave off the domain.
The temptation is to pipe the inactive list straight into a delete command. That turns an estimate into an irreversible action. This guide treats the list as a set of candidates, stages the change so each step can be undone, and stops short of deletion until a person signs it off. It sits alongside the other housekeeping tasks in this toolkit, such as finding expiring certificates on Windows servers with PowerShell.
Assumptions to check first: you run the ActiveDirectory module from a management host, you can read computer objects in the target OUs, and you hold write rights only on those OUs and the quarantine OU. Start in a lab or a single test OU, not the whole domain.
#The moving parts behind the logon date
The attribute behind LastLogonDate is lastLogonTimestamp. Microsoft documents that when an account logs on, the value is read from the domain controller, and it is only updated if it is older than the current time minus msDS-LogonTimeSyncInterval. The same page says the initial update after raising the domain functional level is calculated as 14 days minus a random percentage of 5 days. In other words, the value is replicated lazily on purpose, so it can lag the real last logon by days.
Two consequences follow. First, any threshold shorter than that interval will flag machines that are in daily use, so choose a threshold well above it. Read the interval configured in your own domain rather than assuming the default. Second, the date only tells you when the account last authenticated to the domain. A device that works offline, a server that runs a job once a quarter, or a laptop on a long absence can look dormant and still be wanted.
The cmdlet also has a documented limit: -AccountInactive depends on that attribute, so it only works when the domain is at Windows Server 2003 domain functional level or higher. Every supported domain meets that, but the note explains why an unusually old lab domain returns nothing.
A second signal helps. Get-ADComputer can return PasswordLastSet when you ask for it with -Properties. An account with both an old logon date and an old password date is a stronger candidate than one with only the first. Treat that as a judgement of ours, not a Microsoft rule, and still confirm against your asset records.
#Building the candidate list
The workflow has five stages, and only the last needs a separate approval.
Rendering diagram...
#Step by step: find, record, disable and quarantine

#Find the candidates
Set a search base so the command can only see the OU you intend. -SearchBase and -ComputersOnly are documented parameters of Search-ADAccount, and -TimeSpan sets the period.
1$base = 'OU=Workstations,DC=corp,DC=example,DC=com'
2$span = New-TimeSpan -Days 90
3$candidates = Search-ADAccount -ComputersOnly -AccountInactive -TimeSpan $span -SearchBase $base
4$candidates | Measure-ObjectThe OU in the example is a placeholder. Replace it with your own distinguished name and keep the value in a variable so it appears once.
#Enrich and save the evidence
Look each account up with Get-ADComputer, requesting the properties you want to review, and export the result. Export-Csv writes objects as rows, so select the columns first and do not format the objects before exporting.
1$report = $candidates | ForEach-Object {
2 Get-ADComputer -Identity $_.DistinguishedName -Properties LastLogonDate, PasswordLastSet, OperatingSystem, Enabled
3} | Select-Object Name, DistinguishedName, Enabled, LastLogonDate, PasswordLastSet, OperatingSystem
4$report | Export-Csv -Path .\stale-computers-before.csv -NoTypeInformation
5$report | Format-Table Name, LastLogonDate, PasswordLastSet, Enabled -AutoSizeThe CSV is your rollback record. It holds each account’s original distinguished name, which you need to move it back. Store it somewhere the change record can reference, and do not edit it.
#Review against inventory
Compare the report with the sources that know whether a device still exists: the asset register, your endpoint management console and the people who own the machines. Remove from the list any account that is still wanted. This is the step that protects you from the approximate logon date, and it cannot be automated away.
#Disable with a preview first
Disable-ADAccount supports -WhatIf, which shows what would happen without running it. Use it before every real run.
1$approved = Import-Csv -Path .\stale-computers-approved.csv
2$approved | ForEach-Object { Disable-ADAccount -Identity $_.DistinguishedName -WhatIf }When the preview lists only the accounts you expect, run it again without -WhatIf. Stop at once if the count differs from the approved file.
#Move to a quarantine OU
A dedicated OU keeps disabled accounts visible and separate. Move-ADObject needs -TargetPath, and it also supports -WhatIf.
1$quarantine = 'OU=Quarantine-Stale,DC=corp,DC=example,DC=com'
2$approved | ForEach-Object { Move-ADObject -Identity $_.DistinguishedName -TargetPath $quarantine -WhatIf }Run it for real once the preview is correct. After the move, each account’s distinguished name changes, which is why the original is in the CSV. Moving an account out of its original OU may also change which Group Policy applies to it, so create the quarantine OU with that in mind.

#Permissions and the limits of this approach
Run this with an account that has only the rights it needs: read on the target OUs, and the rights to disable and move computer objects there and in the quarantine OU. Avoid running it as a domain-wide administrator. Scheduling it needs a credential, and the guide on keeping passwords out of scheduled PowerShell scripts with SecretManagement and SecretStore covers storing one properly. Use a scheduled job only for the read-only report. Keep the disable and move steps interactive, with a person approving the file.
Keep the export private. It lists host names, operating systems and distinguished names, which are useful to an attacker mapping a network.
#What goes wrong
- The list includes machines that are still in use. The logon date is replicated lazily, or the machine rarely joins the network. Lengthen the threshold, and always cross-check against inventory before disabling.
- The list is empty or short. Check the domain functional level, the search base and the threshold. The documented parameter only works at Windows Server 2003 domain functional level or higher.
- A user reports that a laptop cannot sign in to the domain after the holding period starts. The account was disabled. A disabled computer account cannot be used as a normal one until it is enabled, so re-enable it and move it back from the CSV record, then confirm it is genuinely in use before trying again.
- The move fails. Check your rights on the quarantine OU and that the account is not protected from accidental deletion in a way your tooling does not handle. Read the error text rather than retrying in a loop.
- The CSV opens with odd columns. Formatted objects were exported. Select properties with
Select-ObjectbeforeExport-Csv, not afterFormat-Table.
#Confirm the right accounts are quarantined
Success means the right accounts, and only those, are disabled and in the quarantine OU, and nothing that mattered has broken. Check it directly:
- Count the accounts in the quarantine OU and compare with the approved file.
- List them with
Get-ADComputer -Filter * -SearchBase $quarantine -Properties Enabledand confirmEnabledis false for every one. - Run the original
Search-ADAccountquery again. The moved accounts no longer appear under the original OU. - Watch your service desk and monitoring for the holding period. Any report that a machine has stopped authenticating is a reason to restore that account, not to wait.
#Backing out, and what to do about deletion
Reversing the change is two commands per account, driven by the CSV. Enable-ADAccount enables an account, and Move-ADObject returns it to its original OU.
1$approved | ForEach-Object {
2 $ou = ($_.DistinguishedName -split ',', 2)[1]
3 $current = 'CN=' + $_.Name + ',' + $quarantine
4 Enable-ADAccount -Identity $current
5 Move-ADObject -Identity $current -TargetPath $ou
6}Test that block on one account before relying on it. Pass -WhatIf to each cmdlet the first time.
Deleting is deliberately not part of the scripted flow. Remove-ADComputer removes the object, and it supports -WhatIf and -Confirm, but removal is the one step you cannot simply reverse by flipping a setting. Make it its own change, with its own approval, after the holding period has passed with no complaints. Before that change, check whether the Active Directory Recycle Bin is on. Microsoft documents that it requires a Windows Server 2008 R2 forest functional level and Enterprise Admins membership to enable, that it cannot be disabled once enabled, and that it increases the size of the database on every domain controller. With it on, Restore-ADObject restores a deleted object to its last known parent unless you give another -TargetPath, and Get-ADObject -IncludeDeletedObjects finds it. Without it, a deleted computer account must be rejoined to the domain, not restored. Whether to enable it is a forest-wide decision for the people who own the forest.
#Where to go next
Run the report on a schedule, keep the before and after CSVs with the change record, and review the quarantine OU monthly. Once the OU is stable, agree a standing holding period with the owners of the estate, and write it down so each cycle follows the same rules. For version-specific detail on any cmdlet, check the vendor page for your module version.
About this page
Credited to
Dates
Published
Sources
Corrections
No corrections are listed for this page. Some review sweeps corrected pages without naming each one; those are in the corrections log.
Recommended reading
More from KBY that goes with this article.
Small business ITSPF, DKIM and DMARC: the three DNS records that make it harder to send email as your business
8 min read

Discover more
Graduate Learning
Ops Playbook
Lexicon Definitions
Was this useful?
Reports go to the site owner and are used to correct pages. Corrections are listed at /corrections.
Engineering insights, direct to you.
Receive the latest Systems Engineering tutorials, production guides, Engineering Labs and operational best practices.
Comments
Add a thoughtful note on Find Stale Computer Accounts in Active Directory with PowerShell, and Disable Them Before You Delete. Comments are checked for spam and held for moderation before appearing.