Strange behaviour

I tried this in powershell 5 and it works ok:

$days = (get-date).adddays(-90)
Get-aduser -filter {lastlogondate -lt $days} -properties name, lastlogondate

But it doesn’t work in powershell core 7.1. It fails with error:

Get-Aduser: variable: "days" found in expression: $days is not defined

What’s wrong?

Welcome to the forums.

Would you please format your code as code here on the forum?
Here you can read how that works: Guide to Posting Code.

You can go back and edit your existing post. You don’t have to create a new one. :wink:

Thanks in advance.

That’s not a helpful description. What exactly happens? Do you get any errors? If yes - please post them here completely - formatted as code as well.

$days = (get-date).adddays(-90) ; Get-aduser -filter {lastlogondate -lt $days} -properties name, lastlogondate

Works perfect on my workstaion and my DC

What powershell versions are on your workstation and DC?

Both are Powershell 7.1.3
and in WindowsPowershell 5.1.19041.610 it works, too

??? How does it “not work” for you?

It raises an error listed above in my original post in version 7.1.2. Works well in version 5.1

This error

Get-Aduser: variable: "days" found in expression: $days is not defined

Is this the complete code you run?

Yes, it is. Variable definition and cmdlet

Than I don’t have an idea. The code works for me as well with no problems.
I tested it from Windows PowerShell 5.1 and PowerShell 7.1.3 from my local machine.

Ok, i got the answer here powershell - Get-Aduser -Filter will not accept a variable - Stack Overflow.
So, this one works as supposed to me:

Get-aduser -filter "lastlogondate -lt '$days'" -properties name, lastlogondate

Very strange. As mentioned above the code works for me just as you posted it in your question. :man_shrugging:

Anyway thanks for sharing the solution. :+1: