You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I noticed that syntax highlighting does not work properly for control statements used on the right-hand side of assignments.
I tried a small fix and for now it seems to work fine without unexpected side-effects.
I'm not opening this as a PR because I don't know how to write syntax tests, and I'm unsure if this needs to be implemented differently as a proper fix.
You can also assign the value created by a statement to a variable, as in the following example:
$a=if ($b-lt0) { 0 } else { $b }
This example assigns zero to the $a variable if the value of $b is less than zero. It assigns the value of $b to $a if the value of $b isn't less than zero.
And as an example, not itself documented at foreach:
I think I've found the real documentation for this at Statement values. These can get crazy:
$i=1$v=while ($i-le5) {
$i# $i is written to the pipelineif ($i-band1) {
"odd"# conditionally written to the pipeline
}
++$i# not written to the pipeline
}
# $v is object[], Length 8, value 1,"odd",2,3,"odd",4,5,"odd"
Does this mean that technically every statement is an expression? Including function and class definitions?
# legal, $foo is 4 (= 0 + 4)$foo=try { Get-Thing } catch { 0 } +4# crashes$foo=4+try { Get-Thing } catch { 0 }
# compiles, but unclear what $foo becomes$foo= function Name { 5; Write-Host6; return7 }
I think we move exceptions, loops, flow, and conditionals from statements to expressions-without-commands and let the user sort it out if they try to add 4 + try {....
Activity
michaelblyons commentedon Jun 23, 2026
Hi Steve, thanks for reporting!
Do you know if those conditionals, loops, and exceptions are available in any arbitrary expression?
Like can I do this with a try/catch?
michaelblyons commentedon Jun 23, 2026
The OP's pattern for
ifis seen in the official docs in Assignment Operators:And as an example, not itself documented at
foreach:michaelblyons commentedon Jun 23, 2026
I think I've found the real documentation for this at Statement values. These can get crazy:
Does this mean that technically every statement is an expression? Including function and class definitions?
michaelblyons commentedon Jun 23, 2026
As usual, PowerShell is more complicated.
I think we move
exceptions,loops,, andflowconditionalsfromstatementstoexpressions-without-commandsand let the user sort it out if they try to add4 + try {....Support statement values
Support statement values (#233)
Support statement values (#233)