EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial Comparison Operators in PowerShell
Secondary Sidebar
PowerShell Tutorial
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

Comparison Operators in PowerShell

By Chirag NagarekarChirag Nagarekar

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.

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,060 ratings)

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

-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
Popular Course in this category
Programming Languages Training (41 Courses, 13+ Projects, 4 Quizzes)
  41 Online Courses |  13 Hands-on Projects |  322+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
1 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more