Powershell

if-else Statements in PowerShell

In programming as well as scripting languages including PowerShell, experiencing decision-making scenarios or dealing with different conditions are very common. PowerShell deals with such situations using conditional statements/expressions such as if, else-if, etc. These decision-making statements are responsible to manage the program’s flow based on different conditions. Most of the time, “If” and “else” statements are used collectively so that the script must run in every case.

This write-up will present a thorough guide for the if-else statements:

  • What is if-statement in PowerShell?
  • Syntax of if-statement.
  • What is if-else statement in PowerShell?
  • Syntax of if-else statement.

So let’s get started!

What is if-statement in PowerShell?

The if statement in PowerShell takes an expression/condition in its parenthesis and tests it. Consequently, it will return either a true or false value, if the specified condition is true then the code-block associated with the if-statement will get executed. The if-statement deals with the true condition only, it has nothing to do with the false condition.

Syntax of if-statement

The below-given snippet shows the basic syntax of if-statement in PowerShell:

if(expression/condition) {
   // Executes only if the given expression is true
}

Let’s consider the below script to understand the working of if-statement in PowerShell:

$a =12;
$b =15;
if($a -le $b) {
  write-host("a is less than or equal to b");
}

In this example program, we utilized the if-statement to test an expression, if the returned value is true then the body of if-statement will execute else not:

The output verified the working of the if-statement.

What if the returned value of the specified expression is false? How if-statement will deal with the false value?

$a =12;
$b =15;
if($a -ge $b) {
  write-host("a is less than or equal to b");
}

The above script will generate the following output:

The cursor moved to the next line without performing any specific task. It verified that the if-statement doesn’t handle the false conditions.

What is if-else statement in PowerShell?

To tackle the false conditions, the else statement can be used along with the if-statement. In Powershell, if we utilized the combination of if and else statements, as a result, both true and false conditions will be tackled.

Syntax of if-else statement
The below snippet depicts the basic syntax of the if-else statement in PowerShell:

if(test-condition/expression) {
   // Executes only if the given expression is true
}
else{
    // Executes if the specified expression is not true
}

How to use if-else statement in PowerShell
Below snippet will assist you in this regard:

$a =20;
$b =15;
if($a -le $b) {
  write-host("a is less than or equal to b");
}
else{
  write-host("a is greater than b");
}

This time we utilized both if and else statements, now if the value of a is less than or equal to the b then body of if-statement will execute otherwise the body of else-statement will execute:

The above snippet verified that the else-statement was executed because the specified condition was false.

Conclusion

In PowerShell, decision-making statements such as if, else, and else-if are used to manage the program’s flow based on different conditions. The if-statement deals with the true condition only while the else-statement deals with the false condition only. Therefore, in PowerShell, if and else statements can be used combinedly to handle both true and false conditions. This write-up explained all the basics of if and else conditions in PowerShell using some suitable examples.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.