2010-10-18

if operator (not) in PowerShell

I do miss the if operator (?:) that I have in C++, C#, JavaScript and likewise but not in PowerShell.

A quick workaround is the subexpression ($()) in a string surrounded by ".

$ref = 'blue'
"The color is $(if ($ref -eq 'blue'){''} else{'not '})blue."

The output is
The color is blue.

If the $ref value is changed
$ref = 'yellow'
The output is
The color is not blue.

Reference

wikipedia: ?:

No comments: