EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial Else If in PowerShell
 

Else If in PowerShell

Chirag Nagarekar
Article byChirag Nagarekar
EDUCBA
Reviewed byRavi Rathore

Updated February 28, 2023

Else If in PowerShell

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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

Flowchart1

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:

two conditions

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:

Service exists

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

Flowchart2

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:

Else If in PowerShell - 3

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:

Else If in PowerShell - 4

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

Flowchart3

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:

Else If in PowerShell - 5

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:

Nested If

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:

multiple conditions

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:

multiple conditions

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 –

  1. PowerShell Switch Statement
  2. PowerShell Wildcards
  3. PowerShell Grep
  4. PowerShell Rename Folder

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - ENROLL NOW