PowerShell
Introduction : 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 toward windows . In the below image we can see how Powershell controls all Automation works.
if statement in PowerShell
Introduction: The if statement allows the programmer to control the flow of execution of program ,”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 it allow program to take decision on the basis of one or more conditions. An if 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.
Syntax :
Syntax of if in PowerShell is very much similar to other programming languages . It checks for condition ,if the condition expression is true it will 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 start , it first checks condition if condition is true than it will go to 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 output of 0,”” , false all these are considered as false statements .
In the below flow diagram we can see when execution start , it first checks condition if condition is true than it will go to 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 output of 0,”” , false all these are considered as false statements .
How if statement in PowerShell:
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 than statement2 will be executed .If cond1 and cond2 both are false or none of condition is true than else statement will be executed .
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 :
Simple if else example,
if($x -le 20){
write-host("value of x is not less than 20")
}else{
write-host(“value of x is greater than 20”)
}
Output: value of x is greater than 20
Explanation : Above code is checking the value of $x , if it is less than 20 or not , if value of $x is less than 20 it will execute if statement block .
Example with Multiple conditions ,
$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”)
}
Explanation : Above code will print output according to day .Here -ne matches case the value of $day , so if 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 with if else if conditions ,
$occupation =”engineering”if($occupation -eq "engineering"){
write-host("engineer")
}elseif($occupation -eq "sales"){
write-host("sales")
}else{
write-host("accounting")
}
Explanation: In the above code it checks for $occupation value , if it is equal to engineering than print engineering ,if value if $occupation is sales than it will print sales, and if $occupation is none than it will print accounting.
functional based example,
function check ($VALUE) {
if ($VALUE) {
Write-Host(“TRUE”)
} else {
Write-Host(“FALSE”)
}
}
Calling function ,
check $FALSEFALSE
check TRUE //TRUE is a string length >0
TRUE
check FALSE //FALSE is a String with length > 0.
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 similar way again check called with parameter $FALSE and it check $VALUE and as it is false it will go to the else block .
With the above example we are clear that if statement can play a very crucial role in real software world .
Conclusion
PowerShell IF is a very powerful tool to handle conditional statements ,I hope I was able to simplify IF in PowerShell.
No comments:
Post a Comment