Home/PowerShell/PowerShell – Matching Operators

PowerShell – Matching Operators

-like and -notlike Operators

The like operators find elements that match or do not match a specified pattern using wildcard expressions.

How its handle single & Collection values?

If we input scalar value, comparison operators return a Boolean value. When the input is a collection of values, the comparison operators return any matching values. If there are no matches found in a collection then the comparison operators do not return anything.

The syntax is:

<string[]> -like <wildcard-expression>
<string[]> -notlike <wildcard-expression>

Example:1

OUTPUT:

Example:2

OUTPUT:

-match and -notmatch Operators

The match operators find elements that match or do not match a specified pattern using regular expressions. It search only in strings and they can’t search in arrays of integers or other objects.

The syntax:

<string[]> -match <regular-expression>
<string[]> -notmatch <regular-expression>

Example:1

OUTPUT:

Example:2

OUTPUT:

Note:

There is automatic variable called “matches” element which return the match elements. You can use $matches variable to get the matched value.

What is difference between -Like & -Match?

The most technical distinction is -Match is a regular expression, whereas -Like is just a wildcard comparison, a subset of -Match. If you need a wildcard to find this item’, then start with -Like. However, if you are sure that most of the letters in the value that you are looking for, then you are better you can go with -Match. -match operator is quite faster than -like operator.

Leave A Comment