

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between –Match, -Like and –Contains Operator in PowerShell?
All the above 3 operators (Match, Like, and Contains) mentioned are the comparison operator in PowerShell. Match and Like operators are almost similar operator only difference is the Wildcard character and the Contains operator is entirely different. We will see the example and understand how they work.
Example
PS C:\WINDOWS\system32> "This is a PowerShell String" -Match "PowerShell" True PS C:\WINDOWS\system32> "This is a PowerShell String" -Like "PowerShell" False PS C:\WINDOWS\system32> "This is a PowerShell String" -Contains "PowerShell" False
If you see the output of the above example, the result is True only for the Match statement and reason is when you match the keyword in the string, it checks whether the keyword exists or not. It doesn’t matter the keyword you used is an individual keyword or connected word. The below statement is also true.
PS C:\WINDOWS\system32> "This is a PowerShell String" -Match "Shell" True
But wildcard character (*) is not helpful for the Match operator.
PS C:\WINDOWS\system32> "This is a PowerShell String" -Match "*PowerShell*" parsing "*PowerShell*" - Quantifier {x,y} following nothing. At line:1 char:1 + "This is a PowerShell String" -Match "*PowerShell*" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], ArgumentException + FullyQualifiedErrorId : System.ArgumentException
So Match condition doesn’t evaluate the Regex operator. In contrast, Like operator is used with the wildcard character(*).
PS C:\WINDOWS\system32> "This is a PowerShell String" -like "*PowerShell*" True
So the wildcard plays a major role between the Match and Like operator. We can check the wildcard (*) use with the Contains operator.
PS C:\WINDOWS\system32> "This is a PowerShell String" -contains "*PowerShell*" False
Contains operator also doesn’t work with the wildcard (*) character. It is an entirely different operator than Match and Like and works with the collection of objects (Array).
PS C:\WINDOWS\system32> "Apple","Dog","Carrot","Cat" -contains "dog" True
- Related Questions & Answers
- What is the significant difference between MySQL LIKE and equal to (=) operator?
- Combining LIKE and CONTAINS operator in SAP HANA
- What is the difference between Dictionary and HashTable in PowerShell?
- What is the difference between equals() method and == operator in java?
- What is the difference between $ErrorActionPreference and $ErrorAction cmdlet in PowerShell ?
- What is the difference between the dot (.) operator and -> in C++?
- Difference between == and is operator in python.
- What is the difference between MySQL ISNULL() function and IS NULL operator?
- What is the difference between operator and method on Python set?
- What is the difference between new operator and object() constructor in JavaScript?
- Explain difference between == and is operator in Python.
- Difference between the and$ operator in php
- What is the difference Between AND, OR operator in MySQL while Retrieving the Rows?
- What is the use of MySQL NOT LIKE operator?
- What is the use of MySQL SOUNDS LIKE operator?