Updated February 28, 2023
Introduction to Else If in PowerShell
If / else conditions are the most useful conditions in a scripting language and you may want to use multiple times while writing script. If the first condition in If the statement fails then the second stage is ElseIf statement. InElseIfblock you can also specify your conditions or test values. If the above two conditions fail then the last block is Else, which doesn’t require any condition to check and it will be directly executed when If and ElseIf statement fails.
Syntax
if (condition) {<Statement List 1>}
elseif (condition) {<Statement List 2>}
else {<Statement List 3>}
How to Use the IF Condition?
Below is the detail explanation of if condition:
1. Flow chart for If condition
How Does If condition works?
As shown in the above diagram, If the statement contains a condition. If condition satisfies then it executes the block. If there are multiple if statements then script checks each if statement condition and executes whichever is true.
If the statement is used to evaluate the condition. If the condition succeeds then block statement runs. You can specify more than one or more conditions in If statement, depending upon the operators used (comparison, Boolean, bitwise) statement evaluates condition.
Example
Code:
if((Get-Service Spooler).Status -eq "Running"){
Write-Output "Spooler Service is Running"
}
if((Get-Service Spooler).Status -eq "Stopped"){
Write-Output "Spooler Service is Stopped"
}
if((Get-Service Power).Status -eq "Running"){
Write-Output "Power Service is running"
}
Output:
Explanation: In the above example, two conditions (first and last) are getting satisfied so two blocks are executed. But according to programming standard multiple If statements are not recommended as it takes more execution time. Instead, we can use If / elseif / else block.
2. Nested If statement
Syntax:
if (condition) {
if (condition1) {"Execution Statement1"}
if (condition2) {
"Execution Statement2"
if (condition3) {"Execution Statement3"}
}
}
Example
Code #1: Consider the below example for nested If statement.
$srv = Get-Service Spooler
if($srv){
if($srv.Status -eq "Running"){"Service is Running"}
if($srv.Status -eq "Stopped"){"Service is Stopped"}
}
Code #2:
$srv = Get-Service Spooler
if($srv){
if($srv.Status -eq "Running"){"Service is Running"}
if($srv.Status -eq "Stopped"){"Service is Stopped"}
}
Output:
Explanation: In this example, first it checks the If Service exists, and if service it exists then it enters the block and again checks if service is Running or Stopped and executes their block accordingly. This is the perfect example of an explanation of nestedIfloop.
If /else functionality in PowerShell
Writing multiple if statements are not a good idea if the condition is already satisfied. So that we can use else statement if there is the only single condition to check.
Syntax
if (condition1) {"Execution Statement1"}
else {"Execution Statement2"}
Flowchart If / else condition
Example
Code:
Stop-Service Spooler
if((Get-Service Spooler).Status -eq "Running"){"Spooler service is running"}
else{"Spooler service is stopped"}
In the above example, Spooler service is stopped so else block is executed without further conditions check.
Output:
Nested If /else statement
Below are the example for nested if statement:
Code: #1
if (condition1) {
if (condition2) {"Execution Statement2"}
else{"Execution Statement2"}
}
else{
"Execution Statement1"
if (condition3) {"Execution Statement3"}
else {"Execution Statement3"}
}
Code: #2
$srv = Get-Service Spooler -ErrorAction Ignore
if($srv){
if($srv.Status -eq "Running"){"Spooler Service is running"}
else{"Spooler Service is not running"}
}
Else{
Write-Output "There is no service with name Spooler"
}
Output:
If / elseif /else functionality PowerShell
In the above examples, we have seen that if and else conditions are not satisfied if we have multiple if conditions, so it checks every If condition and when they are not true then else statement is executed. Here, to meet execution time criteria there is another elseif statement that we can use.
Syntax:
if (condition1) {"Execution Statement1"}
elseif (condition2) {"Execution Statement2"}
elseif (condition2) {"Execution Statement3"}
else {"Execution Statement4"}
Flowchart If / elseif / if condition
Example
Code:
$srv = Get-Service Spooler -ErrorAction Ignore
if($srv.Status -eq "Running"){
"Spooler Service is running"
}
elseif($srv.Status -eq "Stopped"){
"Spooler Service is stopped"
}
else{
"Spooler service is in Start Pending or Stopped pending status"
}
Output:
Nested If / elseif /else statement
Below are the example to nested if statement:
Code:
[int]$num = Read-Host "Enter Number"
if($num -ge 10){
if($num -lt 15){"Number is less than 15"}
elseif(($num -ge 15) -and ($num -lt 20)){
"Number is between 15 and 20"
}
else{"Number is greater than 20"}
}
elseif(($num -ge 5) -and ($num -lt 10)){
"Number is greater than 5 and less than 10"
}
else{
if($num -gt 2){"Number is greater than 2"}
else{"Number is less than 2"}
}
Output:
Multiple Conditions
You can use multiple conditions check within a single If or ElseIf statement and depends on which operators you use according to your requirement. For Example,
Code #1
$srv = Get-Service Spooler
if(($srv.Status -eq "Stopped") -and ($srv.StartType -eq "Automatic")){
"Spooler Service Starttyp is Automatic but in Stopped status"
}
else{"Service is Running or Start type is not Automatic"}
Output:
Code #2
$srv = Get-Service Spooler
if(($srv.Status -eq "Stopped") -or ($srv.Status -eq "Running")){
"Spooler Service is in Stopped or Running State"
}
else{"Spooler Service is StopPending or StartPending State"}
Output:
Like the above examples, you can use multiple or single conditional operators in If or elseif condition.
Recommended Articles
This is a guide to Else If in PowerShell. Here we discuss how to use if condition, and use multiple or single conditional operators in If or elseif condition. You can also go through our other related articles to learn more –