≡ Menu

PowerShell -eq, -ceq and -ieq Comparison Operators

This article helps you in understanding -eq, -ceq, -ieq Comparison operators in Windows PowerShell which is used for comparison purpose. The usage examples of PowerShell -eq operator in this article gives a better understanding of its usage.

Summary

Windows PowerShell is one of the great shells that Microsoft ever designed & developed. With help of PowerShell Windows Administrators and developers are benefited a lot as it simplifies the automation in Windows Operating System. You can read more about PowerShell and its uses from TechNet site.

Since the scope of this article is to know more about PowerShell -eq operator, let us start looking at it.

The comparison operator (-eq):

PowerShell is like any other programming language. While developing scripts using PowerShell we need to make use of operators. The -eq (called as equal) operator is one of them. As the name indicates the purpose of this -eq operator is to perform comparison in PowerShell. We can compare strings, numbers and characters using -eq operator. This operator is used for comparing scalar values. The output this -eq operator is a Boolean value(True or False) which indicates whether the input scalar values are equal or not.

Examples:

Now let us look at some examples to understand how to use this -eq operator in Windows PowerShell for comparison purpose.

String Comparison:

Two strings can be compared using -eq operator in PowerShell to verify if they are equal or not.

“abc” -eq “abc”

“abc” -eq “xyz”

string-comparison-1

The above code is comparing one string with another and returning True or False based on whether strings are equal or not. You will have to do this many times while writing PowerShell scripts based on your requirements.

Look at the below example where PowerShell -eq operator is used to determine if the user has entered correct PIN number or not.

$UserPIN = Read-Host “Enter your Name”

if($UserPIN -eq “Steve”) {

Write-Host “Welcome Steve!!”

} else {

Write-Host “Sorry $UserPIN, only Steve is allowed”

}

String-Comparision-2

This -eq operator in PowerShell can accept collections type of variables as well but the use cases of it are less and I don’t want to create confusion by discussing it here.

 Numeric Comparison:

Like the way the -eq operator used for string comparison, it can compare number values as well. Look at the below examples.

100 -eq 100

100 -eq 110

0100 -eq 100

10.1 -eq 10.1

10.2 -eq 10.1

numeric-comparison-1

It compared both the operands passed to it and returned the result. You might wonder how it knows if the operands passed to -eq operator are strings or numeric values. That is the beauty of PowerShell. It is capable of understanding the variable type and then perform the comparison.

Also you might have noticed that -eq is capable of comparing decimal number as well.

Character Comparison:

Character comparison in PowerShell is similar to String or numeric comparison. You can compare two characters using -eq operator to verify if they are equal or not.

‘c’ -eq ‘c’

‘c’ -eq ‘z’

See above example for character comparison. If both characters are equal then it returns True otherwise False.

character-comparison-1

Case-sensitive and Case-insensitive Comparison:

So far all the examples we have seen above are case-insensitive comparisons. Yes, PowerShell -eq operator is designed to perform case insensitive comparison operators. But your PowerShell script requirements might need case sensitive comparisons as well for exact string comparison.

To address such needs, PowerShell has two more operators for string comparison.

  1. Case-Sensitive Comparison (-ceq)
  2. Case-Insensitive Comparison (-ieq)

Case-Sensitive Comparison (-ceq):

This operator works similar to -eq operator but returns false if the strings being compared are different by case. For example, it returns false when you try to compare Windows with windows. Because the first term is having W in capital.

See below example to see how it works:

“Windows” -eq “windows”

“Windows” -ceq “windows”

case-sensitive-compare

Case-Insensitive comparison (-ieq):

Even though the -eq operator performs string comparison in case-insensitive way, you still want to ensure a case insensitive comparison, you can use -ieq. The usage of this parameter is very less because most people use -eq which does the same thing. If you know any case where -ieq is preferred over the -eq, please post in the comments of this article so that I will get it added here.

Below is a small example that shows the case-insensitive comparison of strings.

“linux” -eq “Linux”

“linux” -ieq “Linux”

“linux” -ceq “Linux”

case-insensitive-compare

The case insensitive compare(-ieq) and the regular compare operator(-eq) returned True in both the cases. But the case sensitive(-ceq) compare returned False.

This case insensitive or sensitive kind of operators are applicable only for Strings and Characters. The numeric values don’t have any case so using -eq operator alone is sufficient.

Do you have any questions about using any of these -eq, -ceq, or -ieq operators? Feel free to post it in comments section below this article.