PowerShell logical operators are used to connect the statements and expressions. It is beneficial to test multiple conditions using a single expression. More specifically, PowerShell supports logical operators including “-and”, “-or”, “-xor”, “-not”, and “!”. The “-or” operator is utilized to connect/join two conditions, or statements. If either or both operands are “True”, it returns “True”, otherwise “False”.
This post will cover a detailed guide about the “Or” statement.
How to Use PowerShell “Or” Statement?
The “Or” statement is the one in which two conditions are connected using it. As stated earlier, the “Or” statement returns “True” only when both the conditions are, or either of them is evaluated “True”, else the output will be “False”.
Examples related to the “Or” statement are discussed with different scenarios below.
Example 1: Use of the “-Or” Operator When One of the Conditions is True
This illustration will connect the two conditions using the “-Or” operator and then output the result in Boolean value:
$b = 4
($a -eq $b) -or ($a -lt $b)
According to the above code:
- First, initialize two variables “$a” and “$b”.
- Assign them two different values, then specify two conditions using comparison operators and connect them using the “-or” operator:
The resultant output is “True”, as one of the conditions is true (The value of “a” is less than “b”).
Example 2: Use of the “-Or” Operator When Both of the Conditions are True
In this example, two true conditions will be connected using the “-or” operator:
$b = 4
$c = 2
($a -eq $c) -or ($a -lt $b)
According to the above code:
- First, initialize three variables with different values assigned to them.
- After that, create the two conditions which are true and connect both of them using the “-or” operator:
The resultant output is true because both conditions are evaluated as true.
Example 3: Use of the “-Or” Operator When Both of the Conditions are False
This demonstration will test the two false conditions connected using the “-or” operator:
$b = 4
$c = 2
($a -eq $b) -or ($a -lt $c)
According to the above code, the two false conditions are connected using the “-or” operator.
The output is “False”, as both of the statements or conditions are false.
Conclusion
The PowerShell “Or” statement is the one in which two conditions are connected using the “-Or” operator. The “Or” statement is “True” only when either of the conditions or both of them are “True”, else the output will be “False”. This write-up has discussed the “Or” statement with every possible scenario.