EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • 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 If Statement in PowerShell

If Statement in PowerShell

Priya Pedamkar
Article byPriya Pedamkar

Updated March 24, 2023

If Statement in PowerShell

Introduction to If Statement in PowerShell

The If Statement in PowerShell allows the programmer to control the flow of execution of the program, the” IF” statement defines if a program has to execute a section of code or not, based on whether a given condition expression is correct or not. Here correct in programming terms true or false. One of the main uses of if statement allows the program to make the decision on the basis of one or more conditions.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

And if a statement is based on the boolean value, if the boolean condition value is true than inside the if block and execute the lines of statements inside if block. In another simple word, To execute any statements it has to test one or multiple conditions if conditions are true it will execute the statement. “If” statement needed when we wanted to check any specific case.

Introduction to If Statement in PowerShell

What is PowerShell?

PowerShell is a way to write Administrative commands on Windows environments, it is similar to bash scripting in Linux. It provides a way to automate the Windows operating system and its applications to deal with various tasks.PowerShell is available for both Windows and Linux operating systems, but in this tutorial, we are focusing on windows. In the below image we can see how Powershell controls all Automation works.

Syntax:

Syntax of if in PowerShell is very much similar to other programming languages. It checks for the condition if the condition expression is true it will be got to if block if the condition expression is false it will go to else.

if(condition) {
// Executes when the condition is true
}else {
// Executes when the condition is false
}

We can also use elseif, syntax below.

if(condition 1) {
// Executes when the condition 1 is true
}elseif(condition 2) {
// Executes when the condition 2 is true
}elseif(condition 3) {
// Executes when the condition 3 is true
}else {
// Executes none of the condition is true.
}

Flow diagram

Flow diagram

In the below flow diagram we can see when execution starts, it first checks the condition. If the condition is true then it will go to the statement block. Here conditions can be one or multiple. Any condition other than zero, false, blank are considered as true only .for example if any conditional expression gives an output of 0,”, false all these are considered as false statements.

How does if statement work in PowerShell works?

if (<cond1>)
{<statement1>}
[elseif (<cond2>)
{<statement2>}] [else
{<statement3>}]

Here, when it starts execution it checks for cond1 as if it is true or false, based on the value it will execute the statement block if cond1 is true it will execute statement1 and PowerShell exit. But if cond1 is false, then it will check else if block cond2, if cond2 is true then statement2 will be executed. If cond1 and cond2 both are false or none of the condition is true then else statement will be executed.

The condition can be one or multiple, for example.

if (<condition 1 -or condition 2>)
{<statement1>}
[elseif (<condition 3 -or condition 4>)
{<statement2>}] [else
{<statement3>}]

Examples for If Statement in PowerShell

The examples of If Statement in PowerShell is given below:

Example #1 – Simple if-else example

Code:

$x = 40
if($x -le 20){
write-host("value of x is less than 20")
}else{
write-host(“value of x is greater than 20”)
}

Output:

if statement in powershell for Examples 1

Explanation: The above code is checking the value of $x, if it is less than 20 or not, if the value of $x is less than 20 it will execute if the statement block.

Example #2 – with Multiple conditions

Code:

$day = (get-date).dayofweek
if(($day -ne "Saturday") -or ($day -ne "Sunday")){
write-host("Welcome to Our Banks")
}else{
write-host(“Hello friends , Banks are closed today”)
}

Output:

if statement in powershell for Examples- Multiple conditions

Explanation:

The above code will print output according to today. Here it matches case the value of $day, so if a day is Sunday or Saturday, it will print  “Hello friends, Banks are closed today” and if it’s other than Sunday and Saturday it will print “Welcome to Our Banks”.

Example #3 – if-else if conditions

Code:

$occupation =”engineering”
if($occupation -eq "engineering"){
write-host("engineer")
}elseif($occupation -eq "sales"){
write-host("sales")
}else{
write-host("accounting")
}

Output:

 if-else if conditions

Explanation:

In the above code, it checks for $occupation value, if it is equal to engineering than print engineering if the value if $occupation is sales then it will print sales, and if $occupation is none then it will print accounting.

Example #4 – Functional Based

Code:

function check ($VALUE) {
if ($VALUE) {
Write-Host(“TRUE”)
} else {
Write-Host(“FALSE”)
}
}
check $FALSE
check $TRUE
check FALSE //FALSE is a String with length > 0
check TRUE //TRUE is a string length >0

Output:

Functional Based 

Note: In PowerShell String with length, more than zero is considered as true.

Explanation:

In the above example first, it calls for check function with parameter  $TRUE, and inside function check, it checks for $VALUE and if it is true it will print TRUE, in a similar way again check-called with parameter $FALSE and it check $VALUE and as it is false it will go to the else block.

Let me explain to you some real use cases where we needed “if”.

If we want to check if the database server is up or down(checking if the database is running or not) then we can use if statement. So if the database is down then run any service or command to up database. This will be completely automated which will check all the time database status.

Code:

$x = Dbconnection()//$x is true or false
if($x){
write-host("Start database services process")
//run some script to handle a situation
}

Many times because of the heavy load on a server it stops working, so to check the threshold load capacity of the server we can use Powershell if condition and inside condition we can write our required statements.

Code:

$x = loadValue()//200000 is threshold load capacity
if($x -le 200000){
write-host("Load is ok")
}else{
write-host("Load is exceed threshold capacity")
//write conditions for handling conditions .
}

With the above example, we are clear that if a statement can play a very crucial role in the real software world.

Recommended Articles

This is a guide to If Statement in PowerShell. PowerShell IF is a very powerful tool to handle conditional statements Here we discuss what is PowerShell? how does if statement works in PowerShell? along with respective examples and flow chart. You can also go through our other related articles to learn more–

  1. If Else in PowerShell
  2. Hashtable in PowerShell
  3. Powershell Write-Host
  4. PowerShell Run Command
  5. Complete Guide to Else If in C++
  6. Methods and Constructor of Hashtable in Java
ADVERTISEMENT
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
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
  • Blog as Guest
Courses
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - 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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

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

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

Forgot Password?

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW