Powershell

How to Use If-Else Conditions in PowerShell

PowerShell Scripts can make decisions by utilizing a concept known as conditional logic. The “if” and the “if-else” statements are commonly used to define conditions. Suppose that in your mind, you have developed a PowerShell script for a specific task. However, depending on one or more conditions, an operation may have many alternative actions. In this case, the PowerShell if-else statements are quite useful.

Today, you will discover the if-else conditions, it’s working, and its usage in PowerShell. We will show the ways you can utilize PowerShell if-else for adding conditional logic handling to your scripts. So, let’s get this journey started!

What are If-Else Conditions in PowerShell?

The if-else condition is used to execute a code block based on specific conditions that need to be true. You can also add more than one condition to your script. Furthermore, an else block can be defined to execute if all conditional statements are tested false.

Syntax of If-Else Structure in PowerShell

Here is the syntax of if-else condition:

if(expression) {
   // Executes this part when the expression is true
}else {
   // Executes this part when the expression is false
}

First of all, the program will check the if-condition; if it is “true,” then the code written inside the body of the if block will be executed. Otherwise, if all “if” conditions are “false”, the program will move forward to the “else” block, and will execute its code block.

Working of If-Else Condition in PowerShell

There exist three possible statements in the if-else condition statements:

  • The if-condition is placed in the round brackets after the “if” and curly brackets {} contain the part of the program that is going to be executed if the condition is true.
  • You can add an else-if block if you want to check multiple conditions.
  • Else-statement does not test any condition. The statement present inside the body of the other part will be executed if all the conditions are false.

Executing a Simple If-Else Condition in PowerShell:

We will start this section by executing a simple if condition. For that, we have to create a PowerShell Script. Open your Windows PowerShell ISE and create a new file.

Now, add the following code to your script. We have saved the file as “testfile2.ps2”, you can name it as you want.

$number = 5
if ($number -gt 4) {
    "$number is greater than 4"
}

The above-given script will check if the value of the “$number” variable is greater than 4. If this condition is true, the output will print out the statement “$number is greater than 4” on your PowerShell. In the other case, if the condition tends to be false, then no operation will be performed because we have not added any “else” condition here.

As we have defined the “$number” variable value as “5” which is greater than “4,” which makes our “if” condition true. We will have the following output:

Now, we will check out the other block for the same scenario. The below-given script will print out “$number is greater than 9” when the condition placed in the “if” block is true.

Otherwise, it will show “$number is NOT greater than 9” by executing the “else” block. Remember, we have already assigned the “8” value to our $number variable. Here, “-gt” is representing the “greater than” operator.

$number = 8
if ($number -gt 9)
{
    "$number is greater than 9"
}
else
{
    "$number is NOT greater than 9"
}

In the scenario mentioned above, the “if” condition is false. So, the script will execute the statement present in the “else” block as follow:

Nested If-Else Conditions in PowerShell

If your program needs to test nested conditions, then the If-else statement can handle multiple if-else or if/else if/else conditions as well. We will demonstrate this concept by using an example. Suppose you want to test any user input based on multiple conditions. To do so, define your conditions with the help of if-else if-else statements. Add as many else-if blocks as required.

We have created a script to test the fruit name entered by the user. “Read-Host” will take the user input and store it in the “$fruit variable.” Here, “-ne” indicates the “not equal” operator, and “-eq” is for the “equal” operator. We have defined three conditions separately in the “if” block statement and the other in the “else-if” block. If none of the specified conditions are true, PowerShell will execute the “else” statement block.

While ($fruit -ne "X") {
    $fruit = Read-Host "Enter any fruit name"

    if ($fruit -eq 'Apricot') {
        'I have an Apricot'
    }
    elseif ($fruit -eq 'Strawberry') {
        'I have a Strawberry'
    }
    elseif ($fruit -eq 'Peach') {
        'I have an Peach'
    }
    else {
        'Sorry, your entered fruit is not in the list'
    }
}

Save this “testfile2.ps1” script and execute it.

You will be asked to enter any fruit name. Enter a fruit of your choice. The program will take your input and match it to the added conditions in the “if” and “else-if” blocks. Whenever you enter a fruit name that does not match the defined condition, the script will execute the “else” block by printing out “Sorry, your entered fruit is not in the list” on your PowerShell screen.

Conclusion

As part of flow control in any program and script, every programming and scripting language has some sort of ability to perform a conditional operation using an if-else statement. PowerShell is also capable of executing if-else conditions.

This post covered the basics of if/else/else if conditional statements in PowerShell. Now, you can utilize the if-else statement for creating conditional logic and direct your scripts operations based on the execution of conditions.

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.