Onsite or Remote Support
Call (519) 573-3759
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