I’m on the cutting edge, but my writing isn’t always there with me. When a new PowerShell feature is released, it’s not often me that’s telling you about it. That said, if it’s about trying something, or sharing something “cutting edge” at work, then that’s a different story. Well, this is a bit of a different story, too.
While it’s already been introduced, the Ternary operator joined us in one of the preview releases of PowerShell 7. As of this writing, PowerShell 7 rc2 has recently been introduced and the Ternary operator is still an experimental feature. This means, don’t use it in production just yet, as it could change and cause breaking changes for you. Before I go on, and in case you need to educate yourself about the Ternary operator, then read through this link: https://toastit.dev/2019/09/25/ternary-operator-powershell-7. I put this on Twitter recently and picked up several Retweets and Likes! Perhaps I should write about cutting-edge PowerShell topics. One person replied as though I wrote that article; nope, that wasn’t mine. This one is, however.
The Ternary operator allows you to simplify an If-Else statement, such as <condition> ? <If true> : <If false>
. I liked the new inclusion in PowerShell the first time I saw it, but today I wondered something. I eventually tested it, and as a result, I’ve authored a new article.
So to recap for a half-second, the newly introduced Ternary operator in PowerShell 7 is a condensed way to structure an If-Else statement. Notice, I didn’t say an If statement, and yes, there’s a difference between the two. But, what if we get to the point one day when the Ternary operator isn’t an experimental feature, but we want to employ this operator in place of an If statement. Not an If-Else statement.
It came to me quickly, and even as simple as it is, I thought I’d write about it, in order that the person it doesn’t come to quickly one day, can read this post right here. Let’s start with the Ternary operator’s intended use: If-Else. First, we’ll start with an If-Else, and then move to the Ternary option.
If (Get-Item -Path 'C:\file' -ErrorAction SilentlyContinue) { 'Located file.' } Else { 'Unable to locate file.' } # End If-Else. (Get-Item -Path 'C:\file' -ErrorAction SilentlyContinue) ? 'Located file.' : 'Unable to locate file.'
I tend to try and keep my PowerShell line length on the lower end. Do note that the next three examples are parsed properly across multiple lines, as one would expect. I did at least.
(Get-Item -Path 'C:\file' -ErrorAction SilentlyContinue) ? 'Located file.' : 'Unable to locate file.' (Get-Item -Path 'C:\file' -ErrorAction SilentlyContinue) ? 'Located file.' : 'Unable to locate file.' (Get-Item -Path 'C:\file' -ErrorAction SilentlyContinue) ? 'Located file.' : 'Unable to locate file.'
Let’s consider an example that lines up better with an If statement. Notice that I first clear the $Error variable. I don’t recommend this in most situations, but for this example (and, other real-life experiences), it has its place.
PS > $Error.Clear() PS > 1/0 RuntimeException: Attempted to divide by zero. PS > PS > $Error.Count 1 PS > $Error.Count -eq 1 ? $Error.Remove($Error[0]) : 'Nothing.' PS > PS > # ^ That removed the error, but let's run it again. PS > PS > $Error.Count -eq 1 ? $Error.Remove($Error[0]) : 'Nothing.' Nothing.
Now that you’ve looked over the above code, you can see why we don’t need an Else portion. Better stated: As we’re working with the Ternary operator, you can see why we don’t need an If false portion. If the error doesn’t exist it just doesn’t remove it; I don’t need a report that there’s nothing happening. Speaking of nothing, here’s what we’re really after.
PS > $Error.Count -eq 1 ? $Error.Remove($Error[0]) : $null PS >
Perfect — silence is golden. Makes you wonder though, right? Can we use $null in the If true portion? Of course. We can likely use it in both, at the same time? Yes, but I’m not going to go there. I mean…, how could that be helpful? Let’s do see an example of using $null in the If true portion, however.
PS > $Error.Clear() PS > 1/0 RuntimeException: Attempted to divide by zero. PS > PS > $Error.Count -eq 1 ? $null : '$Error.Count is less than/greater than 1.' PS > $Error.Count 1 PS > $Error.Remove($Error[0]) PS > $Error.Count 0 PS > PS > $Error.Count -eq 1 ? $null : '$Error.Count is less than/greater than 1.' $Error.Count is less than/greater than 1. PS > PS > $Error.Count -eq 1 ? $null : '$Error.Count is less than/greater than 1.' $Error.Count is less than/greater than 1. PS >
And that’s it! Me writing about some newer additions, and you do not have to wonder, or test, if the Ternary operator handles things as you’d expect it might.