Home > Back-end >  Can I parse Date using -Filter parameter in the ActiveDirectory Powershell module?
Can I parse Date using -Filter parameter in the ActiveDirectory Powershell module?

Time:02-19

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

CodePudding user response:

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.

CodePudding user response:

This value is stored in a weird format. It resembles unix epoch time, but isn't quite the same. See here:

https://social.technet.microsoft.com/wiki/contents/articles/12814.active-directory-lastlogontimestamp-conversion.aspx

  • Related