Ternary Operator in PowerShell

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

<compare> ? <value of true> : <value of false>

(a == ‘a’ ) ? 1 : 2

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

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

Sample:

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

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

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

Happy scripting….

Like what you read? Give River Yang a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.