Workaround for Unexpected token '?' in expression or statement
The ternary operator was included in (I believe) PowerShell 7.1. In earlier versions, trying to use the ternary operator would fail with the error message Unexpected token '?' in expression or statement..
A workaround in such version sis to use the evaluation value of an
if
statement:
$flag = $false
$val = if ($flag) { 'true' } else { 'false' }
write-output "val = $val"
In some earlier versions of PowerShell Core, it was possible to enable the ternary operator as an experimental operator:
PS C:\> enable-experimentalFeature PSTernaryOperator