Learn About PowerShell 7 Ternary Operator with Examples!
4K views
May 14, 2024
What is the PowerShell ternary operator and what does it do? Learn all about this new PowerShell 7 feature and how you can use it in scripts and functions! š Get the Code Here! š https://github.com/JeffBrownTech/jeffbrowntech_youtube/tree/main/2024-05-13-PowerShell7TernaryOperator ā Read: PowerShell 7 Ternary Operator: Introduction & Examples ā https://jeffbrown.tech/powershell-7-ternary/ ā° Chapters ā° 00:00 Introduction 00:15 Review if else Statements 00:40 Ternary Operator Syntax 01:57 More Examples 03:31 Nested Ternary Operators 04:34 Multi-line Ternary 05:32 Calculating Values using Ternary 07:22 Summary Social Links š LinkedIn: https://www.linkedin.com/jeffbrowntech
View Video Transcript
0:00
What's up, everyone? Today we're going to talk about a feature that came out in PowerShell 7 and the
0:04
Ternary Operator. We'll compare it to a regular if-else statement and a couple of tips and tricks
0:10
you can use when you're trying to put it in your scripts or functions. Let's go ahead and get started
0:15
So here we have an example of a regular if-l statement. I have two variables I've defined
0:20
value A and value B. And we're just saying if value A is greater than value B, if that evaluates
0:26
to true, we're going to output a statement. Otherwise, we'll say it is a value A and value B. We're just saying it
0:29
is less than that value. Pretty straightforward example. We'll just highlight this and execute it
0:36
And it comes out accurately saying 50 is less than 100. Now the turnary operator does the exact same thing
0:43
It has a condition, and then a part of it that executes if that is true
0:48
and then another part that executes if that is false. Here on line 12, we can take a look at that syntax
0:55
We have our condition, and then a question mark. The portion after the question mark is the code that is executed if it's true
1:03
Then after the colon, the code that is executed if the statement is false
1:09
Here is our if-l statement that we just took a look at, written out in a ternary format
1:15
We have our condition that we're evaluating. Value A, greater than value B, is that true or false
1:21
If it is true, we output our statement here after the question mark
1:25
If that is false, then we have our statement. statement to execute after the colon
1:31
So we take this line and execute it. We get the exact same output
1:36
But you can see, you know, compared to this up here, we have, you know, six, seven lines or so
1:43
That's, you know, quote unquote, maybe properly formatted. And then down here, we can just put it into one line
1:48
The syntax is just a little bit more compact. This type of turnary operator is available in multiple underlanguages And now PowerShell has it as well Let take a look at a few more examples of how you can use the turnary operator Here on 919 our condition
2:03
we're testing to see if a path exist. One thing to note here is I have the condition
2:08
wrapped in parentheses, meaning it needs to evaluate that first, whereas our other example
2:14
we're just doing a comparison, evaluate greater than value B. Sometimes in these turn area operators
2:19
you will need to put your condition or what you're evaluating inside
2:23
of parentheses so we can evaluate that first. We're going to test to see if this path exists
2:29
If it does, if that evaluates to true, we're just going to output the directory already exist
2:34
And then if it doesn't, if that is false, we are going to create the directory
2:39
Okay, so let's highlight our line here and execute it. It's going to come back and we'll show it just outputs that it created the directory
2:46
But if we run that again, it should come back and say the directory exists because our earlier statement executed and created it for us
2:53
Let's take a look at another example here. PowerShell 7 also introduced some automatic variables, meaning variables that automatically
3:02
exist in your session and you don't have to create them. We have is Windows, is MacOS, or is Linux
3:08
These just evaluate to true or false based on your operating system
3:12
So here I have is Windows, that's our condition, just a single statement, you're running
3:16
windows, you're not running Windows, and so on for the rest of our operating systems
3:21
We'll highlight all three of these. It'll come back. You are running Windows
3:25
You are not running MacOS. You are not running Linux, which is accurate. I'm on a Windows system here
3:31
One thing you can do is nest your ternary operations. So let's take a look at this next example here
3:38
First, we're just going to say, is Mac OS? Am I running on a Mac operating system
3:43
The true part of that is you are running on Mac. It will output that statement. But then in the false part of it, we can then create a
3:51
another turnerary operation We can say is this Linux And then the true part of that says you are running Linux And then the false part of that will just then default to your running Windows
4:03
So think about how you might do this before you had turnary operators. You would say, is macOS
4:09
You know, if this is true, you're running macOS. Else if are you running Linux
4:16
You are not. So then an else statement, you're running Windows. Lots of things to type out there
4:21
Again, here with ternary operators is just a slightly simpler syntax. We'll execute this one line here instead of the three different ones we had earlier and saying you're running Windows
4:34
If you have slightly longer conditions or maybe true, false code that you're executing in your ternary operator
4:42
you can put it on multi-lines and you don't need to use anything like a backtick or pipe symbol or anything like that
4:48
You can do new lines on each of the question. and the colones that are part of the ternary operator
4:54
So then this is just our example from earlier. We're going to test to see if this path exists
4:59
Question mark for the true part of the ternary operator, I can go on a new line here saying it already exist
5:06
put in the colon and then go to a new line saying here is the false part of that
5:11
If I highlight this and rerun it, get the exact same thing
5:16
We know this directory already exists. Just showing how if you need to, you can put this
5:21
on multiple lines without using some of the things you probably already know with using backticks
5:26
or anything like that. You can just put new lines on each of those pieces of syntax there
5:32
Finally, a really good example of using turn area operators is calculating values inside of your script
5:38
If you're trying to figure out, you know, if this equals this, then you want something to equal, you know, another value
5:44
We'll demonstrate this here. I'm going to create an array called widget array
5:49
It is a collection of some custom here that includes the name of our widget the price and if it on sale true or false So let me highlight this and get this into my session here
6:03
And then we'll go down inside of my four each. We're going to iterate through each widget and create another object that's going to output
6:11
to our command line here. We're going to put the name of the widget. We're going to say is over $10
6:16
And I have a turnerary operator here. It's looking at the widget. Is it greater than 10
6:21
Then that becomes true. Otherwise, it's false. Is the item on sale
6:26
We're looking at that property. And then we're going to put, is it on sale? Not on sale
6:30
Just output that. And then if it's on sale, here's the sale price
6:34
It's 85% of the regular price. Otherwise, we're just going to output some dashes here
6:40
We'll highlight our 4 each here and execute it. And it outputs our information here
6:46
Which at 1 is not over $10, but it is on sale. So here's the sale price
6:51
The other ones are over $10. One's on sale, one's not, and calculating those price
6:56
But just showing how instead of maybe having if-l statements beforehand, calculating all these different properties on your custom object that you might have here
7:06
or having, you know, if-l statements on all one line, it might be a little harder to read with the curly brackets and everything in our traditional
7:13
if-l statements. Turnary operator is just simpler syntax, and it keeps your script or function nice and neat
7:22
Well, that does it for this video. Hope you learned a little something here about a turn area operator
7:26
and start thinking about how you could include this inside of your scripts or functions
7:30
Thank you for watching, and we'll see you next time. Thank you
#Computer Education
#Programming