Powershell

Jumpstart Your Game With PowerShell Like Operator (And More)

Just like any other programming language, PowerShell has several comparison operators. In a comparison operator, two values are compared and returned as a boolean value, such as “True” or “False”. These comparison operators include “-Like”, “-Contains”, or “-Match”. However, the symbols of these operators could be different from other programming languages, but their functionality will be the same.

This article will observe several comparison operators including the “-like” operator and others.

Jumpstart Your Game With PowerShell Like Operator (And More)

The approaches that will be discussed are:

Method 1: Like Operator

The ‘like’ operator in PowerShell is a comparison operator. It is used to find the matching objects in the provided string by using a regular expression. It utilizes wild characters to find the exact match in the string. If the specified regular expression is found in the corresponding string, then the output will be “True” else the result will be “False”. In case both the string and the regular expressions are the same then the output will also be “True”.

Example 1: Use the “-like” Operator to Find the Regular Expression in the Corresponding String

This illustration will find a word inside a string using the “-like” operator along with the usage of the wild character:

"This is a Linux Hint portal." -like "*Hint*"

 According to the above code:

    • First, create a string, and then use the “-like” operator.
    • After that, add a word or a partial string wrapped inside the wild characters, within double inverted quotes:

 

Example 2: Use the “-Notlike” Operator to Find Out the Inverse of the “-like” Operator

In this example, the inverse of the “-like” operator will be displayed by using the “-Notlike” operator:

"Linux Hint portal" -Notlike "Linux Hint portal"

 

Method 2: Contains Operator

The “-contains” operator is one of the containment operators. It checks whether a certain value exists in the given set or not. If the value exists there, then the output is “True”, otherwise it is “False”.

Example 1: Use the “-Contains” Operator to Find the Item From the Collection of Items

This illustration will look for the specified item in the collection of items:

$collection = "Car", "Bike", "Cycle"
$collection -contains "Bike"

 
According to the above code:

    • First, initialize a variable “$collection” and then assign several items separated by commas.
    • After that, add an item’s assigned variable, followed by the “-contains” parameter, and define the value “Bike” to it:

 

Example 2: Use the “-notcontains” Operator to Find Out the Inverse of the “-contains” Operator

This example will utilize the “-notcontains” operator to get the inverse of the “-contains” operator:

$collection = "Car", "Bike", "Cycle"
$collection -notcontains "Cycle"

 

Method 3: Match Operator

The “-match” operator is quite similar to the “-like” operator. However, if the single word match is found in the string, then the output will be “True”.

Example 1: Use the “-match” Operator to Match a Part of a String Inside a String

This illustration will find a regular expression match in the string by utilizing the “-match” operator:

$str = "This is Linux Hint."
$str -match "Hint"

 
According to the above code:

    • First, initialize the “$str” variable and assign a string to it.
    • After that, in the next line, first, write the “$str” variable, followed by the “-match” parameter, and specify the regular expression within double inverted quotes:

 

Example 2: Use the “-notmatch” Operator to Find the Inverse of the “-match” Operator

This example will find the inverse of the “-match” operator by using the “-notmatch” operator:

$str = "This is Linux Hint."
$str -notmatch "Hint"

 

We have discussed different comparison operators in PowerShell.

Conclusion

PowerShell’s “-Like” operator is used to find the string containing the regular expression. It is one of the comparison operators. There are also other comparison operators, such as “-contains” and “-match”. The output of comparison operators is always returned as boolean values which are “True” or “False”. This post has illustrated the comparison operators.

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.