Question:
Reading official docs it’s obvious that PowerShell -match
operator is more powerful than -like
(due to regular expressions). Secondly, it seems ~10 times faster according to this article https://www.pluralsight.com/blog/software-development/powershell-operators-like-match.
Are there specific cases when I should prefer -like
instead of -match
? If there not, why at all should I use -like
? Does it exist because of historical reasons?
Answer:
See this explanation from Differences Between -Like
and -Match
In a nutshell, if you are thinking, ‘I am probably going to need a wildcard to find this item‘, then start with
-Like
. However, if you are pretty sure of most of the letters in the word that you are looking for, then you are better off experimenting with-Match
.Here is a more technical distinction:
-Match
is a regular expression, whereas-Like
is just a wildcard comparison, a subset of-Match
.
So, whenever you are not sure what character classes, i.e. digits, letters, punctuation, etc., can there be inside, when you just want to match any character, you should be using -Like
with its wildcards.
When you know there must be a digit at the start followed with 1+ sequences of a colon followed with alphanumeric characters up to the end of the string, use -Match
with its powerful regular expressions.