If you are learning PowerShell scripting or have been using PowerShell scripting for quite a long time, you must have realized that it’s quite difficult to design a PowerShell script without including PowerShell operators. PowerShell operators play an important role in overall PowerShell scripting. You might have worked with operators in general with Visual Basic scripting or any other scripting languages. The operators are available in all of the scripting as well as programming languages. There are two types of operators available in PowerShell — conditional and logical. In this article, we will explain and provide some examples using PowerShell comparison operators.
Why use operators?
It is certainly true that you will not get far in creating PowerShell scripts without conditional and logical operators. However, the first question is why do we use operators? You may want to use operators to execute an action or a series of PowerShell statements based on the result returned by the operator. For example, if you are working on a PowerShell script that starts Windows services in sequence, you will be required to use operators. Let’s assume you have two Windows services named “Service1” and “Service2” and you want to make sure that the “Service2” starts only if “Service1” is running. To accomplish this goal, what you can do is check the status of “Service1” and ensure that it is running before executing the PowerShell commands to start “Service2.” This is shown in the script below:
$AllServices = Get-Service
ForEach ($ThisService in $AllServices)
IF ($ThisService.Name -eq "Service1")
IF ($ThisService.status -eq "Running")
Start-Service -Name "Service2"
As you can see in the script, we are collecting all Windows services from the local computer, checking the status of “Service1” by using the “-eq” operator, and then issuing a service start command for “Service2”. So the command to start “Service2” will be executed only if the “Service1” is running.
Several categories of PowerShell comparison operators
We use comparison operators to let you specify conditions for comparing values and then finding values that match the specified conditions or patterns. When using PowerShell comparison operators you will be required to specify a value. The comparison operators are divided into several categories such as equality operators, matching operators, containment operators, and replacement operators.
The table below provides a description of equality operators.
|"-eq"||Use this operator to match a condition.|
|"-ne"||Use this operator to say a condition does not match.|
|"-gt"||Use this operator to say that a value is greater than the specified value.|
|"-ge"||Use this operator to say that a value is greater than or equal to a value specified.|
|"-lt"||Use this operator to say that a value is less than the specified value.|
|"-le"||Use this operator to use that a value is less than or equal to a value specified.|
Here are some examples using equality operators. Open a PowerShell command window and execute the below PowerShell commands and see if the results matches with what you see here:
- Command 1: 4 -eq 4 and the result will be “True”
- Command 2: 4 -eq 5 and the result will be “False”
- Command 3: "xyz" -eq "xyz" and the result will be “True”
- Command 4: "abc" -eq "abc", "def" and the result will be “False” as the “abc” first value is not equal to “abc” and “def” values specified in the condition.
You can use matching operators to check if a condition matches or not. The table below provides matching operators in PowerShell.
|"-like"||Use this operator to return true if a pattern specified in the condition matches.|
|"-notlike"||This operator is opposite to “-like” operator. Use this operator to return true if a condition “DOES NOT” matches.|
|"-match"||Use this operator to return true if a value matches exactly.|
|"-notmatch"||Use this operator to return true only if a value “DOES NOT” match exactly. This operator is opposite to “-match” operator.|
Here are some examples using matching operators. Open a PowerShell command window and execute these PowerShell commands and see if the results match with what you see here:
- Command 1: “TechGenix” –like “Genix” and the result will be “True”.
- Command 2: “TechGenix” –like “Genix” and the result will be “False”.
- Command 3: “John”, “Thomas”, –match “John” and the result will be “John” as the John matches with one of the values provided in the command.
- Command 4: “Nirmal”, “Bijal”, –match “Bijal” and the result will be “Bijal” as the Bijal matches with one of the values provided in the command.
- Command 5: “Nirmal” –notlike “Bijal” and the result will be “False”.
You can use containment operators, which are quite similar to equality operators, to return values in a Boolean value. There are two containment operators: -Contains and –Notcontains as is shown in the PowerShell commands below:
- Command 1: “Nirmal”, “Bijal” –contains “Nirmal” and the value returned is “True”.
- Command 2: “Server”, “PowerShell” –notcontains “Shell” and the returned value is “True”.
PowerShell comparison operators: Now you know the building blocks
You are ready to expand your scripting power by using PowerShell comparison operators. We have provided a few examples using equality operators, matching operators and containment operators. With these building blocks, the sky’s the limit for what you can accomplish.
More PowerShell Basics articles
- Which type of PowerShell loop should you be using?
- Working with CSV files in PowerShell
- Reading and extracting values from XML files with PowerShell
- Working with dates in PowerShell revisited
- PowerShell regular expressions: Making string evaluation easier