Back to all recipes
Level: JuniorLast updated:
Find computers that have not logged on for 90 days
$cutoff = (Get-Date).AddDays(-90)
Search-ADAccount -ComputersOnly -AccountInactive -TimeSpan 90.00:00:00 |
Where-Object { $_.LastLogonDate -lt $cutoff } |
Select-Object Name,LastLogonDate,DistinguishedName |
Export-Csv stale-computers.csv -NoTypeInformationWhy it works this way
Stale computer accounts are needless attack surface and inventory noise.
Command breakdown
Caution
Never delete straight away. Step 1: export the list. Step 2: disable and move to a separate OU. Delete only after 30 quiet days.
Watch out
- Do not delete straight away: disable and park in a separate OU for a month.
- Temporarily powered-off servers show up here too — verify first.