Powershell

Ternary Operator in PowerShell

PowerShell released its latest version, 7.2, with several new features. Various functions are not supported in PowerShell version 5 but in PowerShell 7.2, such as the “Ternary ?” operator. In case if you don’t have PowerShell 7 installed on your system, then simply read this post to install it. After doing so, utilize the ternary operator “?” as it is the most simplified form of the if-else condition.

This post will illustrate PowerShell’s ternary operator.

What is Ternary Operator in PowerShell?

A ternary operator “?” takes two expressions that need to be compared based on the condition. After that, there comes a ternary operator “?” and an output section. The output section is separated by the colon (:). The statement at the left of the colon gets executed upon the condition being true. When the condition is false, then the right-side statement comes into execution.

Syntax

<Condition> ? <True-Condition-Output> : <False-Condition-Output>

In the above syntax, the left of the ternary operator refers to a condition, and the right side contains the output.

Example 1: Use Ternary Operator in PowerShell to Compare Values

This example will illustrate the working of the ternary operator in PowerShell:

> (12 -lt 14) ? "True" : "False"

In this code example:

  • First, we have created a condition inside the small braces.
  • After that, we added the ternary operator “?” with two outputs, separated by a colon.
  • Upon the true condition, the output on the left side will be invoked. Else, the right-side statement will print out on the console:

As condition 12 is less than 14 has been evaluated as true, the output has been displayed as “True”.

Now, check what happens if the added condition turns out to be false:

> (12 -gt 14) ? "True" : "False"

Output

Example 2: Use Ternary Operator in PowerShell to Compare Variable Values

In this example, we will first store the values into the variables and, after that, apply the ternary operator:

> $a = 2
> $b = 3
> ($a -gt $b) ? "True" : "False"

In this code example:

  • First, we created two variables having the given integer values.
  • After that, we compared them via the ternary operator “?”.

The above output signifies that the value of the former variable is less than the latter one. So, “False” has been displayed on the console.

Now, let’s change the condition and make it true:

> $a = 2
> $b = 3
> ($a -gt $b) ? "True" : "False"

The condition proved to be true, which is why the resultant output is “True”.

Conclusion

The ternary operator “?” was introduced in PowerShell 7.2 as the keyword “?” (question mark). This operator is the simplified form of the “if-else” condition. In its first part, it defines the condition, and in the later part, it gives an output. A colon separates the output. If the condition is true, then the left side of the colon will get executed. Otherwise, the right side will get executed. This write-up guided about the ternary operator in PowerShell.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.