EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial Comparison Operators in PowerShell
 

Comparison Operators in PowerShell

Chirag Nagarekar
Article byChirag Nagarekar
EDUCBA
Reviewed byRavi Rathore

Updated March 23, 2023

Comparison Operators in PowerShell

 

 

Introduction to Comparison Operators in PowerShell

In this article, we will see in detail different Comparison Operators in PowerShell. Comparison operators are used to compare, search and alter two or more values. You can also use comparison operators in conditions to match and compare values.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Comparison Operators

Windows PowerShell uses below comparison operators and by default they are Case-Insensitive. To perform a case-sensitive operation, just need to type ‘c’ ahead of the below operators. For example, -clike, -cne, -ceq etc.

-eq Equal
-ne Not Equal
-gt Greater than
-ge Greater than or equal to
-lt Less than
-le Less than or equal to
-like Checks if part of string matches (Wildcard comparison)
-notlike Checks if part of a string doesn’t matches (Wildcard comparison)
-match RegEx comparison
-notmatch RegEx comparison
-contains Containment Operator
-notcontains Non-Containment Operator
-In In Operator
-notIn Non In Operator
-Replace Replaces a string pattern

Examples to Implement Comparison Operators

Here are some of the examples of comparison operator given below with examples:

1. –eq: Equal to

This operator is used to check equality between values. They should match exactly and this is case-insensitive. The output will be True or False. For example,

Comparison Operators in PowerShell eg1

You can also compare two different data types.

Comparison Operators in PowerShell eg2

You can compare multiple values with a single value as well.

Comparison Operators in PowerShell eg3

If you use –ceq operator then the comparison will be case-sensitive.

Comparison Operators in PowerShell eg4

2. –ne: Not Equal to

This is contrary to equal to Operation. If value matches then it will return FALSE, otherwise it will return TRUE.

Comparison Operators in PowerShell eg5

For multiple values,

Comparison Operators in PowerShell eg6

Comparison Operators in PowerShell eg7

3. –gt: Greater than

It compares the value of its left side with the right side and output will be TRUE or FALSE based on values. In the example below, it compares 5 with 6 and checks if it’s greater than 6 and provides answer FALSE.

Comparison Operators in PowerShell eg8

When you compare two characters, it checks its ASCII value and provides results based on it. ASCII value of ‘a’ is 97 and ‘c’ is 99, so ‘c’ is greater than ‘a’. As a result, the second sentence is TRUE.

Comparison Operators in PowerShell eg9

If you compare multiple characters together then it will check the ASCII value of first characters on both sides and provide results accordingly. For example,

Comparison Operators in PowerShell eg10

In the above first sentence, ASCII value of ‘d’ is greater than ‘a’ and in the second sentence, the ASCII value of ‘a is less than ‘b’. For multiple value comparison.

Comparison Operators in PowerShell eg11

4. –ge: Greater than equal to

Checks if the value of the left side is greater than or equal to the value of the right side.

Comparison Operators in PowerShell eg12

5. –lt: Less than

Check if the value of the left side is less than the value of the right side.

Comparison Operators in PowerShell eg13
Comparison Operators in PowerShell eg14

6. –le: Less than or equal to

Checks if the value of the left side is less than equal to the value of the right-side value.

Comparison Operators in PowerShell eg15

7. –Like

It matches the left side of the value with the right side value with Wildcard character (*).

like

Wildcard character (*), when applied it checks if part contains in a string or not. If it applied after particular value (i.e. This*) then it checks if that word + successor string exist or not. In the first example.

  • Word: This
  • Successor String: is Powershell

If applied before the particular word then it checks if a particular word + precedence string exists or not. In the second example.

  • Word: Powershell
  • Precedent string: This is

In the third example, wildcard (*) is applied afterword “Powershell” and word is matching but there is no successor string so the output is False. In the fourth example, wildcard (*) is applied both sides so it checks if a string exists before or after it in other words mentioned word is part of that string. So here output is TRUE.

8. –NotLike

It is contrary to Like operator but definition remains the same for a wildcard, and only output is reversed.

not like1

not like2

9. –Match

It matches the string using a regular expression. When the input is scalar, it populates $matches variable automatically.

match

$matches

Name         Value

—-               —–

0                 coming

matches

To match at least any one of the characters, put them into [ ].

char

In the above example, the character ‘c’ is matching. To match at least one of the characters in the contiguous range [range].

char1

Here, from characters n to r [case-insensitive] will be checked against a string and ‘o’ and ’p’ are matching. Hence, the output is TRUE.

10. –NotMatch

Contrary to Match operator. If match found then returns false or vice versa.

notmatch

matches1

Name              Value

—-                   —–

0                     coming

matches2

matches3

11. -Contains: Containment Operator

Tells whether a collection of reference values includes the exact (case-insensitive) single value. If yes then returns TRUE, otherwise FALSE.

Syntax:

<Reference-values> -Contains <Test-value>

Comparison Operators in PowerShell eg16

Comparison Operators in PowerShell eg17

The above output is FALSE because the left side contains two values. But if you store them into a variable and match them, the output will be TRUE.
true

For case-sensitive operations, use –ccontains operator.

12. –notContains: Containment Operator (Contrary to Contains)

If the test value exactly matches the set of reference values then output is FALSE. Otherwise, the output is TRUE.

Comparison Operators in PowerShell eg18

Comparison Operators in PowerShell eg19

13. -in

Similar to Contain Operator, only the syntax is reversed. -in the operator was introduced in Powershell 3.0. If the test value matches the reference values then returns TRUE otherwise FALSE.

Syntax:

<Test-value> -in <Reference-values>

Comparison Operators in PowerShell eg20

comparison 21

In the above example, “Shell” doesn’t exactly match with Powershell.

comparison 21

comparison 22

14. –notin: Not in Operator (Contrary to -in operator)

If test value matches exactly to Reference values then it returns FALSE, otherwise TRUE.

notin

15. –Replace: Changes the specified value

Syntax:

<String> -replace <Old value>,<New Value>

comparison 23

To replace the value in a variable

replace

Note: There are two matching values of “is”, so it has replaced two values.

16. Other Similar Operators

There are other similar operators as mentioned below.

a. Bitwise Operators

-bAnd Bitwise AND
-bOr Bitwise OR (Inclusive)
-bXor Bitwise OR (Exclusive)
-bNot Bitwise NOT
-shl Shift-Left
-shr Shift-Right

2. Logical Operators

-and Logical AND
-or Logical OR
-xor Logical Exclusive OR
-not Logical NOT
! Logical NOT

Recommended Articles

This is a guide to Comparison Operators in PowerShell. Here we discuss the different types of comparison operators in Powershell along with examples. You may also look at the following articles to learn more-

  1. PowerShell Operators
  2. PowerShell String Functions
  3. Comparison Operators in JavaScript
  4. PowerShell Commands
  5. Array in PowerShell with Examples
  6. Guide to Oracle LIKE Operator
  7. Different Types of Matlab Logical Operators

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW