PowerShell Conditional Statements (If Then Else, Switch)
Full-Access Members Only
Sorry, this lesson is only available to Server Academy Full-Access members. Become a Full-Access member now and get instant access to this and many more premium courses. Click the button below and get instant access now.
Instructions
Q&A (0)
Notes (0)
Resources (0)
Saving Progress...
Resources
There are no resources for this lesson.
Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.
In this lecture, we are going to discuss PowerShell Conditional Statements.
As always, I will launch the PowerShell ISE as an Administrator.
The first conditional statement we are going to talk about is If Then Else statements.
So, we can write an If statement very easily by just typing the following
If (1 -eq 1) { echo “True”}
This is a very simple statement. We are comparing the two values and if that is equal to true then we are going to echo out “True”. We can change “True” to “I Like hot dogs” or anything you want, it doesn’t really matter.
So what we are going to do now is hit Play.
Since 1 is equal to 1 that means “I like hotdogs” and outputs “I like hotdogs”.
Now, if we set this to 1 equals 2 obviously that’s a False statement since 1 is not the same as 2:
If (1 -eq 2) { echo “True”}
If we run this, it will not output “I like hotdogs”.
So, we can append an Else statement to this script.
If (1 -eq 2) { echo “True”} else {echo “I dislike hotdogs}
And I can change the equal condition value. Click Play.
Now, there's an easier way to write these IF statements. We can also write these in the command line just by typing:
If (1 -eq 2) { echo “Hi”}
So don’t feel like you only can do this in the ISE. You can also write IF statements directly in the PowerShell command line.
Now, there’s an easier way to format this. I just like to do the following:
If (1 -eq 2) {
echo “Hello”
} else {
echo “I dislike hotdogs
}
This is, in my opinion, an easier way to view this. If we hit play it is going to do the same thing:
Just keep in mind that there are different ways that you can output this data but in the end, it doesn’t matter. Some programming languages require that you format it in a separate way, some require semicolons. PowerShell is not one of those languages.
So, we can also compare not only integer numbers but we can also compare string variables or any kind of variable in general.
So, let’s update the code and do the following:
$MyVar1 = 1
$MyVar2 = 1
If ($MyVar1 -eq $MyVar2) {
echo “Hello”
} else {
echo “I dislike hotdogs
}
We are still comparing essentially 1 to 1.
We can change one of the variables to 2 and re-run the script.
So, we can also compare strings. So we could say:
$MyVar1 = “PowerShell”
$MyVar2 = “Bash”
If ($MyVar1 -eq $MyVar2) {
echo “Hello”
} else {
echo “I dislike hotdogs
}
Let’s move on and let’s look at how you can compare integers. You can use more than equal to.
$MyVar1 = 1
$MyVar2 = 0
If ($MyVar1 -gt $MyVar2) {
echo “Hello”
} else {
echo “I dislike hotdogs
}
Now if we change it to less than.
$MyVar1 = 1
$MyVar2 = 0
If ($MyVar1 -lt $MyVar2) {
echo “Hello”
} else {
echo “I dislike hotdogs
}
So, there are a lot of different ways you can do this.
Now, let’s talk about when you want to choose if you are creating a text-based menu for example. Let’s go ahead and create the script:
$choice = Read-Host “Please select an option (1-3)”
If ($choice -eq 1) {
echo “You chose option 1”
}
But if I run it with option 2 it is not going to do anything because we haven’t done that logic.
$choice = Read-Host “Please select an option (1-3)”
If ($choice -eq 1) {
echo “You chose option 1”
}
If ($choice -eq 2) {
echo “You chose option 2”
}
But that is not the right way to do this. It does get the job done. But really we would like to run all this inside the IF statement. I can do the following:
Server Academy Members Only
Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.