PowerShell Comparison Operators

PowerShell comparison operators allow users to compare values and make decisions based on conditions. In this tutorial, I will explain everything about the comparison operators in PowerShell with various examples.

Comparison Operators in PowerShell

Comparison operators in PowerShell compare two values or expressions, which ultimately helps in decision-making processes. These operators can be used with various data types, including strings, numbers, and dates.

Now, let us check out various lists of comparison operators with examples in PowerShell.

Basic Comparison Operators

Below is the list of the basic PowerShell comparison operators.

Equals (-eq)

The -eq operator is used to check if two values are equal in PowerShell. If the values are equal, it returns True; otherwise, it returns False.

Here is an example.

$a = 5
$b = 5
$c = 10

$a -eq $b  # Returns True
$a -eq $c  # Returns False

I executed the above PowerShell script, and you can see the output in the screenshot below:

Comparison Operators in PowerShell

Not Equals (-ne)

The -ne operator checks if two values are not equal in PowerShell. It returns True if the values are not equal and False if they are equal.

$a = 5
$b = 10

$a -ne $b  # Returns True
$a -ne 5   # Returns False

Greater Than (-gt)

The -gt operator checks if the value on the left is greater than the value on the right. Here is a PowerShell script.

$a = 10
$b = 5

$a -gt $b  # Returns True
$a -gt 15  # Returns False

Greater Than or Equal To (-ge)

The -ge operator checks if the value on the left is greater than or equal to the value on the right. You can check a PowerShell script.

$a = 10
$b = 10

$a -ge $b  # Returns True
$a -ge 5   # Returns True
$a -ge 15  # Returns False

Less Than (-lt)

The -lt operator in PowerShell checks if the value on the left is less than the value on the right. You can see an example below:

$a = 5
$b = 10

$a -lt $b  # Returns True
$a -lt 1   # Returns False

Less Than or Equal To (-le)

The -le operator checks if the value on the left is less than or equal to the value on the right in PowerShell. Here you can see a script below:

$a = 5
$b = 5

$a -le $b  # Returns True
$a -le 10  # Returns True
$a -le 1   # Returns False

Look at the screenshot below for the output after I executed the above script using VS code.

PowerShell Comparison Operators

Check out PowerShell Like Operator

Case Sensitivity in PowerShell Comparison Operators

By default, PowerShell comparison operators are case-insensitive. However, you can use their case-sensitive counterparts by prefixing the operator with a c. For example, -ceq is the case-sensitive version of -eq.

You can see an example below:

$a = "Hello"
$b = "hello"

$a -eq $b   # Returns True
$a -ceq $b  # Returns False

Check out

Use Comparison Operators with Different Data Types in PowerShell

PowerShell comparison operators can be used with various data types, including strings, numbers, and dates. The behavior of the operators may vary depending on the data type.

Strings

Here is an example of how to work with strings using a comparison operator.

$a = "PowerShell"
$b = "powershell"

$a -eq $b   # Returns True (case-insensitive)
$a -ceq $b  # Returns False (case-sensitive)

Numbers

Here is an example of a comparison operator with PowerShell number data types.

$a = 10
$b = 20

$a -lt $b  # Returns True

Dates

Here is how to use the PowerShell comparison operator with Dates data types.

$date1 = Get-Date "2024-01-01"
$date2 = Get-Date "2024-12-31"

$date1 -lt $date2  # Returns True

I executed the above PowerShell script, and you can see the output in the screenshot below:

Advanced PowerShell Comparison Operators

Check out How to Use -and Operator in PowerShell?

Advanced PowerShell Comparison Operators

Now, let me show you a few advanced PowerShell comparison operators and how to use these comparison operators.

Match (-match)

The -match operator in PowerShell is used for regular expression matching. It returns True if the value on the left matches the regular expression on the right.

Here you can see an example below:

$string = "Hello, World!"
$string -match "Hello"   # Returns True
$string -match "world"   # Returns False (case-insensitive)

Not Match (-notmatch)

The -notmatch operator in PowerShell checks if the value on the left does not match the regular expression on the right.

$string = "Hello, World!"
$string -notmatch "world"  # Returns True (case-insensitive)

Containment (-contains)

The -contains operator checks if a collection contains a specific value. It returns True if the value is found in the collection in PowerShell. You can see an example below.

$array = 1, 2, 3, 4, 5

$array -contains 3  # Returns True
$array -contains 6  # Returns False

Not Contain (-notcontains)

The -notcontains operator in PowerShell checks if a collection does not contain a specific value. You can see an example below:

$array = 1, 2, 3, 4, 5

$array -notcontains 6  # Returns True
$array -notcontains 3  # Returns False

In (-in)

The -in operator checks if a value is in a collection in PowerShell. It is the reverse of -contains. You can see an example below.

$array = 1, 2, 3, 4, 5

3 -in $array  # Returns True
6 -in $array  # Returns False

Not In (-notin)

The -notin operator in PowerShell checks if a value is not in a collection. It is the reverse of -notcontains.

Here is an example.

$array = 1, 2, 3, 4, 5

6 -notin $array  # Returns True
3 -notin $array  # Returns False

Check out PowerShell Not Operator

PowerShell Comparison Operators Examples

Now, let me show you a few examples of how to use comparison operators in PowerShell.

Example-1: Conditional Statements

Let me show you an example of how to use the comparison operators in conditional statements in PowerShell.

Comparison operators are crucial in conditional statements, such as ifelseif, and else.

$number = 15

if ($number -lt 10) {
    Write-Output "The number is less than 10."
} elseif ($number -le 20) {
    Write-Output "The number is between 10 and 20."
} else {
    Write-Output "The number is greater than 20."
}

You can see the output in the screenshot below, after I executed the above PowerShell script.

PowerShell Comparison Operators Example

Example-2: Validation and Error Handling

You can use comparison operators to validate input and handle errors in PowerShell. Here is an example.

function Get-SquareRoot {
    param (
        [double]$number
    )

    if ($number -lt 0) {
        Write-Output "Error: Cannot calculate the square root of a negative number."
    } else {
        [math]::Sqrt($number)
    }
}

# Test the function with a valid input
Get-SquareRoot -number 16  # Output: 4

# Test the function with an invalid input
Get-SquareRoot -number -4

Example-3: Regular Expression Matching

The -match operator is powerful for pattern matching using regular expressions. Here is an example and a complete PowerShell script.

$email = "user@example.com"

if ($email -match "^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$") {
    Write-Output "Valid email address."
} else {
    Write-Output "Invalid email address."
}

Example-4: Combine Multiple Conditions

You can combine multiple conditions using logical operators (-and-or-not) with comparison operators in PowerShell.

Here is a complete example.

$age = 25
$hasLicense = $true

if ($age -ge 18 -and $hasLicense) {
    Write-Output "You are eligible to drive."
} else {
    Write-Output "You are not eligible to drive."
}

Look at the output in the screenshot below, you will also get the exact output when you execute the above PowerShell script.

PowerShell Comparison Operators Examples

Conclusion

PowerShell comparison operators allow you to compare values, filter data, make decisions, and control the flow of your scripts.

In this tutorial, I have explained basic comparison operators, such as -eq-ne-gt-ge-lt, and -le, as well as advanced operators like -match-notmatch-contains-notcontains-in, and -notin. I have also shown a few examples of PowerShell comparison operators.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.