Working With If Else Statement in PowerShell

The logical operators If Else and Else are used by PowerShell to check conditions. The If statement is designed to check a condition and perform a specific action associated with that condition. If the specified condition is met, then one action is performed, if not, then another.

Conditional constructs in PowerShell include operators:

  • IF
  • IF…ELSE
  • IF…ELSEIF…ELSE
  • SWITCH

Hint. When using conditionals, you have to compare something. PowerShell has a large number of comparison operators (-eq, -ne, -gt, -lt, -ge, -le, etc.).

Logical comparisons are at the heart of almost all algorithmic programming languages. In PowerShell, the If statement can only execute certain blocks of code when the specified condition is True.

Let’s look at some examples of using If Else statements in PowerShell. The simplest PowerShell construct with If statement looks like this:

$isActive = $true

if ($isActive) {

Write-Output "The value is True"

}

The commands inside the brackets {} (called ScriptBlock) are executed only if the conditions inside the if () are met.

In our example, the line if ($isActive) compares the variable with the boolean values True and False. If the condition is met (variable $isActive = $true), the code from the ScriptBlock construction – {… action…} is executed. If the condition is not met, the ScriptBlock is skipped.

powershell if then

Unlike the If statement, the Else statement is optional. The code from the ScriptBlock of the Else statement is executed when the result of the previous checks is False.

$isActive = $true

if ($isActive) {

Write-Output "The value is True"

}

else {

Write-Output "The value is False"

}

The Elseif operator is used when multiple values need to be checked. The Elseif operator can be used multiple times.

$isActive1 = $False

$company = “Theitbros”

if ($isActive1) {

Write-Output "The first condition is True"

}

elseif ($company -eq “HPE”) {

Write-Output "The first condition isn’t True"

Write-Output "The company is HPE"

}

elseif ($company -eq “Apple”) {

Write-Output "The first condition isn’t True"

Write-Output "The company is Apple"

}

else {

Write-Output "Both conditions are not met"

}

As a result of executing this script, the message “Both conditions are not met” will be displayed on the PowerShell console.

powershell if then else

If there are a lot of check conditions, it becomes inconvenient to use the If Else contract. With a large number of conditions to be checked, it is better to use the logical Switch operator, which allows you to combine a list of conditions in one construction. Switch alternately compares the tested value with each given condition, and if it finds a match, then performs the ScriptBlock action associated with this condition. The basic syntax for a PowerShell construct using a Switch statement looks like this:

Switch (value) { 
condition 1 {action} 
condition 2 {action} 
condition x {action}}
I enjoy technology and developing websites. Since 2012 I'm running a few of my own websites, and share useful content on gadgets, PC administration and website promotion.
Cyril Kardashevsky

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.