Contents

Ternary Operator in PowerShell

Contents

I’m very noob .Net C# developer, but I remember this:

1
2
<compare> ? <value of true> : <value of false>
(a == a ) ? 1 : 2

I want the same thing in PowerShell, here we go:

1
. ({'condition is false'},{'condition is true'})[$condition]

Sample:

1
@("false","true")["a" -eq "b"]

Which I came up with a more readable and something easy to memory:

1
@{$true=1;$false=2}[$a -eq 'a']

Happy scripting….