Understanding PowerShell Comparison Operators By Example


The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

PowerShell comparison operators, also known as logical and relational operators, help you compare values in PowerShell. This article is an introduction to the different types of comparison operations available to you when using PowerShell and how they work.

The “powershell operator example” is a blog post that explains the different comparison operators in PowerShell. This blog post provides an example of how to use each one.

Understanding PowerShell Comparison Operators By Example

Like many other programming languages, PowerShell has various comparison operators. However, unlike other languages, those comparison operators can look a little odd. Instead of ==, you have eq. Instead of <>, you’ve got ne. It can be confusing to newcomers. Let’s demystify PowerShell comparison operators in this article.

Using the eq and ceq operators to test for equality

The eq operator is used in PowerShell to determine if one object is equal to another. Simple objects of various sorts, such as strings, boolean values, integers, and so on, are compared using the eq operator. Depending on the outcome, the eq operator will return either a boolean True or False value.

To compare two values, use the -eq operator. Assume you’ve assigned a string to the variable $string. You want to make sure the value of this variable is equivalent to the string value “PowerShell.”

You can see that we’re assigning the value PowerShell to the variable $string in the following example. The example then compares the value of $string with the string powershell using the eq operator. Isn’t that simple? But wait, they’re not exactly the same.

PS> $string = “PowerShell” PS> $string -eq “powershell” True

It’s important to remember that = is an assignment operator, not a comparison operator. In PowerShell, you can’t compare two values using the = operator.

Using the ceq Operator to test case-sensitivity

Notice how, in the preceding example, eq returned a boolean True result even if the string wasn’t identical. Because the eq operator is case-insensitive, this behavior occurs. Use the ceq operator to check for case-sensitive equality. The ceq operator is identical to the eq operator, with the distinction that it is case-sensitive.

The following code snippet is an example of how to use ceq. Now you’ll note that PowerShell is aware of the case-sensitive distinction.

PS> $string -ceq “PowerShell” True PS> $string -ceq “powershell” False

Using the ne and cne Operators to Test for Inequality

PowerShell features a pair of operators named ne and cne that perform the exact opposite of eq and ceq, which test for equality. These two operators, like their counterparts, conduct the identical action but in the opposite direction.

PS> $string = “PowerShell” PS> $string -ne “PowerShell” False PS> $string -cne “PowerShell” False PS> $string -cne “powershell” True

Inspecting a Collection for Items

For scalar or single values like texts, integers, and boolean values, the eq and ceq operators are often employed. However, these operators may also locate instances of certain values inside a collection, such as an array.

Notice how we’ve created an array with 12 numbers in the example below. Perhaps you’d want to locate all occurrences of the number 9 in that array. No issue, just use the eq operator to the array to get all of the compared integers.

PS> $array [email protected](1,2,3,4,5,6,7,8,9,9,9,9) PS> $array -eq 9 9 9 9 9 PS> ($array -eq 9).Count 4

Collections are being tested to see whether they can hold their values.

Let’s take it a step further and present the “containment” operators, because you just learned how to use the eq operator to identify instances in arrays. contains, notcontains, in, and notin are the operators. If a collection has an instance, these operations yield a boolean True or False result.

Take a look at the sample below. We’re going to create an array of nine numbers in this example. You may use contains or notcontains to receive a simple Yes/No response to whether or not the array includes a certain number.

PS> $array = @(2,4,5,6,8,8,9,9,9) PS> $array -contains 9 True PS> $array -notcontains 9 False PS> $array -contains 5

In the same way, there are the in and notin operators. With the exception that the value to compare is on the left instead of the right, these operators are almost similar to contains and notcontains.

@(2,4,5,6,8,8,8,9,9,9,9) 9 -in $array True $array $notin $notin $array $array $array $array $array $array $arra

Using the gt and ge operators to test “greater than”

What if you want to see whether a number (an integer) is larger than another number, or if it is greater than or equal to another number? The gt and ge operators would be used. Integers are compared using these operators.

These operators, like the others, produce boolean True or False results based on whether one integer is larger than another. Both of these operators check whether the left integer is greater than, equal to, or less than the right integer.

You can see how each of these operators works in the example below.

PS> 7 -gt 5 True PS> 7 -gt 7 False PS> 7 -ge 7 True

