How to Find Inactive Computers in Active Directory Using PowerShell

How to Find Inactive Computers in Active Directory Using PowerShell.

To identify inactive computer accounts, you will always target those that have not logged on to Active Directory in the last last 90 days. To accomplish this goal, you need to target the LastLogonTimeStamp property and then specify a condition with the time as shown in the following PowerShell commands:

$DaysInactive = 90

$time = (Get-Date).Adddays(-($DaysInactive))

Get-ADComputer -Filter {LastLogonTimeStamp -lt $time} -ResultPageSize 2000 -resultSetSize $null -Properties Name, OperatingSystem, SamAccountName, DistinguishedName

If you wish to search computer accounts that have been inactive for more than 90 days, all you need to do is modify the $DaysInActive variable value. The current value is set at 90 days; however, you can specify your own value.

To export output to a CSV file, add the Export-CSV PowerShell cmdlet as shown in the following command:

Get-ADComputer -Filter {LastLogonTimeStamp -lt $time} -ResultPageSize 2000 -resultSetSize $null -Properties Name, OperatingSystem, SamAccountName, DistinguishedName | Export-CSV “C:\Temp\StaleComps.CSV” –NoTypeInformation
You might also like

Leave a Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More