Updated March 23, 2023
Introduction to If Else in PowerShell
We have learned from the IF statement in PowerShell that it gives us the power to decide the execution of any statement on the basis of certain conditions. But what if we want to execute another statement in case of IF condition gets failed or what if we have multiple situations where if one condition gets failed then it should check for other conditions. Let’s take an example, suppose we want to show student grades in percentage. Like if the grade is A than show message 50%, if a grade is B then 65% if the grade is C than 80% and If a grade is E show 90%. So to display different messages on the different grade IF statement is not enough, for such type of situation we can use IF ELSE of PowerShell. IF ELSE gives us the power to execute a different set of statements on the basis of weather condition expression if true or false. We will learn it in more detail in examples and in the syntax part.
Syntax:
In the below syntax we are checking condition expression 1 if this expression returns true then statement 1 will execute if expression 1 returns false it will execute statement 2.
if (<condition expression 1>)
{<statement 1>}
[else )
{<statement 2>}]
Flowchart
In the below flow chart first, it will check for condition expression 1, if condition expression 1 returns true it will execute statement 1 if condition expression 1 returns false then statement 2 will execute that means it will execute else block if the condition of IF is false.
Refer to the flow diagram given below for better understanding. Let me explain to you a real time situation where you would need to write else if. Suppose You have a server that is running and is connected to database d1, and we have another database d2. And we are writing a script that will check if database d1 is active or not. If database d1 is inactive than connect to another database d2.So to write a program for this we can use the if-else statement. In the if statement we will check the status of database d1 if it returns true, which means it is running so no need to connect to database d2. If database d1 status is false then it will connect to database d2 and our server will keep working.
How If Else Statement Works in PowerShell?
It’s always based on the conditional expression, these conditional expressions always return true or false, for example, if you want to check number comparison, if you want to check less than greater than and if you want to check type of variable (checking if variable if integer, string boolean or float) all these operations will return either true or false. In the below example we are checking if 5 is equal to 5 or not, so it is equal so it will print the message ‘Number is 5’.
Example:
If (5 -eq 5) {
'Number is 5'
} Else {
'Number is not 5'
}
In below example, we are checking if 4 is equal to 5 or not, and the condition is false, hence it will print the message,’Number is not 5’
If (4 -eq 5) {
'Number is 5'
} Else {
'Number is not 5'
}
Output:
Examples of If Else Statement in PowerShell
Below are the examples of If Else Statement in PowerShell:
Example #1
In this example, we are running a loop on numbers 1 to 10 and check if the number is less than 5 or equal to 5 or greater than 5. So here each time it compares the number with 5 if the condition expression inside the if block is true it will print the statement else it will check for the next if condition. And in the final, if none of the condition is true it will print the else block.
Code:
$Data = 1..10
$Data | ForEach {
If ([int]$_ -gt 5) {
"$($_) number is greater than 5"
} ElseIf ([int]$_ -lt 5) {
"$($_) number is less than 5"
} ElseIf ([int]$_ -eq 5) {
"$($_) number is equal to 5"
} Else {
"Number is not identify"
}
}
Output:
Example #2
In this example, we have marks of a student and we want to print a message about his/her marks. let see below example here we have students mark is 40 and we are checking his marks. So whichever condition of if expression gets the success it will print that if statement messages.
Code:
$studentMarks = 40
if($studentMarks -eq 30){
write-host("Value of student marks is 30")
} elseif($studentMarks -eq 40){
write-host("Value of student marks is 40")
} elseif($studentMarks -eq 50){
write-host("Value of student marks is 50")
} else {
write-host("Value of student marks is 60")
}
Output:
Example #3
Let’s work with string operation, suppose we have one student and based on the match we wanted to display the message about student name. So if the student’s name is Ranja Display “Name of the student is Ranjan”.
Code:
$studentName = “Ranjan”
if($studentName -eq “Ajay”){
write-host("Name of student is Ajay")
} elseif($studentName -eq “Vijay”){
write-host("Name of student is Vijay")
} elseif($studentName -eq “Ranjan”){
write-host("Name of student is Ranjan")
} else {
write-host("IT LOOKS STUDENT DOES NOT BELONGS TO US")
}
Refer below the screen, here we assigned the student name as Ranjan, so when IF conditions get succeeded (ie. it matches the name) it will print that IF block else it will print ELSE block. You can try by assigning Ajay to $studentName and you can check the output.
Output:
Example #4
In this example, we are checking the student study graph, which means we are checking if the student is studying properly or not. So here we are assuming currently student marks is 200 and we are assuming that if marks are more than 200 than the student is hard-working if marks if equal to 200 than the student is ok and if marks are less than 200 it means the student is not studying properly.
Code:
$marks=200
if ($marks -gt 200) {
Write-Host("This student is very hard working.")
} elseif ($marks -eq 200) {
Write-Host ("This student is ok")
} else {
Write-Host ("This student is not studying properly")
}
Output:
Recommended Articles
This is a guide to If Else in PowerShell. Here we discuss how If Else Statement works in power Shell along with different examples and code implementation. You may also look at the following articles to learn more –