Powershell

How to Use like Operator in PowerShell Where-Object?

PowerShell is a CLI (Command line Interface) tool used by system administrators to perform automation tasks. It is supported on Windows and other Unix operating systems. PowerShell has a variety of comparison operators. In simple terms, it uses comparison operators to compare two values and output a result in boolean values, such as “True” or “False”. Moreover, comparison operators include equal, not equal, greater than, less than, like, or not like operators.

This article will guide about:

What is “like” Operator in PowerShell?

The “like” operator in PowerShell is one of the matching operators. It is used for finding elements in the objects according to the specified condition using regular expressions. You can also utilize it to determine whether a specified string is present in the corresponding string.

How Does “like” Operator Work in PowerShell?

The “like” operator uses the “Wildcard character *” to match the corresponding strings’ exact strings. It can represent one or multiple exact characters in the string that are placed before.

How to Use/Utilize the “like” Operator in PowerShell Where-Object?

Here is the basic syntax to utilize the “-like” operator:

<String> -like <regular-expression>

Now, head towards the given examples to view the variations of using the like operator in PowerShell.

Example 1: Using “like” Operator for Matching a Part of a String

In the corresponding example, we will look for “This” in the “This is linuxhint” string by using the “-like” operator:

>'This is linuxhint' -like 'this'

The output is “False” because both strings are not the same.

Example 2: Using “like” Operator for Matching a Whole String

When both of the strings are the same, then the output will be returned as “True”:

>'This is linuxhint' -like 'This is linuxhint'

Output

Example 3: Using “like” Operator for Matching a Partial String

In this example, look for the partial string in the corresponding string using the “Wildcard character *”. It permits both strings not to be the same (as a whole) to get the “True” output. If the partial string is found in the other string, we will get the same output:

>'This is linuxhint' -like '*this*'

Output

The output is “True” because the partial string is found in the corresponding string.

Example 4: Using “like” Operator for Matching With a Variable Value (Without Wildcard)

Let’s assign a string to a variable and then use the “-like” operator to find the matching values:

write-host 'This is an example without wildcard *'
$input = 'This is linuxhint'
$input -like 'linuxhint'

In the above-given script:

  • First, add the “write-host” command to write a text line.
  • After that, assign a string value to the “$input” variable.
  • Finally, look for a matching string stored in the “” variable using the “-like” operator.

Output

The output is “False” because the exact matching string was not found

Example 5: Using “like” Operator for Matching With a Variable Value (With Wildcard)

Now, use the “Wild character *” to find the partial string in the corresponding variable value:

write-host 'This is an example with wildcard *'
$input = 'This is linuxhint'
$input -like '*linuxhint*'

Output

The output is “True”, as the partial string is found in the corresponding variable.

Conclusion

The “like” operator is one of the matching operators used to find a match within the corresponding string. It also uses the wildcard operator to find the partial part of the string. This operator return “True” if it finds the match in the corresponding strings, otherwise, the output will return as “False”. This article has elaborated on the use of the “like” operator in the PowerShell Where-Object.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.