If you want to run the Get-ADComputer Powershell cmdlet on a domain workstation, you have to download and install Active Directory Administrative Center (ADAC) or the ActiveDirectory PowerShell module on that machine.
- Open the PowerShell ISE → Run the following PowerShell commands, adjusting the value of the $DaysInactive variable to suit your needs (the sample script below will search for and collect all computers that have not logged in for the last 90 days):
# Specify inactivity range value below
$DaysInactive = 90
# $time variable converts $DaysInactive to LastLogonTimeStamp property format for the -Filter switch to work$time = (Get-Date).Adddays(-($DaysInactive))
# Identify inactive computer accounts
Get-ADComputer -Filter {LastLogonTimeStamp -lt $time} -ResultPageSize 2000 -resultSetSize $null -Properties Name, OperatingSystem, SamAccountName, DistinguishedName, LastLogonDate
- To export the list of stale computer accounts to a CSV file, add the Export-CSV PowerShell cmdlet, as shown in this updatedPowerShell script:
# Specify inactivity range value below
$DaysInactive = 90
# $time variable converts $DaysInactive to LastLogonTimeStamp property format for the -Filter switch to work
$time = (Get-Date).Adddays(-($DaysInactive))
# Identify and collect inactive computer accounts:
Get-ADComputer -Filter {LastLogonTimeStamp -lt $time} -ResultPageSize 2000 -resultSetSize $null -Properties Name, OperatingSystem, SamAccountName, DistinguishedName, LastLogonDate| Export-CSV “C:\Temp\StaleComps.CSV” –NoTypeInformation
- Open the file created by the script in Microsoft Excel:
- Run Netwrix Auditor → Navigate to “Reports” → Expand the “Active Directory” section → Go to “Active Directory – State-in-Time” → Select “Computer Accounts – Last Logon Time” → Click “View”.
- If you want to limit the list by period of inactivity (for instance, to list only computers with a last logon 30 days ago or longer), adjust the “Inactive Days” parameter and switch “Status” to “Enabled” → Click “View Report”.
- To save the report, click the "Export" button → Choose a format from the dropdown menu → Click “Save”.