2

Why isn't where-object working in this case?

$controlFlowArgs = @{ waitForEnter = $false }
$controlFlowArgs | Format-Table
$controlFlowArgs | Format-List
$result = $controlFlowArgs | Where-Object -FilterScript { $_.Name -eq "waitForEnter" }
$result

Output

Name         Value # Format-Table
----         -----
waitForEnter False

Name  : waitForEnter # Format-List
Value : False

# Missing result would be here
1
  • What exactly is the expected result? It could be just the value or both key-value pair. Also, should the input be an array of hashtables? Commented Nov 29, 2021 at 17:55

2 Answers 2

2

$controlFlowArgs is a HashTable. You should probably think it differently.

$result = $controlFlowArgs | Where-Object { $_["waitForEnter"] }

would store $false in $result. Else you can use the Hashtable directly:

if ($controlFlowArgs["waitForEnter"]) {
  ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

Where-object is working fine. In this example, only the 'joe' hashtable appears. Confusingly the format takes up two lines.

@{name='joe';address='here'},@{name='john';address='there'} | ? name -eq joe

Name                           Value
----                           -----
name                           joe
address                        here

It's still considered one thing:

@{name='joe';address='here'},@{name='john';address='there'} | ? name -eq joe | 
  measure-object | % count

1

If you want the value itself, use foreach-object (or select-object -expandproperty):

@{name='joe';address='here'},@{name='john';address='there'} | ? name -eq joe | 
  % name

joe

Usually powershell works with pscustomobjects:

[pscustomobject]@{name='joe';address='here'},
  [pscustomobject]@{name='john';address='there'} | ? name -eq joe

name address
---- -------
joe  here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.