Administration and Automation with Windows PowerShell

0%

0/1 Lessons

Course Introduction

• 5min

0 / 2 lessons complete

Getting Started with Windows PowerShell

• 42min

0 / 7 lessons complete

Getting Help and Finding Commands

• 39min

0 / 6 lessons complete

PowerShell Command Syntax

• 33min

0 / 5 lessons complete

PowerShell Objects and Properties

• 35min

0 / 6 lessons complete

The PowerShell Pipeline

• 24min

0 / 2 lessons complete

PowerShell Providers

• 30min

0 / 5 lessons complete

PowerShell Arrays and Variables

• 28min

0 / 4 lessons complete

PowerShell Loops

• 19min

0 / 3 lessons complete

PowerShell Conditional Statements

• 11min

0 / 1 lessons complete

On Premises Lab Setup

• 36min

0 / 8 lessons complete

Basic Domain Administration with Windows PowerShell

• 2hr 27min

0 / 10 lessons complete

Send Emails with PowerShell

• 22min

0 / 2 lessons complete

PowerShell Desired State Configuration (DSC) Basics

• 1hr 48min

0 / 6 lessons complete

Course Conclusion

• 1min

0 / 1 lessons complete

You need to sign up to get access!

Sign up to get full access to this course.

Sign up to access this lesson

Click here to sign up and get access to this lesson!

Saving Progress...

PowerShell Conditional Statements (If Then Else, Switch)

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:

Sign up to access the rest of this lesson

You must either log in or sign up to access this lesson.

Saving Progress...

0 0 votes
Lesson Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments