Powershell

PowerShell else if | Explained With Examples

Each programming language has the ability to perform basic conditional operations such as if, else if, and else conditions. PowerShell is a scripting language that supports conditional operations using conditional statements (if, else-if, if-else-if).

Programming logic is the core part of any computer program. The else-if statement acts as a joining bridge to the if and the else block. Thus, the else-if condition stands to be the key player in building a logic and deciding the flow of the program.

Keeping in view the importance of the else-if statement, this post demonstrates the working and usage of the else-if statement in PowerShell.

How to Use the else-if Statement in PowerShell?

The else-if statement is used with the if statement as an option to check the second condition. Users can use it multiple times depending on the logic or requirements of the program.

Syntax

The else-if statement is provided below.

if (condition 1)
    {Statement(s)}
elseif (condition 2)
    {Statement(s)}
else
    {Statement(s)}

The else-if statement lies in between the if and else statement. The working flow of the above syntax is described as below:

  • The if block checks the first condition and executes the statements if the condition is true.
  • The else-if block tests the second condition and decides the further flow of the program.
  • While the else block is executed when neither the if nor the else condition is true.

Note: Users can use different mathematical operators, including comparison or logical operators.

Example 1: Using else-if on Predefined Value(s)

The following PowerShell code is utilized to exercise the else if statement.

Code

$RefNum = 50
  IF ($RefNum -gt 30) {
         Write-Host "VALUE $RefNum IS GREATER THAN 30."
     }
  ELSEIF ($RefNum -eq 70) {
         Write-Host "VALUE $RefNum IS EQUAL TO 30."
  }
  ELSE {
    Write-Host "VALUE $RefNum IS NOT GREATER THAN 30 NOR EQUAL TO 70"
    }

In the above code, a value is stored in a variable named $RefNum, and this variable is used in the if and else if conditions. Furthermore, the code is explained as follows:

  • The IF condition is set to “$RefNum -gt 30”.
  • If the first condition ($RefNum -gt 30) is false, the interpreter will jump to ELSEIF condition “$RefNum -eq 70”.
  • Lastly, the ELSE part of the program would be executed if none of the previously mentioned conditions were met.

To get the output, we will execute the script by providing the script’s path in the PowerShell terminal.

Output

As the ELSEIF condition stands true, and thus the statement inside the else if statement is executed.

Example 2: Using else-if on User-Defined Values

Let’s have a look at another ELSEIF example in PowerShell.

Code

$User = Read-Host -Prompt 'INPUT THE NUMBER'
  IF ($User -lt 30) {
         Write-Host "VALUE $User IS LESS THAN 30."
     }
  ELSEIF ($User -eq 50) {
         Write-Host "VALUE $User IS EQUAL TO 50."
  }
  ELSE {
    Write-Host "VALUE $User IS NOT GREATER THAN 30 NOR EQUAL TO 50"
    }

The above code is written to validate that the user’s input matches with conditions in PowerShell. The user input is stored in $User variable and the if, else-if-else blocks are used to compare the user input value.

Output

The outcome of the code shows that the ELSEIF statement is true, and the user input value 50 is equal to the ELSEIF condition. Therefore, it prints the statement “VALUE 50 IS EQUAL TO 50”.

Conclusion

The else-if statement belongs to the conditional statement category of PowerShell. These are utilized to build logic for a computer program. It is employed with an if-else statement to check the multiple conditions in a complex program. In this post, you have learned the basic concept and working of the else-if statement.

About the author

Syed Minhal Abbas

I hold a master's degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.