PowerShell offers a variety of comparison operators that are used to achieve a couple of functionalities such as matching some particular patterns and comparing different values. There are different categories of the comparison operators such as equality, replacement, matching, etc. If we talk about the like operator it falls in the category of match operator.

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.

Basic Syntax

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.

‘Welcome to linuxhint.com’ -like ‘*welcome*’

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/How-to-use-like-Operator-in-PowerShell-Where-Object-1.png" data-lazy- height="298" src="data:image/svg xml,” width=”881″>

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:

‘Welcome to linuxhint.com’ -like ‘welcome’

When we run the above cmdlet, we get the following result:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/How-to-use-like-Operator-in-PowerShell-Where-Object-2.png" data-lazy- height="251" src="data:image/svg xml,” width=”892″>

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:

‘welcome’ -like ‘welcome’

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/How-to-use-like-Operator-in-PowerShell-Where-Object-3.png" data-lazy- height="242" src="data:image/svg xml,” width=”916″>

The output shows that this time the like operator returns true.

Conclusion

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.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/FA9AFCF66E2D44C3A65378AC87A87EB4-150×150.jpg62a2a3a96e7de.jpg" height="112" src="data:image/svg xml,” width=”112″>

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.