So let's go into our PowerShell console,
and we'll first review how we can use the if/else statements,
and then we'll implement some basic logic using if/else statements.
So we're back in the PowerShell console.
And, of course,
what we want to do here is actually look at the if statement itself.
So the first thing we'll do is actually just create a variable,
and I'm just going to create it as an arbitrary variable here,
which just basically says = "Value",
and of course to check the value of that one,
I can just do this and it will return that value for me.
Now what I want to be able to do is now that I've got this
variable is run it through an if statement to be able to
check if a value is in there.
So I can do this by saying if($variable),
and then of course you can see we have our code block commands right here.
This is where we type in the commands that we wish to run.
So I'm going to say Write,
let's just do Write‑Host and say the $variable returned, and I'll just say
true, and then close that. And notice when I press Enter,
it executes the code immediately because it's just
a line that says if something.
So we have some syntax now where it just says if the
variable has a value of some description,
we're not doing the evaluation of the value that's in there,
whether it's a 1 or a 10 or a string value, we're
literally just saying if it has something,
then render that value out.
So let's just clear that for a second and try this one again.
And this time, what we'll do is we'll include an else statement.
So what I can do here is you can see I'm writing it all out on one line.
So what I'm going to do here is actually just kind of go here and press Enter,
and you'll notice it gives me to kind of chevron arrows, that means I can
continue writing my code in the next line down because,
of course, as you're writing code,
you need a little bit more space than just trying to write it on one line.
So $variable check returned true,
and then we'll do this, and then of course I can go here, I can
then type the word else with my open bracket here, and then I'm
going to say Write‑Host "The $variable check returned false",
and then close that out.
So that gives me my statement.
Now, of course, you'll notice it's still kind of sitting there,
and it doesn't really know what to do with it, so when you
look at your code you need to double check that you've got
everything in the right places.
I'm going to press Enter, that will finish, and it will say The Value check
returned true because, of course, it does, it has a value.
So what I can do now is I can say $variable = null. Now,
of course, notice what happened.
It doesn't take that blank value or understand what the word null is, so
if I now do $null and then say go and get me variable,
you notice it has no value.
So let me just clear this, and what we'll do is just loop
through our commands and I should get my larger one that's here,
and I can quickly write this value out.
So if we just loop back through here,
so clear if the variable write it true, else write it false. Oh,
I don't need that one, I can just actually press Enter here. And then
it will come back and say the variable has no value,
so it doesn't write anything out. It now returns false.
So this is literally the most basic if statement is just
checking if the variable has a specific value.
Now, of course, though that's useful, it's not always the best approach because,
of course,
what you want to be able to do is check if this value is equal to
something or it's like that or it matches something else.
But before we can do that,
we also need to understand that we can have multiple comparisons too.
So I'm going to create a new variable, and I'll assign it the number 1.
I'll then do a variable2 2 and assign that number 2.
So we have two variables, variable and variable2.
What I can then do is do my same kind of if statements,
and I'm just going to check if they have a value.
So I'm going to say if($variable), remember I can then go to the statement and
say Write‑Host, and then from here I'll just say $variable.
So I'm just going to output the value. Then,
of course,
at the end of this one I'm going to close that code, kind of script
block out, then I'm going to do something different.
I'm going to say elseif, and then I'm going to check variable2. So
variable2, there's my script block, Write‑Host. I'm then going to
say $variable2, close that script block,
and then I'm going to do an else statement,
which will then just say Write‑Host "Anything", and
then I'll close that one out too.
Okay, now notice what it does.
It passes the first test and it says yes,
there's a value, and then it writes out the value because that's
the first thing it matches because it says okay,
this has a value of some description.
Now,
if we were to change that, if I said variable = $null, like so,
I'm just going to clear that because we're going to have to tap
through a couple of these again.
So let me just leap up here.
I'm going to say if($variable), Write‑Host, elseif, Write‑Host, else, Anything,
it now goes to number 2 because the first one fails the test because it's a
null entry, and so it then goes to the second one.
If I was then to null out the second variable, it would drop out to anything.
So we can do that too. So I can say variable2 = $null.
Let's go back here and clear,
and we'll just loop back up to if($variable), Write‑Host, else,
Write‑Host, else,
Write‑Host Anything, let me get rid of that last line, and Enter,
and it should go to Anything.
So as you can see,
we've just forced it to drop down to each one. So variable, when it was
correct and it was not null, it was a value of 1 so it passed.
Then when that was null, it went to variable2, and then when that was null,
it went to the default value, which is number 3.
Now, of course, that works out really,
really well in order for you to be able to compare and
check whether a variable has a value.
But like we talked about, you often want to compare things together.
So, I'm going to create a new variable1,
and I'll put the number 1 in that variable.
I'll then go ahead and create variable2 and put the number 3
in there. So we have to number values.
Now what we're going to do here is look at how we compare those two together.
So I'm going to say variable1, and then I'm going to use the eq operator.
So let's do variable2.
So if variable1, so number 1, equals number 3,
then, of course,
I'm going to write the host message here and say They are the
same. And we'll leave it as that, I'm not going to add an else
or an else/if, but when I run it, you can see what happens.
It just says, well, if they're equal to something,
do something, and because I don't have an else, which is the default dropout,
it returns me no value whatsoever.
Now, if we go back and change this and say,
well,
what about if we chose a different operator, such as less
than, now I get a message back. Now, of course,
the message makes no sense because I'm having it write They are the same, but
if the variable was less than, or I could change it and say if it was greater
than, you can see that we get different outputs.
So this is the key in using if statements is that they can often
output different values based on the kind of querying that we're
doing together to be able to say, oh, hey,
if this number is greater than that or less than this or
this text value matches this value,
etc. So we can do all kinds of different if statements and checks here.
Now, of course,
one of the big things that we can do is obviously
creating variables that contain values.
So I'm going to create a new variable here.
I'm going to use 1..10, and then I'm going to say, well, what's in the array?
And sure enough, you can see it's 1 to 10. So this creates me an array of values.
What I'm also going to do is just create a new variable called compare,
and I'll assign that to number 6.
So let me just clear that.
So let me just go back and say array and then
$compare 6, so we have the two values.
What I want to be able to do here is check if the current array or the
array contains the number that I want to utilize.
So I'm going to use an if statement again.
So I'm going to say, if($array, and then what I can do is say ‑contains,
which is a valid operator, and I'm going to say contains the
$compare, then I'm just going to say Write‑Host "Compared." So
let's press Enter, and of course, sure enough,
it does.
The array contains that specific number. Now we could flip this
around the other way and do a different kind of validation here. So
I can go back and say compare is in the array, and it will come
back and give us the same value.
Now, if we look at the syntax a little bit,
and maybe I'm going to change from contains and say
array is greater than compare, it will come back and say,
well, yes, there's more in the array.
It's a greater value than that that's in compare,
which obviously works for numbers.
If we're using other values,
it becomes a little more complicated and you have to
do other things to make that work.
Now, of course,
we could do simple calculations, we could do multiple calculations.
So, let's say we wanted to use our same variables.
So let's just double check.
We have our array.
So let's say we wanted to check if a value existed in something and
then if a number was bigger than something else.
So what we can do here is say if, let's do compare ‑in $array.
So there's our first comparison statement.
Now I'm going to do something different here.
I'm going to go here and put double kind of quotes around it.
But before I put the other one in there,
I'm going to say ‑and, and then I'm going to close
out. So you can see our syntax here.
So if the current compare value is in the array and I'm going to say compare
is greater than 4, I'm just using an arbitrary value here,
then we're going to use our script block here,
and I'll just say Write‑Host "Yes", and press Enter. Now notice it passes both.
It will say, well yes,
compare does exist in the array and then compare is greater than 4.
Now, if I go back and change this to 8, we should get nothing,
which is exactly right.
And why do we get nothing?
Because if we just go here, I didn't have an else output.
So I'm going to say Write‑Host "No" and close that.
And now we get a No. So if you don't utilize an else or else/if, you'll
always just pass the first one and then nothing gets passed,
you'll get no value,
no nothing, it just kind of stops that's there. So
you can see we can do a comparison.
I'm just going to clear this again,
and what we'll do is I'll just change the if one here, and this time
instead of saying ‑and, I'm going to change that to be an ‑or because
we know that it fails on this second one.
So let me just add my else statement.
Okay,
there we go, and press Enter, and it should say Yes because it
ignores the second one because it does a test and whichever one of
those passes will allow it to move forward.
So you need to be kind of a word of warning with that one is making
sure that that's exactly what you want to do.
Very rarely are you wanting to take two values and
doing an or comparison between them.
Okay, so our last one here that we want to look at quickly, if we just go down,
is we want to look at the kind of numeric values and kind
of how we can add those things together.
Now it's really,
really simple to kind of do. So what we can do here is
we can kind of add things together.
So if I say variable1 = 1 and variable2 = 5, I can then say, well,
if $variable1 + $variable5, now notice I'm doing a sum here,
so I need to kind of put those into a separate piece, then I can take
the output of that and say if that is greater than 4,
then do something. So you can see we, like normal math, you do
the kind of calculations first and then you use the output of the
calculations to do something else.
So if variable1 + variable5,
which should give us 6, is greater than 4, then Write‑Host "Yes",
and we'll just just do a Yes output for now.
So, then we have that, it will say, well,
is it greater than it, is it less than it, and it adds those together. Now,
of course, if we had a No option, then of course we could do that too.
Now,
what I want you to do is just look at the syntax for a
second and wonder why it didn't execute.
And when you look at it,
you can kind of see that we've got an if statement where we've gone
through and said if do this is this is greater than this and this, and
should it return a Yes or should it return a No? Now,
of course,
depending on the calculation that you're doing, so we could say times, do
that, and then you get that if variable1 * variable5, etc.
Now you may wonder why it's still not working, hopefully you've all seen it,
and it's because I named the variable the wrong name
and it didn't return anything.
So if I change that to variable2, then sure enough,
I get the right name, I get the right value.
If I change variable5 to variable2, I get the output as well.
So just be aware. You'll notice it didn't error, it
just kind of returned nothing.
So it's critical that you name things correctly when you're
building variables and that you make sure that you're using the
right ones as you kind of go through your code.
So that was a little bit of a test for you there.
Okay, finally,
the option that we have available to us now in PowerShell
7 is obviously the ternary command.
So let's do the same thing again.
So this one is going to be a number 1 and $variable2 is going to be a number 3.
Now, if we look at this syntax, it's a little bit easier.
So variable1 is equal to variable2, which is the one that we use in.
And then, of course, we simply do this one.
So I'm going to just return a true value,
and then I'm going to return a false value. So it should
return false. Exactly the same as writing the if statements
except it's a little bit cleaner,
a little bit simpler, and easier to kind of return those values.
So that was just some base examples of how to use the if and the else operators, as well as the ternary commands, etc., to help us define the flow of PowerShell.