PowerShell -OR Operator Explained: Syntax And Examples

Do you want to use the PowerShell -or operator but don’t know how? This guide teaches you all you need to know about the -or logical operator.

The first section of the guide gives you a quick overview of this popular PowerShell logical operator. Then, in the second section, you’ll learn the syntax of the -or operator.

Once you know the syntax, I will show you multiple examples of the PowerShell -or operator. There is also a section where I answer questions that most users ask about the -or operator.

PowerShell -OR Operator: Overview

PowerShell -Or Operator: Overview

The -or operator belongs to the group of logical operators. The other logical operators are -AND, -XOR, -NOT and !.

These PowerShell operators create conditional statements. In addition to connecting simple conditional statements, you can also use these logical operators to create complex conditional statements.

Specifically, you can use the logical -OR operator to check if EITHER two conditional statements are true. On the contrary, if you need to confirm if BOTH the conditional statements are true, use the logical -and operator.

The beauty of logical operators is that you use them to connect multiple conditional statements.

Like other logical operators, the logical -OR operator evaluates only the statements required to determine the truth value of the statement. More in the examples section of this guide.

Syntax Of The PowerShell -OR Operator

The syntax of the -or operator is…

<statement> -OR <statement>

The syntax of all other logical operators follows the same pattern. Below are the syntaxes of all other PowerShell logical operators.

<statement> -AND <statement>
<statement> -XOR <statement>

You would have noticed that I did not include the other two logical operators – !, -NOT – in the last set of syntaxes. The reason for this is that these two operators use a different styles of syntax.

Here are the syntaxes of ! and -NOT operators:

! <statement>
-NOT <statement>

In any of these syntaxes, <statement> represents PowerShell statements or commands.

PowerShell -OR Operator Examples

PowerShell -OR Operator Examples

So farm you know a bit about the logical -or operator. In addition to that, you have learned about the syntax of this simple but versatile operator, it is time to see some applications.

In the next sub-sections, I will show you various ways to use the logical -or operator. The examples not only show you how to use the logical -or, but you’ll also learn how to combine -or with other operators.

How To Use PowerShell -OR Operator In IF Statements

One of the common situations you’ll need to use the -or operator is in IF statements. If you don’t know how the IF statement works, I strongly recommend you learn about it before proceeding.

To learn about IF statements and how to use them, read my article – Powershell If Else Explained: Syntax and Examples.

Ok, let’s now show you how to use PowerShell logical -or operator in IF statement. The general syntax of the IF statement is…

IF (Condition is true)
{Execute the codes here}

The IF statement has a condition test part. This is where you use the logical -or operator. So, here is the syntax of the -or operator in an IF statement:

IF (<statement> -OR <statement>)
{Execute the codes here}

In the last updated syntax, I replaced “Condition is true” with “<statement> -OR <statement>”

In essence, when you use the PowerShell logical -or operator in an IF statement, the IF statement will only execute the commands in its {} block if the -or condition returns TRUE.

Here is a quick example…

IF ( (1 -gt 0) -OR (2 -lt 3)) {

Write-Host "If you see this, it means that ONE of the statements in the condition block of the IF statement is TRUE" -foregroundcolor red

}

In the overview section of this guide, I said that you use logical operators to connect multiple conditional statements. This is what I have done in the script I created above.

Now, let’s break down the statements in the conditions test block of the IF statement

(1 -gt 0) -OR (2 -lt 3)

In English, the above PowerShell -or statements says “1 greater than 0″ OR “2 less than three”. In essence, if ONE of the statements “1 greater than 0″ OR “2 less than three” is TRUE, the result of the command will be TRUE.

If this happens, the statements in the {} block will be executed. Otherwise, they will not. Now, let’s see the result of the IF statement…

IF ( (1 -gt 0) -OR (2 -lt 3)) {
Write-Host "If you see this, it means that ONE of the statements in the condition block of the IF statement is TRUE" -foregroundcolor red
}

Here is the result in PowerShell ISE…

How To Use PowerShell -Or Operator In IF Statements

Clearly, the result of at least ONE of the statements is TRUE. This is why the Write-Host command in the {} block of the IF statement is executed.

How To Use PowerShell -OR And -NOT Operators

