Khalifa96
Khalifa96

Reputation: 47

How do I query Last Logon Date via Powershell

I'm trying to query machines which were last logged on to 30 days ago

Clear-Host

$Threshold = (Get-Date).AddDays(-30)

$NotInUse = Get-ADComputer -Filter * -Properties LastLogonDate | Where {
    $_.name -Like "*LN-T48*" -and $_.LastLogonDate -gt $Threshold
}

$NotInUse | select Name, LastLogonDate -Verbose

Upvotes: 1

Views: 478

Answers (1)

Scepticalist
Scepticalist

Reputation: 3923

Use -lt

Using the filter is quicker:

$Threshold = (Get-Date).AddDays(-30)

$NotInUse = Get-ADComputer -Filter  {LastLogonDate -lt $Threshold} -Properties LastLogonDate |
 Where { $_.name -Like "*LN-T48*"}

 $NotInUse | select Name, LastLogonDate -Verbose | sort LastLogonDate

Upvotes: 1

Related Questions