PowerShell for Windows Admins

May 26 2019   3:37PM GMT

Exclusive OR

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Tags:
Powershell

PowerShell has a number of logical operators – – -and, –or, –not (or !). One I’ve really thought about is the exclusive OR operator –xor.

With a standard –or operator the result is TRUE if one of the statements is TRUE

PS> (‘a’ -eq ‘A’) -or (‘a’ -eq ‘z’)
True

The standard –or operator is also TRUE if both statements are true

PS> (‘a’ -eq ‘A’) -or (‘Z’ -eq ‘z’)
True

The exclusive OR is only TRUE when one of the statements is TRUE and the other is FALSE. So this returns TRUE

PS> (‘a’ -eq ‘A’) -xor (‘a’ -eq ‘z’)
True

But when both statements are TRUE it returns FALSE

PS> (‘a’ -eq ‘A’) -xor (‘Z’ -eq ‘z’)
False

The exclusive OR seems like bit of an oddity to me.

PowerShell has a bitwise xor operator — -bxor but outside of that I’m struggling to find a use for –xor. Certainly as an admin I can’t think of a situation where I’d use it.

Just out curiosity have you used –xor and if so how?

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: