0%

0/1 Lessons

Course Introduction

• 5min

0 / 2 lessons complete

Getting Started with Windows PowerShell

• 56min

0 / 8 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

PowerShell Modules

• 58min

0 / 7 lessons complete

Powershell Challenges

• 1hr 55min

0 / 23 lessons complete

Section Overview
Full Access Account Required

Video | 5 min

The Trusted Hosts List Challenge
Full Access Account Required

Text | 5 min

The Trusted Hosts List Answer
Full Access Account Required

Video | 5 min

Starting a Remote Session Challenge
Full Access Account Required

Text | 5 min

Starting a Remote Session Answer
Full Access Account Required

Video | 5 min

Rename a Computer Challenge
Full Access Account Required

Text | 5 min

Rename a Computer Answer
Full Access Account Required

Video | 5 min

Change a Password on a Remote Computer Challenge
Full Access Account Required

Text | 5 min

Change a Password on a Remote Computer Answer
Full Access Account Required

Video | 5 min

How to Copy Files To-From a Remote Machine Challenge
Full Access Account Required

Text | 5 min

How to Copy Files To-From a Remote Machine using VSC Answer
Full Access Account Required

Video | 5 min

How to Export Logs to a CSV File Challenge
Full Access Account Required

Text | 5 min

How to Export Logs to a CSV File Answer
Full Access Account Required

Video | 5 min

How to Create Multiple Folders on the Host Machine using Powershell
Full Access Account Required

Text | 5 min

How to Create Multiple Folders on the Host Machine using Powershell Answer
Full Access Account Required

Video | 5 min

Who Rebooted the Server ID1074 Challenge
Full Access Account Required

Text | 5 min

Who Rebooted the Server
Full Access Account Required

Video | 5 min

How can you get Info on all your Hard Drives Challenge
Full Access Account Required

Text | 5 min

How can you get Info on all your Hard Drives Answer
Full Access Account Required

Video | 5 min

How can I get a list of CPU's and Installed Printers, Last Boot-up Time Challenge
Full Access Account Required

Text | 5 min

How can I get a List of CPU's and Installed Printers, Last Boot-up Time Answer
Full Access Account Required

Video | 5 min

How can I Automate Tasks with Task Scheduler Challenge
Full Access Account Required

Text | 5 min

How can I Automate Tasks with the Task Scheduler Answer
Full Access Account Required

Video | 5 min

Course Conclusion

• 1min

0 / 1 lessons complete

PowerShell Conditional Statements (If Then Else, Switch)

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.

Create note

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.

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