PowerShell Ternary Operator and Conditional Expressions

An if/else block that assigns one of two values to a variable takes four lines of code for something logically equivalent to a single expression. PowerShell 7 introduced the PowerShell ternary operator ?: for exactly these one-line conditional assignments, matching a syntax familiar to C#, JavaScript, and Python developers. This post covers the syntax, practical use cases, how to use it inside strings, the PowerShell 5 workaround, and when not to reach for it.
Ternary Syntax in PowerShell 7
The ternary operator uses the form condition ? valueIfTrue : valueIfFalse. It requires PowerShell 7.0 or later — check with $PSVersionTable.PSVersion.Major -ge 7:
#Requires -Version 7.0
$age = 25
$category = $age -ge 18 ? "Adult" : "Minor"
Write-Host $category
Adult
Compare with the equivalent if/else:
# Four lines replaced by one
if ($age -ge 18) { $category = "Adult" } else { $category = "Minor" }
# Ternary equivalent
$category = $age -ge 18 ? "Adult" : "Minor"
Assign Value Based on Condition
Ternary is most useful for assigning one of two values to a variable based on a simple boolean test. Common patterns include status labels, default values, and flag-based messages:
$service = Get-Service -Name "W3SVC"
$statusLabel = $service.Status -eq 'Running' ? "Online" : "Offline"
$freePct = 85
$diskStatus = $freePct -gt 20 ? "OK" : "LOW"
$fileName = $env:COMPUTERNAME + "_" + ((Get-Date).DayOfWeek -eq 'Monday' ? "weekly" : "daily") + ".log"
Write-Host $fileName
SERVER01_weekly.log
Ternary in String Interpolation
Use the ternary operator inside $() within a double-quoted string to embed conditional text without breaking out of the string:
$cpu = 87
$message = "CPU is at $cpu% — status: $($cpu -gt 80 ? 'HIGH' : 'Normal')"
Write-Host $message
$count = 1
$label = "Found $count user$( $count -ne 1 ? 's' : '' )"
Write-Host $label
CPU is at 87% — status: HIGH
Found 1 user
PS5 Workaround with -if/-else Hashtable Trick
PowerShell 5 does not have the ternary operator. The closest equivalent uses a boolean as an array index — $true becomes 1 and $false becomes 0:
# PS5 workaround — index into an array with a boolean
$age = 25
$category = @("Minor", "Adult")[$age -ge 18]
Write-Host $category
# Another PS5 pattern — hashtable lookup
$status = Get-Service W3SVC | Select-Object -ExpandProperty Status
$label = @{$true = "Running"; $false = "Stopped"}[$status -eq 'Running']
Write-Host $label
These workarounds are less readable than a proper ternary. If readability matters, an inline if expression may be clearer for PS5 code:
$category = if ($age -ge 18) { "Adult" } else { "Minor" }
Nested Ternary (Avoid It)
PowerShell 7 supports nesting ternary operators, but the readability cost is high. If you need more than two branches, use a switch or if/elseif block:
# Nested ternary — technically valid but hard to read
$score = 72
$grade = $score -ge 90 ? "A" : ($score -ge 80 ? "B" : ($score -ge 70 ? "C" : "F"))
Write-Host $grade # C
# Much more readable with switch:
$grade = switch ($score) {
{ $_ -ge 90 } { "A"; break }
{ $_ -ge 80 } { "B"; break }
{ $_ -ge 70 } { "C"; break }
default { "F" }
}
Common Use Cases
The ternary operator shines in these specific scenarios:
- Status labels:
$label = $svc.Status -eq 'Running' ? "Running" : "Stopped" - Plural/singular text:
"$count item$($count -ne 1 ? 's' : '')" - Default value fallback:
$value = $input ? $input : "default" - Path selection:
$path = $isAdmin ? "C:\System" : "C:\User"
Common Errors and Fixes
-
Not available in PowerShell 5 — version guard needed. Using
?:in a script that may run on PS5 will throw a parse error. Add#Requires -Version 7at the top of scripts that use ternary, or use the array-index workaround for scripts that must be cross-version compatible. - Nested ternary is hard to read — use if-else instead. More than one level of nesting makes ternary expressions nearly impossible to parse at a glance. The rule of thumb: if you cannot read the expression in one mental step, rewrite it as an if/elseif chain or switch statement.
Related Cmdlets / See Also
Wrapping Up
The PowerShell 7 ternary operator is a readability improvement for simple two-branch assignments. Use it for status labels, plural forms, and default value selection. Add a #Requires -Version 7 guard to prevent it running on PS5, avoid nesting beyond one level, and reach for switch when you have three or more branches.


