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

About steve

I currently reside in Columbus, Ohio and have been an IT Professional since 1996. My background is in Enterprise Networking, specializing in: Virtualizaion – VMware ESX-ESXi Clusters, Hyper-V Clusters Microsoft Enterprise Networking – Active Directory, Exchange Server, Internet Information Server, SharePoint Server, Lync 2010 Server, and SQL Server Linux – Redhat – Fedora – CentOS, Debian – Ubuntu, Unix – BSD and Slackware Network Services – Switching, Routing, VPN, IP Telephony and other Communications Technologies Network Servers – Dell, HP, and IBM Network Storage – Dell StorageVault, Equilogic, EMC, IBM, and other SAN Technologies
This entry was posted in Microsoft. Bookmark the permalink.