# Strange behaviour

**URL:** <https://forums.powershell.org/t/strange-behaviour/16120>\
**Category:** PowerShell Help\
**Created:** [April 1, 2021, 9:52am UTC](https://forums.powershell.org/t/strange-behaviour/16120 "2021-04-01T09:52:11Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Me5166](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@Me5166](https://forums.powershell.org/u/Me5166)\
**Post date:** [April 1, 2021, 9:52am UTC](https://forums.powershell.org/t/strange-behaviour/16120/1 "2021-04-01T09:52:11Z")

</div>

I tried this in powershell 5 and it works ok:

```auto
$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:

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

```

What’s wrong?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 1, 2021, 10:06am UTC](https://forums.powershell.org/t/strange-behaviour/16120/2 "2021-04-01T10:06:04Z")

</div>

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.

> [@Guide to Posting Code - Redux](https://forums.powershell.org/t/guide-to-posting-code-redux/15797):
>
> Discourse uses [Markdown](https://www.markdownguide.org/getting-started/) for text formatting. There is a reference sheet and quick tutorial here: [Markdown Reference](https://commonmark.org/help/) You can produce in-line code formatting by wrapping your code in backticks (`). For example, `Get-ChildItem` produces Get-ChildItem. For larger code blocks, place a line containing three backticks above and below your code. For example ``` # 256-Color Foreground & Background Charts echo “`n$esc[1;4m256-Color Foreground & Background Charts$esc[0m” foreach ($fgbg in 38,48) { #…

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

Thanks in advance.

> [@Me5166](#):
>
> But it doesn’t work in powershell core 7.1.

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.

---

<div class="post-metadata">

**Author:** ![chrism\_cologne](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chrism\_cologne](https://forums.powershell.org/u/chrism_cologne)\
**Post date:** [April 1, 2021, 11:31am UTC](https://forums.powershell.org/t/strange-behaviour/16120/3 "2021-04-01T11:31:35Z")

</div>

> [@Me5166](#):
>
> `Get-aduser -filter {lastlogondate -lt $days} -properties name, lastlogondate`

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

Works perfect on my workstaion and my DC

---

<div class="post-metadata">

**Author:** ![Me5166](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@Me5166](https://forums.powershell.org/u/Me5166)\
**Post date:** [April 1, 2021, 11:35am UTC](https://forums.powershell.org/t/strange-behaviour/16120/4 "2021-04-01T11:35:33Z")

</div>

What powershell versions are on your workstation and DC?

---

<div class="post-metadata">

**Author:** ![chrism\_cologne](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chrism\_cologne](https://forums.powershell.org/u/chrism_cologne)\
**Post date:** [April 1, 2021, 11:40am UTC](https://forums.powershell.org/t/strange-behaviour/16120/5 "2021-04-01T11:40:16Z")

</div>

> [@Me5166](#):
>
> t it doesn’t work in powershell core 7.1. It fails w

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

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 1, 2021, 11:42am UTC](https://forums.powershell.org/t/strange-behaviour/16120/6 "2021-04-01T11:42:36Z")

</div>

> [@Olaf](#):
>
> What exactly happens? Do you get any errors?

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

---

<div class="post-metadata">

**Author:** ![Me5166](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@Me5166](https://forums.powershell.org/u/Me5166)\
**Post date:** [April 1, 2021, 11:45am UTC](https://forums.powershell.org/t/strange-behaviour/16120/7 "2021-04-01T11:45:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Me5166](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@Me5166](https://forums.powershell.org/u/Me5166)\
**Post date:** [April 1, 2021, 11:46am UTC](https://forums.powershell.org/t/strange-behaviour/16120/8 "2021-04-01T11:46:00Z")

</div>

This error

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

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 1, 2021, 11:46am UTC](https://forums.powershell.org/t/strange-behaviour/16120/9 "2021-04-01T11:46:55Z")

</div>

Is this the complete code you run?

---

<div class="post-metadata">

**Author:** ![Me5166](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@Me5166](https://forums.powershell.org/u/Me5166)\
**Post date:** [April 1, 2021, 11:48am UTC](https://forums.powershell.org/t/strange-behaviour/16120/10 "2021-04-01T11:48:10Z")

</div>

Yes, it is. Variable definition and cmdlet

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 1, 2021, 11:50am UTC](https://forums.powershell.org/t/strange-behaviour/16120/11 "2021-04-01T11:50:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Me5166](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@Me5166](https://forums.powershell.org/u/Me5166)\
**Post date:** [April 1, 2021, 1:32pm UTC](https://forums.powershell.org/t/strange-behaviour/16120/12 "2021-04-01T13:32:28Z")

</div>

Ok, i got the answer here [powershell - Get-Aduser -Filter will not accept a variable - Stack Overflow](https://stackoverflow.com/questions/20075502/get-aduser-filter-will-not-accept-a-variable).  
So, this one works as supposed to me:

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

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 1, 2021, 1:37pm UTC](https://forums.powershell.org/t/strange-behaviour/16120/13 "2021-04-01T13:37:43Z")

</div>

> [@Me5166](#):
>
> Ok, i got the answer here [powershell - Get-Aduser -Filter will not accept a variable - Stack Overflow](https://stackoverflow.com/questions/20075502/get-aduser-filter-will-not-accept-a-variable).  
> So, this one works as supposed to me:

Very strange. As mentioned above the code works for me just as you posted it in your question. 🤷‍♂️

Anyway thanks for sharing the solution. 👍

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:27pm UTC](https://forums.powershell.org/t/strange-behaviour/16120/14 "2024-05-16T20:27:00Z")

</div>