As I already explained, the PowerShell -NOT operator is one of the logical operators. Also, as I already discussed earlier, the syntax of the -NOT operator is:

-NOT <statement>

In this example, I’ll show you how to use the -OR and -NOT operators in a PowerShell statement. To demonstrate this example, let’s use the PowerShell statement below:

(Test-Path "D:\ReportFolder\Tempfolder1") -OR -NOT(Test-Path D:\ReportFolder\Tempfolder4)

The result of the above statement is…

How To Use PowerShell -OR And -NOT Operators

Now, let’s see how we arrived at this result…

The above PowerShell statement uses Test-Path to check if the paths “D:\ReportFolder\Tempfolder1” and “D:\ReportFolder\Tempfolder4” exist.

Firstly, let’s see if the paths exist on my computer…

The results show that the first path, “D:\ReportFolder\Tempfolder1” does not exist (the Test-Path command returned False). On the contrary, the Test-Path command confirms that the path “D:\ReportFolder\Tempfolder4” exist.

So, why did the original -OR, -NOT statement return False?

To answer that question, let’s introduce the -NOT operator to that part of the PowerShell statement…

-NOT(Test-Path D:\ReportFolder\Tempfolder4)

The result is False

How To Use PowerShell -OR And -NOT Operators

So, what changed the output of the Test-Path command from True to False? The -NOT operator!

The -NOT operator, or logical -NOT operator reverses the output of a command or PowerShell statement.

This takes us to why the output of our original command retired False.

(Test-Path "D:\ReportFolder\Tempfolder1") -OR -NOT(Test-Path D:\ReportFolder\Tempfolder4)

The first statement, (Test-Path “D:\ReportFolder\Tempfolder1”) returned False, because the path does not exist. However, even though the second statement, (Test-Path D:\ReportFolder\Tempfolder4) returned True, its final result is False.

As I already explained, the -NOT operator reversed the result from True to False.

Based on all these explanations, this is why the final result is a False. As you already know, the -OR operator checks if either statement it connects returned True.

If any of the statements on either side of the -OR operator returns True, the overall statement returns True. Otherwise, it returns False (as seen in this example).

Example Of PowerShell -AND Or -OR Operator Precedence

While researching examples to discuss for this guide, I came across this Stackoverflow.com question: why does the PowerShell statement below return False instead of True?

$true -or $false -and $false

Then, the question contributor asked a follow-up question: According to Microsoft’s about_Operator_Precedence, -AND should take precedence over -OR.

So, let’s use this question to examine how PowerShell treats -AND and -OR operators in terms of precedence.

Going back to the Stackoverflow.com question, why does this statement produce False instead of True?

$true -or $false -and $false

Before I answer this question, let me state categorically that the about_Operator_Precedence link that the question contributed referenced says that -AND and -OR operators have equal precedence.

To help you understand precedence order in PowerShell, I extracted the following from about_Operator_Precedence link:

  1. PowerShell evaluates operators with equal precedence as they appear in a statement, from left to write.

So, in our example…

$true -or $false -and $false

PowerShell evaluates “$true -or $false” first. The result of “$true -or $false” equals True. Then, it combines the result, True ($true) with the next expression (-and $false)…

$true -and $false

The result of “$true -and $false” is False. So, the final result of “$true -or $false -and $false” is False.

  1. Another important piece of information from the previous link is this: if you want to override the standard PowerShell precedence order, use parentheses “()”.

So, if we want to change the result of the statement…

$true -or $false -and $false

We use parentheses “()”. For example, if I want PowerShell to evaluate the last two statements ($false -and $false) first, I’ll enclose them in a parentheses “()”…

$true -or ($false -and $false)

So, since the parentheses “()” force PowerShell to evaluate the expression, ($false -and $false) first, let’s see the result…

$false -and $false

Obviously, the result is False. Next, PowerShell will combine the result of the above statement (which is False) with “$true -or”. The final expression will now become…

$true -or $false

The result of the statement “$true -or $false” is True. So, the final result of the statement – $true -or ($false -and $false) – is True. In comparison, the result of – $true -or $false -and $false – is False.

How To Use PowerShell -OR Operator In A DO-WHILE Statement

If you’ve never used a DO-WHILE statement, here is the syntax:

