Lance Morrow
Lance Morrow

Reputation: 1

How to use And Or Filters in powershell where-object

I am trying to get a list of properties of AD user objects. Most of the script works except the "where-object" filters are applied. I want to get all users who meet the following: LastLogonDate is more than 75 days ago (this works), enabled (this works) AND either of the following - account expires in the future or never expires. $when is defined properly.

I've tried a number of options and I typically get no output or one or the other in the output.

| Where-Object{($_.lastlogondate -le $When -AND $_.enabled -eq $True) -AND Where-Object($_.AccountExpirationDate -gt (Get-Date) -OR $_.AccountExpirationDate -eq 0)} |

I should see accounts with expiry dates in the future AND accounts that do not expire, but I'm having problems with the "This AND this AND (this OR this)"

Upvotes: 0

Views: 1492

Answers (1)

vrdse
vrdse

Reputation: 3154

The extra Where-Object in your code should throw an error...

    Where-Object {
            $_.lastlogondate -le $When -AND 
            $_.enabled -eq $True -AND 
            ($_.AccountExpirationDate -gt (Get-Date) -OR $_.AccountExpirationDate -eq 0)
    }

This should work if your input is correct.

Upvotes: 1

Related Questions