Powershell

PowerShell or Operator | Explained

Each programming language has some basic operations that are performed using conditional or logical operators. PowerShell provides a facility to make a connection between expressions and statements using logical operators. They can be used for multiple conditions by utilizing a single expression. PowerShell supports various logical operators such as or, not, and xor.

In this article, PowerShell or operator is briefly described by utilizing different examples.

  • How Does or Operator Work in PowerShell
  • How to use or Operator in PowerShell

How Does or Operator Work in PowerShell?

The or operator is a family member of the logical operators such as not, and, xor operators, and or operators. It is used in two or more conditions. The usage of the or operator is usually observed in conditional (if, if-else, if-else-if) and looping statements (For, While, Do While).

Syntax

In PowerShell, the syntax of the or operator is as follows:

<Condition 1> -or <Condition 2>

The above syntax refers to using the two conditions at a time with the help of the or operator. For more than two conditions, the following syntax can be followed.

<Condition 1> -or <Condition 2> -or <Condition 3>

How to Use or Operator in PowerShell?

For a better understanding, different examples of the or operators are provided here. To do so, we are using PowerShell ISE here to make a short script in which the usage of the or operator is shown. Let’s dig into them.

Example 1: Compare two Values

In the first example, we will show you how two values can be compared using the PowerShell or operator.

Code

$x=5
$y=20
if(($x -gt $y) -or ($y -lt 30))
{
Write-Output "CONDITION IS TRUE"
}
else
{
Write-Output "CONDITION IS NOT TRUE"
}

In the above code:

  • Two variables “$x=5” and “$y=20” are initialized.
  • These values are used to create two conditions, i.e., $x -gt $y and $y -lt 30.
  • After that, these two conditions are joined using the or operator.
  • If any of the conditions is true, the “if” part of the program will be executed; otherwise, the “else” statement will be printed.

Output

For output, execute the PowerShell script by providing the absolute path of the script as we did here:

The outcome shows that the if block of the code is executed, which states that the if-condition is true.

Example 2: Test two Conditions

Another example is used in this section to better understand the or operator in PowerShell. In this example, a bonus is given to students after checking the student attendance and their subject numbers.

Code

Write-Output "Example of using Or-Operator"
$Student_Attendance = Read-Host -Prompt "INPUT THE STUDENT ATTENDANCE"
$Subject_Number = Read-Host -Prompt "INPUT THE SUBJECT NUMBER"

if($Student_Attendance -gt 35 -or $Subject_Number -gt 50){
Write-Output "BONUS 5 MARKS IS GIVEN"
}
else
{
"Subtract 5 MARKS "
}

In the above code, two variables “Student_Attendance” and “Subject_Number” are initialized. Afterward, the user manually inputs the values into these variables. The condition is applied here: the Student_Attendance value is greater than 35, and the Subject_Number is greater than 50.

Output

Execute the PowerShell script by using the complete path of the script:

The outcome shows that the else block of the if-else statement is executed, which states both the conditions were set to false.

Conclusion

In PowerShell, or operator is used to compare multiple conditions and helps to choose the one that satisfies the specific criterion. In the first section, an overview of the or operator is described, followed by its syntax. Furthermore, a set of examples is provided to show the practical usage of the PowerShell or operator.

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.