The best way to use like Operator in PowerShell The place-Object?

[ad_1]

PowerShell presents quite a lot of comparability operators which might be used to realize a few functionalities similar to matching some specific patterns and evaluating completely different values. There are completely different classes of the comparability operators similar to equality, alternative, matching, and many others. If we speak concerning the like operator it falls within the class of match operator.

On this put up, we’ll discover the completely different features of the “like” operator in PowerShell “The place-object” as listed under:

  • What’s a “like” Operator in PowerShell?
  • Fundamental Syntax
  • The best way to use a “like” Operator in PowerShell The place-Object?

So let’s start!

What’s a “like” Operator in PowerShell?

-like, -match, -notlike, -notmatch, all these operators belong to the identical class i.e. “Matching”. These operators are used to seek out out whether or not a selected string matches the wildcard/regex sample or not. If we speak concerning the “like” Operator, it’s particularly used to match a wildcard sample inside a string.

Fundamental Syntax

The under snippet reveals find out how to use the like operator in PowerShell:

The like operator will return true if a match is discovered within the string, and false if the matching worth isn’t discovered within the focused string.

The best way to use a “like” Operator in PowerShell The place-Object?

This part will discover find out how to use the -like operator in PowerShell, to do this, it is going to think about a few examples.

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

Within the above-given piece of code, we have been searching for the phrase “welcome” within the string, and to do this we utilized the -like operator together with the wildcard “*”. The output for the above cmdlet will go like this:

The -like operator returned true, it verified that the match “welcome” discovered within the specified string.

Let’s modify the cmdlet somewhat bit to test what’s going to occur if didn’t make the most of the wildcard operator:

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

Once we run the above cmdlet, we get the next outcome:

What went flawed this time? Why can we get a false worth?

Within the first instance, we utilized the wildcards which say that there could be some textual content on both aspect of the focused phrase i.e. “welcome”. Whereas the string wherein we didn’t make the most of the wildcards will search for the precise match (nothing extra, nothing much less). This implies it is going to return true provided that it finds the precise phrase i.e. there may be not a single character on both aspect of the focused phrase as proven within the under snippet.

Let’s run the under cmdlet on Home windows PowerShell:

‘welcome’ -like ‘welcome’

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

Conclusion

In PowerShell, the “like” Operator belongs to the class of matching operators and is particularly used to match a wildcard sample inside a string. The like operator will return true if a match is discovered within the string, and false if the matching worth isn’t discovered within the specified string. This put up defined find out how to use the like operator in PowerShell The place-Object with the assistance of some appropriate examples.

[ad_2]

Supply hyperlink

Leave a Reply

Your email address will not be published.