# Powershell assignment makes no sense

**URL:** <https://forums.powershell.org/t/powershell-assignment-makes-no-sense/23649>\
**Category:** PowerShell Help\
**Created:** [February 26, 2024, 9:48pm UTC](https://forums.powershell.org/t/powershell-assignment-makes-no-sense/23649 "2024-02-26T21:48:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jan.Krynicky](https://avatars.discourse-cdn.com/v4/letter/j/7cd45c/32.png) [@Jan.Krynicky](https://forums.powershell.org/u/Jan.Krynicky)\
**Post date:** [February 26, 2024, 9:48pm UTC](https://forums.powershell.org/t/powershell-assignment-makes-no-sense/23649/1 "2024-02-26T21:48:34Z")

</div>

Read the code, guess what it prints, run the code and let us know …

```auto
$dict1 = @{
    'Key1' = 5
    'Key2' = 15
    'Key3' = 30
}

$dict2 = @{
    'Key1' = 8
    'Key2' = 14
    'Key3' = 29
}

$array = @($dict1, $dict2)
Write-Host "We have $($array.Count) items in the array"

$result = $array | Where-Object Key1 -eq 5
Write-Host "And now have $($result.Count)"

```

And yes, it likely is documented. Somewhere. Maybe.

---

<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:** [February 26, 2024, 9:59pm UTC](https://forums.powershell.org/t/powershell-assignment-makes-no-sense/23649/2 "2024-02-26T21:59:46Z")

</div>

What’s your question? Or what’s your issue with it? Or what would you have expected?

> [@Jan.Krynicky](#):
>
> ```auto
> $array = @($dict1, $dict2)
> Write-Host "We have $($array.Count) items in the array"
> 
> ```

It is an array with 2 hashtables in it. 🤷🏼‍♂️

> [@Jan.Krynicky](#):
>
> ```auto
> $result = $array | Where-Object Key1 -eq 5
> Write-Host "And now have $($result.Count)"
> 
> ```

Here you filter for all hashtables where key `Key1` has the value `5`. It is just 1 of the hashtables. And that’s a hashtable with 3 hashsets in it. 🤷🏼‍♂️

---

<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:** [February 27, 2024, 12:50am UTC](https://forums.powershell.org/t/powershell-assignment-makes-no-sense/23649/3 "2024-02-27T00:50:08Z")

</div>

Yep Olaf is right here. I think I understand your confusion though. You’re expecting the result to be `1` yes?

What’s happening is essentially nested objects. When you do the $array assignment, you’re creating two objects in the array, which each have ‘sub objects’, if you will. You then are setting $result equal to a filter, which actually happens to be only 1 object, which has 3-sub objects in it. Because of this, it’s returning that top level object which has 3 objects. If you want to ‘force’ it to a single object, you can try casting the variable to an array:

```powershell
[Array]$result = $array | Where-Object Key1 -eq 5
$result.count

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [March 28, 2024, 12:50am UTC](https://forums.powershell.org/t/powershell-assignment-makes-no-sense/23649/4 "2024-03-28T00:50:15Z")

</div>

Closed on March 28, 2024, 12:50am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
