-like and -notlike Operators
The like operators find elements that match or do not match a specified pattern using wildcard expressions.
How its handle single & Collection values?
If we input scalar value, comparison operators return a Boolean value. When the input is a collection of values, the comparison operators return any matching values. If there are no matches found in a collection then the comparison operators do not return anything.
The syntax is:
<string[]> -like <wildcard-expression>
<string[]> -notlike <wildcard-expression>
Example:1
1 2 3 4 5 6 |
PS C:\> $singleValue = "Aadharsh" $singleValue = $singleValue -like "Aadharsh" Write-Host "" Write-Host "Is varible contain 'Aadharsh' : " $singleValue |
OUTPUT:
1 |
Is varible contain 'Aadharsh' : True |
Example:2
1 2 3 4 |
$singleValue = "Aadharsh" , "Rakshu" $singleValue = $singleValue -like "Aadharsh" Write-Host "" Write-Host "The matched value is : " $singleValue |
OUTPUT:
1 |
The matched value is : Aadharsh |
-match and -notmatch Operators
The match operators find elements that match or do not match a specified pattern using regular expressions.ย It search only in strings and they can’t search in arrays of integers or other objects.
The syntax:
<string[]> -match <regular-expression>
<string[]> -notmatch <regular-expression>
Example:1
1 2 3 4 5 6 |
PS C:\> $singleValue = "Aadharsh" $singleValue = $singleValue -match "Aadharsh" Write-Host "" Write-Host "Is variable contain 'Aadharsh' : " $singleValue |
OUTPUT:
1 |
Is variable contain 'Aadharsh' : True |
Example:2
1 2 3 4 |
$singleValue = "Aadharsh" , "Rakshu" $singleValue = $singleValue -like "Aadharsh" Write-Host "" Write-Host "The matched value is : " $singleValue |
OUTPUT:
1 |
The matched value is : Aadharsh |
Note:
There is automatic variable called “matches” element which return the match elements. You can use $matches variable to get the matched value.
What is difference between -Like & -Match?
The most technical distinction is -Match is a regular expression, whereas -Like is just a wildcard comparison, a subset of -Match. If you need a wildcard to find this item’, then start with -Like. However, if you are sure that most of the letters in the value that you are looking for, then you are better you can go with -Match. -match operator is quite faster than -like operator.
Leave A Comment