Updated March 24, 2023
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.
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.
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
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:
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:
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:
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:
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–