Table of Contents
Using Logical Operators in PowerShell
PowerShell’s logical operator is a potent tool for comparing data and carrying out operations depending on those comparisons. It allows the user to design an if-then scenario in which several parameters are compared. In other words, it enables the users to make choices following the criteria they specify.
A logical operator is a PowerShell cmdlet that performs Boolean operations on two or more values to produce complicated conditional statements. The logical operators with the most significant usage are:
- Logical AND.
- Logical OR.
- Logical EXCLUSIVE OR.
- Logical NOT.
These operators will return a value of True or False depending on the outcome of the operation.
Use Logical AND Operator
To perform the Logical AND operation on two or more values, use the -and operator between the operands.
|
1 2 3 4 5 |
$operand1 = (1 -eq 2) $operand2 = ('ab' -eq 'ab') $operand1 -and $operand2 |
|
1 2 3 |
False |
In PowerShell, the -eq operator is a comparison operator that checks if two values are equal. If the values are equal, it returns True; otherwise, it returns False. We used the -eq operator to create two operands:
operand1withFalsevalue since comparing Numbers1and2yieldsFalse.operand2has a value ofTruesince the comparison of the StringsabandabreturnTrue.
The -and operator performs a logical AND operation. It returns True if both operands are True; otherwise, False. For example, we used the -and operator to compare operand1 and operand2, which returned False because operand1 was False.
Use Logical OR Operator
To perform the Logical OR operation on two or more values, use the -or operator between the operands.
|
1 2 3 4 5 |
$operand1 = (1 -eq 2) $operand2 = ('ab' -eq 'ab') $operand1 -or $operand2 |
|
1 2 3 |
True |
We created operands as we did while using the -and operator. In addition, we used the -or operator in this section. It performs a logical OR operation and returns True if either of the operands is True; otherwise, False.
For example, we used the -or operator to compare operand1 and operand2. It returned True because operand2 was True.
Use Logical EXCLUSIVE OR Operator
To perform the Logical EXCLUSIVE OR operation on two or more values, use the -xor operator between the operands.
|
1 2 3 4 5 |
$operand1 = (1 -eq 2) $operand2 = ('ab' -eq 'ab') $operand1 -xor $operand2 |
|
1 2 3 |
True |
The -xor operator performs logical EXCLUSIVE OR operation and returns True if exactly one of the expressions is True; otherwise, False. For example, we used the -xor operation on operand1 and operand2 that returned True because exactly one, i.e., operand2, is true.
Use Logical NOT Operator
To perform the Logical NOT operation on a value, use the -not or (!) operator between the operands.
|
1 2 3 4 5 6 |
$operand1 = (1 -eq 2) $operand2 = ('ab' -eq 'ab') -not ($operand1) ! ($operand2) |
|
1 2 3 4 |
True False |
This -not operator negates the value of the expression. If the expression is True, it returns False, and if the expression is False, it returns True. We used the -not operator to negate the values of both operands.
- Negation of
operand1returnedTruebecause the expression wasFalse. - Negation of
operand2returnedFalsebecause the expression wasTrue.
Use Multiple Logical Operators
To perform multiple Logical operations on a value, use brackets that help identification.
|
1 2 3 4 5 |
$operand1 = (1 -eq 2) $operand2 = ('ab' -eq 'ab') (-not($operand1) -and $operand2) -xor $operand2 |
|
1 2 3 |
False |
Remember that the operators -and, -or, and -xor take precedence over -not. Use parentheses to indicate the order of operations. We used multiple logical operators:
- Used negation on
operand1, which gave the resultTruebecause its expression wasFalse. - The
operand2and the negation ofoperand1were subjected to a logical AND operation, which produced the resultTruebecause both operands’ values areTrue. - Applied the logical EXCLUSIVE OR operation to
operand2and the result of the aforementioned AND operation. Given that both operands have values ofTrue,Falsewas returned.