fbpx

Mastering PowerShell If Else: 3 Proven Techniques That Will Boost Your Programming Skills!

Windows PowerShell scripts are written with a specific job or set of tasks in mind. However, depending on one or more criteria, an operation may have more than one conceivable action. The PowerShell If Else construct can be used in this scenario.

You can include conditional logic statements in your PowerShell programs using if statements. If statements represent how people make decisions on a daily basis. When a condition is satisfied, something occurs. For instance, if you are running late for work, you will take a cab to get there on time.

Similar to if statements in other programming languages, if statements in PowerShell have the same effect.

Understanding the PowerShell If Syntax

In this graphic, if the condition is met, a certain command or statement is executed. The following command or statement is executed if the condition is false. Here is a straightforward PowerShell script.

Understanding the PowerShell If Syntax

In this instance, a variable called $BANANAS was created and given the value 8. The next step is to set a conditional statement that states to display a message if $BANANAS is less than 12. The message “You have less than a dozen BANANAS” is shown since $BANANAS has a value of 8.

After learning the fundamentals of if statements, let’s delve a little further and discuss the syntax and some more complex instances.

$BANANAS = 8

if ($BANANAS -lt 12) {
    "You have less than a dozen BANANAS."
}
$BANANAS = 8

if ($BANANAS -lt 12) {
    "You have less than a dozen BANANAS."
}
$BANANAS = 8

if ($BANANAS -lt 12) {
    "You have less than a dozen BANANAS."
}

Ways To Use PowerShell if-else Statement

  1. Using the PowerShell If-Else Statement (Single If-Else Condition)
  2. Multiple If-Else Conditions with ElseIf Statement
  3. Using conditional statements with an If Else Statement

1. Using the PowerShell If-Else Statement (Single If-Else Condition)

If statements are frequently followed by an Else statement. If the condition is not satisfied or the result is false, you can take an additional action using an otherwise statement.

Using the PowerShell If-Else Statement (Single If-Else Condition)

It is clear that there are now two statements that can be carried out. If the condition returns true, one statement, and if it does not, one statement. Here is a straightforward If-Else statement example in PowerShell. As you can notice the condition is in parenthesis and the whole if-else operator is in curly brackets.

$a = 9

if ($a -gt 3) {
    "$a is greater than 3"
}
else {
    "$a is less than 3"
}

In this instance, the variable $a has been assigned to the number 9. Then, we set the condition in our If statement to display the message “$a is greater than 3” if $a is greater than 3. Last but not least, we set our Else statement to show the warning “$a is less than 3” if the condition is false.

The screenshot shows that because $a equaled 9, the condition was satisfied. 

Using the PowerShell If-Else Statement (Single If-Else Condition)
Using the PowerShell If-Else Statement (Single If-Else Condition)

Let’s now set $a to 2, which will cause the condition to return false. Given that the condition now returns false, you can see that PowerShell is returning our Else condition, “$a is less than 3.”

$a = 2

if ($a -gt 3) {
    write-host "$a is greater than 3"
}
else {
    write-host "$a is less than 3"
}
Using the PowerShell If-Else Statement (Single If-Else Condition)
Using the PowerShell If-Else Statement (Single If-Else Condition)

2. Multiple If-Else Conditions with ElseIf Statement

You must include one or more ElseIf lines when evaluating several circumstances. The test expression and the associated code are contained in each ElseIf statement.

When the script below is run, it will continuously asking for the name of a fruit until the user types the letter “A” or presses CTRL+C.

While ($fruit -ne "A") {
    $fruit = Read-Host "Name Your fruit"

    if ($fruit -eq 'Apple') {
        'I have an Apple'
    }
    elseif ($fruit -eq 'Banana') {
        'I have a Banana'
    }
    elseif ($fruit -eq 'Orange') {
        'I have an Orange'
    }
    else {
        'Sorry, fruit is not in the database'
    }
}

The If clause will then iterate through each condition in turn until it discovers a match. The Else statement’s function would run if the input did not satisfy any of the conditions.

Here is an example of how the script from above functions when run in PowerShell.

Multiple If-Else Conditions with ElseIf Statement
Multiple If-Else Conditions with ElseIf Statement

3. Using conditional statements with an If Else Statement

If and Else statements can be nested inside of other If and Else statements using PowerShell. Nested conditional statements essentially repeat the assertion until either one is returned true or all are returned false. Conditional statements can be nested in a few different ways. The addition of a new If-Else statement just inside of an If or Else script block is one approach. 

if (condition1) {
    "condition1 is true"
}
else {
    if (condition 2) {
    "condition 2 is true"
}
    else {
        "condition 2 is false"
}
}

Utilizing the elseif statement is the second and recommended method of nesting conditional statements. This example expands on the preceding Banana example.

$BANANAS = 14

if ($BANANAS -eq 12) {
    "You have exactly a dozen BANANAS."
}
elseif ($BANANAS -lt 12) {
    "You have less than a dozen BANANAS."
}
else {
    "You have more than a dozen BANANAS."
}
Using conditional statements with an If Else Statement
Using conditional statements with an If Else Statement

Three results are possible in this case. If there are precisely 12 Bananas, one. If there are fewer than 12 Bananas, one. Moreover, if there are more than 12 Bananas, one.

You can see that we set the $Bananasvariable to 14 in the screenshot above, which caused the Else statement to return with the message “You have more than a dozen Bananas.”