Loading..
  • Lets work together

    Onsite or Remote Support

  • M-F 9:00 am - 5:00 pm

    Call (519) 573-3759

How to Find Inactive Computers in Active Directory with PowerShell- NATIVE AUDITING

Posted ByTeam Lead

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