Press "Enter" to skip to content

Powershell 7’s Ternary Operator

Jeffery Hicks looks at a nice addition to Powershell:

Using this operator in PowerShell 7 means, you can write a PowerShell expression like this:

$IsWindows ? "ok":"not ok"

The traditional If statement alternative would be:

if ($IsWindows) {
    "ok"
}
else {
    "not ok"
}

Their implementation pretty much matches C#’s. Read on for additional testing Jeffery did and a few more examples.