How to use like Operator in PowerShell Where-Object?
In this post, we will explore the different aspects of the “like” operator in PowerShell “Where-object” as listed below:
- What is a “like” Operator in PowerShell?
- Basic Syntax
- How to use a “like” Operator in PowerShell Where-Object?
So let’s begin!
What is a “like” Operator in PowerShell?
-like, -match, -notlike, -notmatch, all these operators belong to the same category i.e. “Matching”. These operators are used to find out whether a specific string matches the wildcard/regex pattern or not. If we talk about the “like” Operator, it is specifically used to match a wildcard pattern within a string.
The below snippet shows how to use the like operator in PowerShell:
The like operator will return true if a match is found in the string, and false if the matching value is not found in the targeted string.
How to use a “like” Operator in PowerShell Where-Object?
This section will explore how to use the -like operator in PowerShell, to do that, it will consider a couple of examples.
In the above-given piece of code, we were looking for the word “welcome” in the string, and to do that we utilized the -like operator along with the wildcard “*”. The output for the above cmdlet will go like this:
The -like operator returned true, it verified that the match “welcome” found in the specified string.
Let’s modify the cmdlet a little bit to check what will happen if didn’t utilize the wildcard operator:
When we run the above cmdlet, we get the following result:
What went wrong this time? Why do we get a false value?
In the first example, we utilized the wildcards which say that there can be some text on either side of the targeted word i.e. “welcome”. While the string in which we didn’t utilize the wildcards will look for the exact match (nothing more, nothing less). This means it will return true only if it finds the exact word i.e. there is not a single character on either side of the targeted word as shown in the below snippet.
Let’s run the below cmdlet on Windows PowerShell:
The output shows that this time the like operator returns true.
In PowerShell, the “like” Operator belongs to the category of matching operators and is specifically used to match a wildcard pattern within a string. The like operator will return true if a match is found in the string, and false if the matching value is not found in the specified string. This post explained how to use the like operator in PowerShell Where-Object with the help of some suitable examples.