PowerShell – Logical Operators

PowerShell - Logical operators

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.

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:

  • operand1 with False value since comparing Numbers 1 and 2 yields False.
  • operand2 has a value of True since the comparison of the Strings ab and ab return True.

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.

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.

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.

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 operand1 returned True because the expression was False.
  • Negation of operand2 returned False because the expression was True.

Use Multiple Logical Operators

To perform multiple Logical operations on a value, use brackets that help identification.

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 result True because its expression was False.
  • The operand2 and the negation of operand1 were subjected to a logical AND operation, which produced the result True because both operands’ values are True.
  • Applied the logical EXCLUSIVE OR operation to operand2 and the result of the aforementioned AND operation. Given that both operands have values of True, False was returned.

Was this post helpful?

Leave a Reply

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