Exclusive OR

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?

This entry was posted in Powershell Basics. Bookmark the permalink.

1 Response to Exclusive OR

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s