PowerShell – Find and remove old computer accounts from Active Directory

Want to find all the old computer accounts and remove them quickly? Weeding through Active Directory in a medium or large environment can be time consuming, and very boring!

If you are migrating AD, or upgrading AD, it is best to do this first.

 

PowerShell command to find old computers:

get-adcomputer -properties lastLogonDate -filter * | where { $_.lastLogonDate -lt (get-date).addmonths(-12) } | sort Name | FT Name,LastLogonDate

 

PowerShell command to delete old computers:

get-adcomputer -properties lastLogonDate -filter * | where { $_.lastLogonDate -lt (get-date).addmonths(-12) } | Remove-ADComputer -whatif