0

I found some strange behavior in PowerShell, and can't figure out why it behaves this way. Out of the three following lines of code, why is it that only -match returns what I want, and the other two commands return nothing?

Get-Service | ?{$_.Name -like "sql"}
Get-Service | ?{$_.Name.Contains("sql")}
Get-Service | ?{$_.Name -match "sql"}      # This one works?

1 Answer 1

4

-like uses a wildcard match. If you don't use any wildcards, it will do an exact match (not case-sensitive).

The .Contains method of the [String] object is case-sensitive.

The -match operator uses a regular expression (not case-sensitive). Since the input string (the name of the service) exists in your pattern, it returns $true.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.