From time to time you might want to clean up your Active Directory by moving or removing inactive Computer Objects. But why should you do this? On the one hand, you do not keep unnecessary inactive objects in your Active Directory. On the other hand, you can also save licenses if you also delete these inactive computers. Let’s say you are also using System Center Configuration Manager (SCCM), which can also be cleaned in this action.

This can be fulfilled easily by using a very short but effective PowerShell Inactive Computers script. Simply define the Organizational Unit (OU) you want to use as a SearchBase, specfiy the days of inactivity and that’s it!
The script will automatically subtract the amount of inactive days from the current date/time and will search the defined OU with the Get-ADComputer command. It will use the attribute LastLogonTimeStamp which you can also check in the Attribute Editor of the GUI.

$TargetOU = "OU=<SubUnit>,OU=<MainUnit>,DC=<domainName>,DC=com"
$DaysInactive = 90
$time = (Get-Date).Adddays(-($DaysInactive))
Get-ADComputer -SearchBase $TargetOU -Filter {LastLogonTimeStamp -lt $time} | Select-Object Name

Of course you could improve this simple PowerShell Inactive Computers script by adapting the output to a CSV file. Or by selecting more attributes. I would recommend you to create a scheduled task for the script and extend it by sending a HTML email/report, so you do not forget to clean up your inactive clients.

You can check out all options of the Get-ADComputer command at Microsoft Docs

Previous articleSharePoint Online Management Shell: Restore A Deleted Site or OneDrive storage
Next articleAnalyze the Performance of your Website with


Please enter your comment!
Please enter your name here