The ge and gt operators work similarly to the eq operator in that they enable you to discover instances in collections. PowerShell examines each item in a collection and compares its value to the one you’ve specified. Below is an excellent illustration of this.

PS> $array [email protected](1,2,3,4,5,6,7,8,9,9,9,9) PS> $array -gt 5 6 7 8 9 9 9 9 PS> $array -ge 5 5 6 7 8 9 9 9 9

Putting “less than” to the test Operators with the letters lt and le

The lt and le operators, like the gt and ge operators, accomplish the same purpose but in the opposite direction. The lt and le operations compare two numbers to see whether the one on the left is less than, equal to, or greater than the one on the right.

As you can see in the example below, gt and ge provide the same capabilities for comparing each value in a collection.

PS> $array [email protected](1,2,3,4,5,6,7,8,9,10) PS> $array -lt 5 1 2 3 4 PS> $array -le 5 1 2 3 4 5

Wildcard-based matching

You’ve already learnt how to make 1:1 matches. So far, all of the operators have compared one full value to another, but PowerShell has one more trick up its sleeve. You may also use wildcards or asterisks to compare values.

Consider the case when you just know a few characters from a string. Because eq needs you to know the complete string, you can’t use it. You don’t need to know the complete string to use the like operator. To see whether a value is similar to another, use a wildcard to substitute the component you don’t know.

The same functionality can be applied to collections as it can to other operators.

Below is an example of like and its case-sensitive sibling, clike.

PS> $string = “PowerShell” PS> $string -like “*Shell” True PS> $array = @(“PowerShell”,”Microsoft”) PS> $array -like “*Shell” PowerShell PS> $array -clike “*Shell” PowerShell

The -notlike operator returns a boolean value. True if no match was discovered and False if a match was found. When applied to a collection, it returns all other values that do not match the pattern specified on the right side of the -notlike operator. Use the -cnotlike operator to add case sensitivity to the pattern.

$string -notlike “*Shell” False $string = “PowerShell” Array $array = @ (“PowerShell”,”Microsoft”) Microsoft $array -notlike “*Shell” PowerShell $array -cnotlike “*shell” Microsoft

Regular Expression-Based Matching

The like and relative operators are useful, but only when more complicated matching is required. You may then explore the deep, dark realm of regular expressions (regex). Regex can conduct exceedingly complicated string matching using the match and notmatch operators.

Match and its opposite, notmatch, compare two strings and return a boolean True or False value, just like the other operators. In addition, as with the other operators, the same behavior may be applied to a collection, as seen in the example below.

PS> $string = “PowerShell” PS> $string -match “ow” True PS> $array = @(“PowerShell”,”Microsoft”) PS> $array -match “ow” PowerShell PS> $string = “PowerShell” PS> $string -cmatch “po” False PS> $string -cmatch “Po” True PS> $array = @(“PowerShell”,”Microsoft”) PS> $array -cmatch “Po” PowerShell


You learnt about PowerShell comparison operators and how to use them for single values and collections in this post. You’ve seen how the output varies depending on the operator and whether the result is a scalar or a collection.

You’ve also seen case-sensitive operators, matching operators that let you match using a regular expression or a wildcard, and containment operators that let you check whether an object contains a given value or if a value exists in a specific object.

The “powershell string comparison” is a PowerShell command that allows users to compare strings. The “strings” are listed in the order they appear in the output.

Frequently Asked Questions

How do you use comparison operators in PowerShell?

A: The comparison operators are used to compare two values and determine if theyre equal, not equal or greater than. They can also be used with strings and integers as well. Comparison operators are
-eq -neq -gt -geq
-lt -leq

How do I compare two values in PowerShell?

A: To compare two values, you would use the following statement.
powershell -command Get-Process | Sort Name
To view that information in a table format, we can run this command.
powershell -command Format-Table Get-process–Name

What are examples of comparison operators?

A: One type of comparison operator is a less than or equal to symbol ( < ). This symbol compares two items and ensures that the first item is smaller than the second. For example, 2 < 3 would return true because 2 is smaller then 3.

Related Tags

  • powershell contains operator
  • what is in powershell
  • powershell operators list
  • powershell not equal
  • powershell not contains

Table of Content