Find computers in On prem AD that have been inactive for X days, sort my last login date, and display OU its in.
Search-ADAccount -ComputersOnly -AccountInactive -TimeSpan "90" | Select-Object -Property *| Where-Object {$_.Enabled -eq $true -and $_.LastLogonDate -ne $null} | Select-Object Name, LastLogonDate, @{Name="OU"; Expression={$_.DistinguishedName -replace '^CN=.+?(?<!\\),'}} | Sort-Object LastLogonDate,DistinguishedName| Format-TableThis leverages the Search-ADAccount cmdlet which can help with search for AD objects rather than building traditional search queryies and filters. With the following cparamaters it an be limited to Only computer objects which are active for a defined timepsan.
-ComputersOnly- will only search Computer objects