The PowerShell 7 team have just released a new PowerShell 7 Preview build. This contains a new, and interesting, new operator, the ternary operator.
The Ternary operator allows you to simplify
This operator is, in effect, a short cut to If/Else. Thus, instead of:
$Message = If ($IsWindows) {"Is Windows"} Else {"Is NOT Windows"}
$Message = $IsWIndows ? "Is Windows" : "Is NOT Windows"
So a cool new feature coming to PowerShell 7. It is available in the latest Preview but as an Experimental feature. Experimental features in PowerShell 7 are features you turn ON and OFF as you like. By default, experimental features are turned off until you turn them on. To view them and turn them on is simple - just use Get-ExperimentalFeature and Set-ExperimentalFeature like this.
A relevant question is whether you SHOULD use this operator. For simple things, such as the snippets above, it does enable a more compact script. at the expense of potential confusion by folks not versed in the new syntax. It may not pass the "Three AM test" (ie can you easily and simply work out a bug in this code when woken up at 3 in the morning?).
Additionally, in my testing, this operator is sometimes a little faster than the old fashioned approach, but there is not a lot in it:
So a new operator, which you have to enable explicitly. It does offer some small performance gains in some cases. Coming soon to a Powershell 7 near you!
No comments:
Post a Comment