do {<statement list>} while (<condition>)

As you can see from the syntax above, the WHILE block of the DO-WHILE statement contains a condition. This is where you can add an -OR operator.

To demonstrate this, here are two PowerShell DO-WHILE statements:

$x = 1,2,3,0
do { $count++; $a++; } while (++$count -lt 10)
$count
$x = 1,2,3,0
do { $count++; $a++; } while (++$a -lt 10)
$count

In the statement below, I introduced the PowerShell -OR operator in the WHILE block of the DO-WHILE…

$x = 1,2,3,0
do { $count++; $a++; } while ((++$count -lt 10) -OR (++$a -lt 10))
$count

It is important to note that, apart from the WHILE block parentheses “()”, I enclosed the individual statements connected by the -OR operator in parentheses “()” as well.

In the above statement with the -OR operator, the DO statement loop will stop when either statement in the WHILE block returns True.

How To Use PowerShell -OR Operator In Where-Object Statements

The Where-Object selects objects from a collection based on a specified property filter. So, to select objects from a collection of objects, the Where-Object evaluates a condition or set of conditions.

If you want the Where-Object to evaluate a set of conditions and return results if ONE of the conditions is TRUE, use the logical -OR operator to connect the conditions.

For example, if I want to return just the “ACCSvc” from the Get-Service command, I’ll use the command below.

Get-Service | Where-Object {$_.Name -eq "ACCSvc"}
How To Use PowerShell -OR Operator In Where-Object Statements

However, if I want to return two services, “ACCSvc” and “defragsvc”, I’ll use the PowerShell -OR operator to connect two conditions in the Where-Object…

Get-Service | Where-Object {($_.Name -eq "ACCSvc") -OR ($_.Name -eq "defragsvc")}

Here is the result in PowerShell.

How To Use PowerShell -OR and -AND Operator In Where-Object Statements

In the last example, I showed you how to use the PowerShell -OR operator in Where-Object. However, in this example, I will show you how to combine the -OR and -AND operators in a Where-Object statement.

Specifically, I want to return all instances of the svchost and chrome process with handles over 500. But first, let’s return all instances of the svchost and chrome process.

Get-Process -Name svchost,chrome

As you can see, the first column of the result displays the process handles.

How To Use PowerShell -OR and -AND Operator In Where-Object Statements

As I said earlier, I want to display all svchost and chrome processes with process handles greater than 500. Here is the command that will accomplish this task.

Get-Process | Where-Object {($_.Name -eq "svchost") -OR ($_.Name -eq "chrome") -AND ($_.handles -gt 500)}

The result of this command is shorter. The reason is that the command returned only the processes with handles greater than 500.

Finally, before I move on from this example, let me show you one more cool stuff. If you wish to order the result by process handles, pipe the output of the last command to Sort-Object.

Then, use then sort by the handles property. Here is the command.

The screenshot of the result is shown below the command.

Get-Process | Where-Object {($_.Name -eq "svchost") -OR ($_.Name -eq "chrome") -AND ($_.handles -gt 500)} | Sort-Object -Property Handles

How To Use PowerShell -OR Operator In Get-ChildItem Command

One way to use the -OR operator in Get-ChildItem command is with the Attributes parameter. You can use the Attributes parameter to fetch files and folders with the attributes you specify.

For example, if I want to return only directories (folders) from the path, “D:\PowerShell Scripts”, I’ll run the command below…

Get-ChildItem "D:\PowerShell Scripts" -Attributes Directory

Now, if I want to return folders and zipped files (Archive files), I will combine the two attributes (Directory and Archive) with the -OR operator. However, in the Get-ChildItem’s Attributes parameter, -OR is specified as a comma (,).

Here is an example…

Get-ChildItem "D:\PowerShell Scripts" -Attributes Directory,Archive
How To Use PowerShell -OR Operator In Get-ChildItem Command

Moving on, a more popular way to use the PowerShell -OR operator in Get-ChildItem command is to pipe Get-ChildItem to Where-Object.

Here is the PowerShell statement that amends the last command using the -OR operator in a Where-Object…

Get-ChildItem "D:\PowerShell Scripts" | Where-Object {($_.Attributes -eq "Directory") -OR ($_.Attributes -eq "Archive")}

