Powershell If-Elseif-Else Conditional Statement – POFTUT

Powershell If-Elseif-Else Conditional Statement


Powershell is a really powerful scripting language. It provides  a lot of modern useful programming language features. It also have basic features like conditional statements like If . In this tutorial we will look syntax and use cases of If .

If is used to check if specified condition/s are met and if the condition is met do something if not check other conditions or exit. We will look different usage types below.

Syntax

Generic syntax of the if statement is like below. Other than first if line each line optional. There may be more than one elseif line

If (condition) {Block Command}

elseIf (condition) {Block Command}

...

else {Block Command}

Single Condition

We will start with simplest usage type of If. We will only check a single condition. According to conditions status which is logical values true or false specified script will run or not. Now we will do an example.

In this example we will check if variable $a is bigger than 5 . If $a is bigger than 5 then we will print a message saying that $a value is bigger than 5.

$a=10

if ($a -gt 5){
Write-Host("$a is bigger then 5")
}

Now lets examine what this code mean.

  • $a=10 creates a numeric variable and assigns the value 10
  • if ()  checks the statement between brackets and is the condition is true the code between curly brackets { Write-Host ("$a is bigger then 5")} is executed if not nothing will be executed
  • $a -gt 5 is the most important part -gt means greater then so if we try to read this statement variable a is greater then. This statement will be used as condition for if. In this example $a is 10 and this statement is true.
LEARN MORE  Switch Case Statement in C/C++ Tutorial with Examples

Multiple Conditions

Previous example we have check single condition. But the real world consist of complex problems and we have to use complex and multiple conditions in decision making process. If can be used to provide multiple conditions by defining them in else lines.

In this example we will check variable $a whether it is bigger than 10 or not.

$a=5

if ($a -gt 10){
Write-Host("$a is bigger then 10")
}
elseif ($a -le 10){
Write-Host("$a is lower then 10")
}
  • The if statement execution will start with first if line but the variable data is 5 and not bigger than 10. So this line will be skipped.
  • The if will resume with elseif statement and as we see that the ($a -le 10) condition will return true.
  • Because of the elseif condition is true the code block of elseif will be executed.

We can add more elseif conditions than one and all of them will have same syntax but different conditions. Here an other example which have more than one elseif

$a=5

if ($a -gt 10){
Write-Host("$a is bigger then 10")
}
elseif ($a -le 10){
Write-Host("$a is lower or equal to 10 and bigger than 0")
}
elseif ($a -lt 0){
Write-Host("$a is lower then 0")
}

Else

Up to now we have defined our conditions explicitly by using if or elseif statements. What is we do not define the condition but if the previous conditions do not match we want to execute a code block? We call this else statement. Else is put as the last statement of the whole if block. If none of the previous conditions are met the last else code block is executed.

LEARN MORE  Python Boolean Variable Types

In the example if the variable $a is not positive integer we will print a warning message to the console.

$a=-4

if ($a -gt 10){
Write-Host("$a is bigger then 10")
}
elseif ($a -ge 0){
Write-Host("$a is lower or equal to 10 and bigger than 0")
}
else{
Write-Host("$a is not positive integer")
}

Leave a Comment