PowerShell: Find Inactive Computer Accounts in AD

Please refer to the following PowerShell to find the inactive Computers for x numbers of days  in Active Directory Domain Controller

</p>
#Inactive Computer Objects for past 60 days
Import-Module ActiveDirectory
$Computers=Get-ADComputer -Filter * -Properties SamAccountName,PasswordLastSet | Select SamAccountName, PasswordLastSet
$DaysInActive = 60
$ReportPath = "c:\temp\Computers.csv" #Report Path
$Count=0
foreach ($Computer in $Computers) {

if ($Computer.PasswordLastSet -lt (Get-Date).AddDays(-($DaysInActive))){
write-host $Computer.SamAccountNAme "had not login for past" $DaysInActive "days"
$Count++
$TotalComputers +=@($Computer) #Insert all the objects into array
}

}
$TotalComputers | export-csv $ReportPath -NoTypeInformation
Write-Host $Count "computers had not login for past" $DaysInActive "days"

With the export csv file, you can do some filtering and sorting in Excel to get the following report

PowerShell-FindInActiveComputers-01.jpg

Share This