Updated March 3, 2023
Introduction to PowerShell not like
The following article provides an outline for PowerShell not like. Comparison operators let the users indicate conditions for comparing values and finding values that satisfies the mentioned condition. To utilize a comparison operator, indicate the values that you just need to compare along with an operator that isolates these values. The not like operator is a type of matching comparison operator. This along with like, match and not match operators are part of the matching comparison operator type. The not like operator returns true if the string doesn’t match the specified condition. Here we will see about not like operator along with other matching operators.
Syntax of not like operator:
Given below is the syntax of not like operator:
<string[]> -notlike <wildcard-expression>
Example:
Code:
"vigneshkrishnakumar" -notlike "*mar"
"vigneshkrishnakumar" -notlike "*sethu"
Output:
Working of not like Operator
The like operator uses wild card expression for comparison. For example, let us consider a sentence “how old are you?” If we execute the cmdlet “how old are you” -like “*you”, the cmdlet will return true however if the cmdlet is modified to “how old are you” -like “you”, the cmdlet will return false. It is due to the asterisk symbol before you. The asterisk symbol stands for wild card expressions.
If the wildcard is present in the beginning of the comparison text, it means anything can be before the searched expression whereas if the asterisk is present at the end, it means the searched expression should have something followed by it. In the above example if the asterisk is placed after the text it will return false. The not like operator is the exact opposite of the like operator. It will return true when there the condition is false and false when there is a match.
Example:
Code:
Write-Host " wild card before the comparison text" -ForegroundColor Green
“my name is Vignesh krishnakumar” -like “*is”
Write-Host "no wild card involved" -ForegroundColor Green
“my name is Vignesh krishnakumar” -like “is”
Write-Host "wild card after the comparison text"-ForegroundColor Green
“my name is Vignesh krishnakumar” -like “is*”
Write-Host " wild card before the comparison text" -ForegroundColor Green
“my name is Vignesh krishnakumar” -notlike “*is”
Write-Host "no wild card involved" -ForegroundColor Green
“my name is Vignesh krishnakumar” -notlike “is”
Write-Host "wild card after the comparison text"-ForegroundColor Green
“my name is Vignesh krishnakumar” -notlike “is*”
Output:
Comparing like and not like with match and not match
Another type of comparison operator is match / non match operator. Although comparative to Like, it’s much more PowerShell (however a small more complicated). The match or non match operators administrator employments customary expressions (regex). This is often a gigantic advantage and gives Coordinate an unequivocal leg up on Like. In any case, in case you’ve never utilized customary expressions some time recently, plan yourself. Let’s go with our past illustration string once more.
Syntax for match and not match operator:
<string[]> -match <regular-expression>
<string[]> -notmatch <regular-expression>
Example #1:
Code:
"sun", "mon","tue","wed","thu","fri","sat" -match "su"
"viki", "vignesh","vikiii","vikii","nandhini","vyapini","sethu" -notmatch "viki"
Output:
Example #2:
Code:
Write-Host " wild card before the comparison text" -ForegroundColor Green
“my name is Vignesh krishnakumar” -match “*is”
Write-Host "no wild card involved" -ForegroundColor Green
“my name is Vignesh krishnakumar” -match “is”
Write-Host "wild card after the comparison text"-ForegroundColor Green
“my name is Vignesh krishnakumar” -match “is*”
Write-Host " wild card before the comparison text" -ForegroundColor Green
“my name is Vignesh krishnakumar” -notmatch “*is”
Write-Host "no wild card involved" -ForegroundColor Green
“my name is Vignesh krishnakumar” -notmatch “is”
Write-Host "wild card after the comparison text"-ForegroundColor Green
“my name is Vignesh krishnakumar” -notmatch “is*”
Output:
As you can see from the above output, the match operator doesn’t work with wild card expressions.
Example #3:
Code:
$start=(Get-Date).Millisecond
Write-Host " wild card before the comparison text" -ForegroundColor Green
“corona originated in china but now it is cured there” -like “*there”
Write-Host "no wild card involved" -ForegroundColor Green
“corona originated in china but now it is cured there” -like “there”
Write-Host "wild card after the comparison text"-ForegroundColor Green
“corona originated in china but now it is cured there” -like “there*”
Write-Host "wild card before the comparison text"-ForegroundColor Green
“corona originated in china but now it is cured there” -notlike “*there”
Write-Host "no wild card involved" -ForegroundColor Green
“corona originated in china but now it is cured there” -notlike “there”
Write-Host "wild card after the comparison text" -ForegroundColor Green
“corona originated in china but now it is cured there” -notlike “there*”
$end=(Get-Date).Millisec
Write-Host "Total time in milli seconds for like and not like: $($start-$end)" -ForegroundColor Yellow
$start1=(Get-Date).Millisecond
Write-Host " wild card before the comparison text" -ForegroundColor Green
“corona originated in china but now it is cured there” -match “there”
Write-Host "no wild card involved" -ForegroundColor Green
“corona originated in china but now it is cured there” -match “now”
Write-Host "wild card after the comparison text"-ForegroundColor Green
“corona originated in china but now it is cured there” -match “america”
Write-Host "wild card before the comparison text"-ForegroundColor Green
“corona originated in china but now it is cured there” -notmatch “america”
Write-Host "no wild card involved" -ForegroundColor Green
“corona originated in china but now it is cured there” -notmatch “sweden”
Write-Host "wild card after the comparison text" -ForegroundColor Green
“corona originated in china but now it is cured there” -notmatch "china”
$end1=(Get-Date).Millisec
Write-Host "Total time in milli seconds for match and not match: $($start1-$end1)" -ForegroundColor Yellow
Output:
As you can see from the above output, like and not like took lesser time to execute.
Conclusion – PowerShell not like
Thus, in this article we saw about the not like operator in detail. Here we saw the syntax, the usage and its advantage with appropriate examples. Here we also saw the match and not match operators and its similarities and differences with not like operator.
Recommended Articles
This is a guide to PowerShell not like. Here we discuss working of not like operator and comparing like and not like with match and not match. You may also have a look at the following articles to learn more –