In this scenario, I will create Temporary folder to put the result ( in csv file). First, Create folder in C Drive and rename as ‘Temp’ and open Powershell (ss Administrator)
To Specify inactivity range value below :
Command :
$DaysInactive = 180 [Enter]
To $time variable converts $DaysInactive to LastLogonTimeStamp property format for the -Filter switch to work :
Command :
$time = (Get-Date).Adddays(-($DaysInactive)) [Enter]
To Identify and collect inactive computer accounts:
Command :
Get-ADComputer -Filter {LastLogonTimeStamp -lt $time} -ResultPageSize 2000 -resultSetSize $null -Properties Name, OperatingSystem, SamAccountName, DistinguishedName, LastLogonDate| Export-CSV “C:\Temp\StaleComps.CSV” –NoTypeInformation [Enter]
Example as below :

And the result, will get in csv file.

![]()
