Nifty little time savers: The power of PowerShell logical operators

If you are just learning PowerShell scripting or have been using PowerShell scripting for quite some time, you must have realized that it’s quite difficult to design a PowerShell script without including PowerShell operators. These PowerShell operators play an important role in overall PowerShell scripting. We have spoken and explained about the power of using PowerShell comparison operators in another article on TechGenix. In this article, we will discuss PowerShell logical operators and how you can use them.

About PowerShell logical operators

Logical operators in PowerShell check multiple conditions specified in a PowerShell statement or expression. For example, checking if two files exist on a server before you take an action is a logical operation. If you want to check if two services exist on a server before you could take an action, that is also a logical operation. Another example: You would like to start two Windows services on a computer only if the binary files of the services exist on the computer. Now there are two ways to check these conditions: 1) You can use multiple IF conditions or 2) use a logical operator within a PowerShell statement. The IF statements below can help you check conditions without using logical operators:

  • IF {Test-Path -Path “C:\Temp\Service1File.EXE”)
  • IF {Test-Path -Path “C:\Temp\Service2File.EXE”}
  • Take Action here

As you can see in the above PowerShell statements, the first IF condition checks if the C:\Temp\Service1File.EXE exists. If that condition is met then the next IF statement is executed (which is to check the existence of C:\Temp\Service2File.EXE). Without logical operators, your PowerShell script can be lengthy. In other words, you will be required to spend much time writing several lines of codes. Now, let’s take a look at the below IF statement.

  • IF {Test-Path -Path “C:\Temp\Service1File.EXE” –AND Test-Path -Path “C:\Temp\Service2File.EXE”}
  • Take Action here

As you can see in the above IF statement, we are using “-AND,” which is actually a logical operator. The “-AND” instructs the IF statement to check both the conditions and execute “Take Action Here” only if both the conditions are met. The logical operators are quite handy and reduce the overhead in your scripting.

Connecting PowerShell statements

We use logical operators to let you connect PowerShell statements that, in turn, allows you to use a single PowerShell statement/expression to test multiple conditions. For example, the PowerShell command below uses an IF statement and is able to check two conditions in one statement.
IF {$ThisVM -eq “TestVM” –and $ThisVMStatus –eq “Running”}
As you can see in the above command, we are checking if the virtual machine stored in $ThisVM is “TestVM” AND make sure the virtual machine is “Running.” The virtual machine state is stored in the $ThisVMStatus variable. Similarly, before the PowerShell command checks if a server is a domain controller then the disk space on C:\ drive must not be less than 10GB.
IF {$ThisServer -eq “Domain Controller” –and $DiskSpace –gt 10}
And if you look at the PowerShell command below, it’s different from the previous PowerShell commands.
IF {$ThisServer -eq “Domain Controller” –OR $ThisServer –eq “Member Server”}
The PowerShell command above needs to ensure only one condition is met out of two. If $ThisServer variable contains a value “Domain Controller” or “Member Server,” that means the condition is met.

You might have noticed the use of “-AND” and “-OR” in the PowerShell commands. These are called logical operators. The “-AND” is used when you need to make sure all conditions specified in a PowerShell statement/expression are TRUE. Whereas “-OR” is used when you need to make sure only one condition is TRUE out of multiple conditions specified. While there are four logical operators in PowerShell, most of the time you will be working with “-AND” and “-OR” logical operators.

This table provides a description on logical operators.

Logical Operator Description
“-and” Use this operator to ensure all conditions specified in a PowerShell statement are met.
“-or” Use this operator to meet only one condition out of multiple conditions.

Here are some examples of how you can use logical operators. Open a PowerShell command window and execute the PowerShell commands below and see if the results match with what you see here:

Command 1: The result will be “True” because both conditions are met.
(1 -eq 1) -and (2 -eq 2)
Command 2: The result will be “True” even though both conditions are not met because we are using the “-OR” logical operator.
(1 -eq 1) -or (1 -eq 2)
Command 3: The result will be “False” because both the conditions are not met.
(1 -eq 2) -and (1 -eq 2)

Nifty little time savers

Now you know a little more about PowerShell logical operators. We also provided some examples of how to use these PowerShell logical operators. Using them will save you a lot of time when you write a PowerShell statement with multiple conditions.

About The Author

Leave a Comment

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top