PowerShell -OR Operator Explained: Syntax and Examples

Photo of author

By Victor Ashiedu

Published

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.

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

So far 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.

1. Use 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.

2. 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).

3. 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.

4. 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.

5. 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.

6. 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

7. 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…

8. 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…

9. 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 Questions

1. What is a 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, !
e) 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 {

}

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 text file, for advanced PowerShell scripting, use PowerShell ISE.

5. Is PowerShell the 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

Conclusion

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 you found this article helpful. If you did, click on “Yes” beside the “Was this page helpful” question below. You may also express your thoughts and opinions by using the “Leave a Comment” form at the bottom 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.

We go the extra mile to deliver the highest quality content for our readers. Read our Content Writing, Content Review, and Anti-Plagiarism policies to learn more.

About the Author

Photo of author

Victor Ashiedu

Victor is the founder of InfoPress Media, publishers of Ilifeguides and Itechguides. With 20+ years of experience in IT infrastructure, his expertise spans Windows, Linux, and DevOps. Explore his contributions on Itechguides.com for insightful how-to guides and product reviews.

Related Articles

Get in Touch

If this article does not meet your expectations, kindly let us know. We have various ways you can get in touch with us:

  1. Respond to "Was this page helpful?" above
  2. Leave a comment with the "Leave a Comment" form below
  3. Email us at [email protected] or via the Contact Us page.

Leave a comment