• Skip to primary navigation
  • Skip to main content
  • Skip to footer

Stephanos Constantinou Blog

PowerShell Scripting

  • Home
  • Blogs
    • PowerShell Tutorials
    • PowerShell Scripts
    • PowerShell Modules
      • Modules Cmdlets
    • Software Reviews
  • About
  • Contact
You are here: Home / PowerShell Tutorials / PowerShell Logical Operators

PowerShell Logical Operators

19/04/2018 by Stephanos 2 Comments

PowerShell Logical Operators

Last time we saw the PowerShell Comparison Operators. This post will be about PowerShell Logical Operators. Logical Operators allow us to connect different statements together and/or expression. By doing so, we will be able to have one more complex expression based on different conditions and statements. The operators are very important and will allows us to create filters of the data that we retrieve from systems and also we will be able to create our custom checks of conditions. The below example uses all the logical operators that are allowed in PowerShell and some of the comparison operators that we saw in last tutorial. The example below  will return “Hello World” to the console in green color.

Example:

$a = 1
$b = 2
$c = 3
$msg = "Hello World"
$d = -not ((($a -eq 1) -and ($b -gt $a)) -or
     (($b -lt $c) -xor ($a -ge $b)))
if ($d -eq $false){
    Write-Host $msg -ForegroundColor Green}

Answer screenshot:

powershell logical operators

PowerShell Logical Operators List:

  • -and
  • -or
  • -xor
  • -not

Now lets go through the list and explain each operators along with some examples that we can use them.

And

The -and operator will return true when both statements are correct. If there are more statement and only -and operator is used, then all statements should be correct.

Examples

PS S:\> $a = 1
PS S:\> $b = 2
PS S:\> ($a -eq 1) -and ($b -eq 2)
True
PS S:\> ($a -eq 1) -and ($b -eq 3)
False
PS S:\> ($a -eq 3) -and ($b -eq 2)
False
PS S:\> ($a -eq 3) -and ($b -eq 4)
False
PS S:\>

Or

The -or operator will return true if at least one of the statements are correct. It does not matter how many statements you are using. If only one them is true then all the expression is true.

Examples

PS S:\> $a = 1
PS S:\> $b = 2
PS S:\> ($a -eq 1) -or ($b -eq 2)
True
PS S:\> ($a -eq 1) -or ($b -eq 3)
True
PS S:\> ($a -eq 3) -or ($b -eq 2)
True
PS S:\> ($a -eq 3) -or ($b -eq 4)
False
PS S:\>

Xor

The -xor operator returns true, if ONLY one statement in an expression is true. It is called exclusive or. It does not matter how many statements you are using in an expression. If more than one statement is true then the expression will return false.

Examples

PS S:\> $a = 1
PS S:\> $b = 2
PS S:\> ($a -eq 1) -xor ($b -eq 2)
False
PS S:\> ($a -eq 1) -xor ($b -eq 3)
True
PS S:\> ($a -eq 3) -xor ($b -eq 2)
True
PS S:\> ($a -eq 3) -xor ($b -eq 4)
False
PS S:\>

Not

The -not operator will turn the result of a statement to the opposite. If the return value is true for a statement then it will return false and the opposite.

Examples

PS S:\> $a = 1
PS S:\> $b = 2
PS S:\> $a -eq 1
True
PS S:\> -not ($a -eq 1)
False
PS S:\> -not (($a -eq 1) -xor ($b -eq 2))
True
PS S:\> -not (($a -eq 1) -xor ($b -eq 3))
False
PS S:\> -not (($a -eq 3) -xor ($b -eq 2))
False
PS S:\> -not (($a -eq 3) -xor ($b -eq 4))
True
PS S:\>

Summary

Logical operators help us to combine our statements and create complex expressions to test our results in a script or filter them according to our needs. Logical Operators in combination with the comparison operators two subjects that you have to understand in order to be able to write effective scripts and filters.

I hope the tutorial about logical operators is helpful.

Please let me know your comments and thoughts. You feedback is appreciated.

Related Links:

  • PowerShell Tutorials
  • PowerShell Scripts
  • about_Logical_Operators | Microsoft Docs
  • PowerShell Comparison Operators
Summary
PowerShell Logical Operators
Article Name
PowerShell Logical Operators
Description
PowerShell Logical Operators. In this tutorial we will learn all PowerShell Logical Operators, how they work and how we can use them in our scripts. Stephanos Constantinou Blog - PowerShell Scripting
Author
Stephanos
Publisher Name
Stephanos Constantinou Blog
Publisher Logo
Stephanos Constantinou Blog

Filed Under: PowerShell Tutorials Tagged With: Logical Operators, PowerShell Operators

Reader Interactions

Trackbacks

  1. PowerShell Comparison Operators - Stephanos Constantinou Blog says:
    03/05/2018 at 12:35

    […] PowerShell Logical Operators […]

    Reply
  2. PowerShell Arithmetic Operators - Stephanos Constantinou Blog says:
    18/05/2018 at 17:54

    […] PowerShell Logical Operators […]

    Reply

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Footer

Recent Posts

  • ICS Cube Product Review 26/04/2019
  • PowerShell Module SysInfo v1.2.0 15/03/2019
  • PowerShell Module SysInfo v1.1.2 13/11/2018
  • PowerShell Module SysInfo 24/10/2018
  • Get-VoltageProbe 24/10/2018
  • Get-VideoController 24/10/2018
  • Get-USBController 24/10/2018
  • Get-TrackPoint 24/10/2018
  • Get-TrackBall 24/10/2018
  • Get-TouchScreen 24/10/2018
Planet PowerShell

Categories

  • Modules Cmdlets (57)
  • PowerShell Modules (5)
  • PowerShell Scripts (38)
  • PowerShell Tutorials (35)
  • Software Reviews (2)

Archives

  • April 2019 (1)
  • March 2019 (1)
  • November 2018 (1)
  • October 2018 (56)
  • September 2018 (13)
  • August 2018 (9)
  • July 2018 (6)
  • June 2018 (8)
  • May 2018 (7)
  • April 2018 (9)
  • March 2018 (4)
  • February 2018 (6)
  • January 2018 (12)
  • December 2017 (4)
Top 10 PowerShell 2018

Blogroll

  • Planet PowerShell
  • Reddit – PowerShell
  • PowerShell Magazine
  • PowerShell.org
  • PowerShell Team Blog
  • Hey, Scripting Guy! Blog
  • Mike F Robbins
  • PowerShell Explained with Kevin Marquette
  • Mike Kanakos – Network Admin
  • The Lonely Administrator
  • AskME4Tech
PowerShell Blogs Sysadmin Blogs Banners for Top 20 Programming Blogs

© 2023 · Stephanos Constantinou Blog

  • Home
  • Blogs
  • About
  • Contact