EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Like Operator
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

PowerShell Like Operator

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.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ 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
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

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

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

Let’s Get Started

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
EDUCBA

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

Forgot Password?

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