Concept Flow - Ternary operator (PowerShell 7+)
Evaluate Condition
Yes
Return True Value
End
No
Return False Value
End
The ternary operator checks a condition and returns one value if true, another if false, all in one line.
$age = 20 $result = ($age -ge 18) ? 'Adult' : 'Minor' Write-Output $result
| Step | Expression | Condition Result | Value Returned | Output |
|---|---|---|---|---|
| 1 | $age = 20 | N/A | N/A | Variable age set to 20 |
| 2 | ($age -ge 18) ? 'Adult' : 'Minor' | True | 'Adult' | Result set to 'Adult' |
| 3 | Write-Output $result | N/A | N/A | Adult |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| age | undefined | 20 | 20 | 20 |
| result | undefined | undefined | 'Adult' | 'Adult' |
Ternary operator syntax: (condition) ? value_if_true : value_if_false Evaluates condition once. Returns value_if_true if condition is true. Returns value_if_false if condition is false. Used for concise if-else in one line. Requires PowerShell 7 or newer.