'Can I parse Date using -Filter parameter in the ActiveDirectory Powershell module?
I would like to return all machines where the LastLogonDate attribute exceeds 180 days. I would like to approach this using the -filter parameter. Currently, I tried
$lastLogonRestriction = (Get-date).AddDays(-180)
Get-ADComputer -Filter "LastLogonDate -lt $lastLogonRestriction" -Properties * | Select LastLogonDate, Name
But I get an error stating it can't parse it
Get-ADComputer : Error parsing query: 'LastLogonDate -lt 08/22/2021 12:02:23' Error Message: 'Operator Not supported: ' at position: '21'.
At C:\Users\Admin.MH\Desktop\powershell\AD\moveDisabledComputers.ps1:8 char:1
+ Get-ADComputer -Filter "LastLogonDate -lt $lastLogonRestriction" -Pro
Sure, I can pipe my output into a where-object and search for this but this -filter param would be better
Solution 1:[1]
You probably just need a single quote around the variable:
Get-ADComputer -Filter "LastLogonDate -lt '$lastLogonRestriction'" -Properties * | Select LastLogonDate, Name
But keep in mind that a computer's last logon date is the last time the computer authenticated to the domain, not the last time someone logged into the computer. If you're looking for the last time someone logged into the computer, read this answer.
Solution 2:[2]
This value is stored in a weird format. It resembles unix epoch time, but isn't quite the same. See here:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gabriel Luci |
| Solution 2 | Joel Coehoorn |