This command produced the exact result as the last command…

How To Use PowerShell -OR And -LIKE Operator

In a previous example, I showed you how to combine -OR and -AND operators in a Where-Object statement. Following on from that example, now I want to use the -LIKE operator with the -OR operator instead.

Also, following on from that -OR, -AND example, here is the command that returns the same result using a -LIKE, -OR statement.

Get-Process | Where-Object {($_.Name -like "sv*") -OR ($_.Name -like "chro*")}

And here is the result in PowerShell…

How To Use PowerShell -AND And -NOTLIKE Operator

This example follows from the last example. But, this time, instead of using -LIKE, -OR, I’ll use -NOTLIKE, -AND.

If you have been following this guide from the beginning, you should know that the -NOTLIKE operator produces the opposite of the -LIKE operator. So, the command below returns all processes NOT like chrome and svchost.

Get-Process | Where-Object {($_.Name -notlike "svc*") -AND ($_.Name -notlike "chro*")}

In other words, I want to remove chrome and svchost from my result.

Frequently Asked Question About PowerShell -OR Operator

Frequently Asked Question About PowerShell -OR Operator
1. What Is PowerShell Operator?

In PowerShell, an operator is a character element that you can use in commands or statements.

2. What Are The Types Of PowerShell Operators?

The most common operators in PowerShell are Arithmetic, Assignment, Comparison, Logical, and Redirection operators.

Examples of:
a) Arithmetic Operators are: +, -, *, /, %
b) Assignment Operators: =, +=, -=, *=, /=, %=
c) Comparison Operators: -eq, -ne, -gt, -lt, -le, -ge
d) Logical Operators: and, -or, -xor, -not, !
Redirection Operators: , >>, 2>, 2>>, and 2>&1

3. How Do You Write A Function In PowerShell?

To write a function, use the syntax below. Then, enter your PowerShell statements with the function blocks.

Function <name> {

}

To learn about PowerShell functions, read my article – PowerShell Function: Syntax, Parameters, Examples

4. How Do I Write A PowerShell Script?

To write a PowerShell script, open a text file and enter your PowerShell statements and commands.

Then, when you finish, save the text file with the .ps1 file extension. Once you’ve done this, you’ve written your first PowerShell script!.

Alternatively, instead of using a textile, for advanced PowerShell scripting, use PowerShell ISE.

5. Is PowerShell tThe Same As CMD?

No. PowerShell is NOT the same as cmd.

Windows PowerShell is a task-based command-line shell and scripting language built on .NET. On the contrary, CMD (Command Prompt) is a Windows command line interpreter used to execute CMD commands.

To learn more, read my article – Windows Powershell vs CMD: Differences and Similarities

My Final Thoughts About PowerShell -OR Operator

My Final Thoughts About PowerShell -OR Operator

The PowerShell -OR operator is one of the logical operators. In PowerShell, you use logical operators to connect conditional statements into a single complex conditional.

Specifically, you use the logical -OR operator to connect multiple statements into a single complex statement that evaluates to True if ONE of the values of the conditions is True.

Similar to other logical operators, the PowerShell -OR operator if the first operator evaluates to True, PowerShell will not evaluate the second statement. Rather, it returns True since the first statement evaluates to True.

When I set out to write this guide, my aim was to help all my readers understand the PowerShell -OR operator. I hope I achieved this with you!

If I did and you found this guide valuable, please vote the Yes to the “Was this post helpful?” question below.

Alternatively, I’ll like you to share your experience with the -OR operator in PowerShell scripting. To share your experience or even ask a question, use the “LEAVE A REPLY” form at the end of this page.

Finally, if you want to learn PowerShell and become a star in your team, visit our Windows PowerShell Explained page. You may also find our  Windows PowerShell How-To Guides page extremely helpful.

References And Further Reading

  1. about_Logical_Operators
  2. Powershell If Else Explained: Syntax and Examples
  3. Logical operator precedence in PowerShell
  4. about_Operator_Precedence
  5. about_Do
  6. Get-ChildItem – Microsoft.com
  7. Get-ChildItem – Itechguides.com
  8. Use -notlike to filter out multiple strings in PowerShell



Source by [author_name]

Leave a Reply

Your email address will not be published.

You May Also Like