EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Like Operator
 

PowerShell Like Operator

Updated March 4, 2023

PowerShell Like Operator

 

 

Introduction to PowerShell Like Operator

Like operator in PowerShell is a type of match operator. The match operators are used to find elements based on a condition using regular expressions. Like and not like both are the type of match operators. These operators are mainly used to identify whether a string is contained within another string. For comparing non-identical strings, they are used along with wildcards. When the input is a single scalar object, the $Matches variable is automatically populated. For scalar input, both like and not like operators return a Boolean value and the value of the $Matches is set to the corresponding matched component. This article will cover in detail about like and not like an operator. It will also cover other string comparison operators like the match, contains, etc.

Watch our Demo Courses and Videos

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

Syntax:

The syntax of like and notlike cmdlet is as follows.

<string[]> -match <regular-expression>
<string[]> -notmatch <regular-expression>

Examples of PowerShell Like Operator

Here are the following examples mention below

Example #1

Input:

Write-Host "Welcome to the example of like operator in PowerShell"
Write-Host "Like operator example without wildcard expression"
$input="My name is vignesh"
$input -like "sh"
Write-Host "like operator example with wildcard expression"
$input="My name is vignesh"
$input -like "*sh"

Output:

PowerShell Like Operator output 1

In the above example, without wild card expression produces a false result as it searched for exact match. With wild card expression it searches only for a match.

Example #2

Input:

Write-Host "Example of finding substring using like operator"
Write-Host "Finding sub string using wild card expression"
"abcde", "fghijk", "lmnop", "qrstuv", "wxyz" -like "abcd*"
Write-Host "finding sub string without wild card expression"
"abcde", "fghijk", "lmnop", "qrstuv", "wxyz" -like "abcde"

Output:

PowerShell Like Operator output 2

Example #3

Input:

Write-Host "Welcome to the example of not like operator in PowerShell"
Write-Host " not Like operator example without wildcard expression"
$input="test input string"
$input -notlike "te"
Write-Host "not like operator example with wildcard expression"
$input="test input string"
$input -notlike "te*"

Output:

PowerShell Like Operator output 3

Example #4

Input:

Write-Host "Example of finding substring using not like operator"
Write-Host "Finding sub string using wild card expression"
"one", "three", "seven", "ten", "tenth" -notlike "ten*"
Write-Host "finding sub string without wild card expression"
"adam", "john", "temp", "macho", "man" -notlike "ad"

Output:

PowerShell Like Operator output 4

-Match and not Match:

Match operator is used to match strings using regular expressions. The $matches variable is populated automatically for scalar input. This can be used only for strings and not for integers or any other data objects. In case if the input is a collection then only the matching members from the input is returned and the $matches variable is not populated. If the input is a single string, then the $matches variable is populated.

Example #5

Input:

write-host "Demo of match operator"
Write-Host "Match with wildcard charcaters for a single string"
$input="My name is vignesh"
$input -match "na*"
Write-Host "$matches is generated and assigned. The value is below"
$Matches
Write-Host "Match without wildcard charcaters for a single string"
$input -match "na"
Write-Host "$matches is generated and assigned. The value is below"
$Matches
Write-Host "Match with wildcard charcaters for a collection"
$input="abc", "abcd", "abdsf","erert"
$input -match "ab*"
Write-Host "$matches value is below"
$matches
Write-Host "Match without wildcard charcaters for a collection"
$input="abc", "abcd", "abdsf","erert"
$input -match "ab"
Write-Host "$matches value is below"
$matches

Output:

PowerShell Like Operator output 5

In the above example as you can see the $matches is not generated for collection of strings and is only generated for a single string input.

Example #6

Input:

write-host "Demo of not match operator"
Write-Host "Not Match with wildcard charcaters for a single string"
$input="My name is vignesh"
$input -notmatch "za*"
Write-Host "$matches is generated and assigned. The value is below"
$Matches
Write-Host "Not Match without wildcard charcaters for a single string"
$input -notmatch "za"
Write-Host "$matches is generated and assigned. The value is below"
$Matches
Write-Host "Not Match with wildcard charcaters for a collection"
$input="abc", "abcd", "abdsf","erert"
$input -notmatch "za*"
Write-Host "$matches value is below"
$matches
Write-Host "Not Match without wildcard charcaters for a collection"
$input="abc", "abcd", "abdsf","erert"
$input -notmatch "za"
Write-Host "$matches value is below"
$matches

Output:

output 6

Contains and not contains:

These are same as like and not like operators. This is used to identify whether a value is present inside a collection of values. This always generates a Boolean value output. Only when the test value matches exactly with at least one of the values, then only a true is returned.

Syntax:

<Reference-values> -contains <Test-value>

-notcontains:

This is the opposite of contains operator. A true is returned when there is no exact match.

Syntax:

<Reference-values> -notcontains <Test-value>

Example #7

Input:

Write-Host "Example of contains operator"
$input= "sdfsdfdsf","adsadsad","test","test1"
$input -contains "test"
$input= "sdfsdfdsf","adsadsad","test","test1"
$input -contains "dfsdf"
Write-Host "Demo of multiple check values"
$input="one","two","three","four","five"
$input -contains "one","two"
$input="one","two"
$input, "three","four" -ccontains $input

Output:

output 7

Example #8

Input:

Write-Host "Example of not contains operator"
$input= "sdfsdfdsf","adsadsad","dfdf","sdf"
$input -notcontains "test"
$input= "sdfsdfdsf","adsadsad","test","test1"
$input -notcontains "test"
Write-Host "Demo of multiple check values"
$input="one","two","three","four","five"
$input -notcontains "one","two"
$input="one","two"
$input, "three","four" -notcontains $input

Output:

output 8

Example #9

Input:

Write-Host "Example of finding substring using like operator"
Write-Host "Finding sub string using wild card expression"
"america", "australia", "antratica", "amesterdam", "angolio" -like "an*"
Write-Host "finding sub string without wild card expression"
"america", "australia", "antratica", "amesterdam", "angolio" -like "abcde"
Write-Host "Welcome to the example of not like operator in PowerShell"
Write-Host " not Like operator example without wildcard expression"
$input="one two three"
$input -notlike "te"
Write-Host "not like operator example with wildcard expression"
$input="my name is vignesh krishnakumar"
$input -notlike "te*"

Output:

output 9

Conclusion

Thus, the article explained in detail the like operator, its syntax and its usage with appropriate examples. The article also covered in detail about the, not like operator, match and not match operator, contains and not operator along with detailed explanation and examples. The differences between each of the type is shown using sample programs and necessary explanation was provided. The best way to learn more about this would be to practice sample programs and explore each operator in detail so as to know better on when to use which operator as most of the above operators discussed are similar in nature.

Recommended Articles

This is a guide to PowerShell Like Operator. Here we discuss the Examples of PowerShell Like Operator along with the other string comparison operators. You may also have a look at the following articles to learn more –

  1. PowerShell Split String
  2. PowerShell Get-Item
  3. PowerShell Alias
  4. PowerShell Get-Content
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
EDUCBA

*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

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

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 Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW