PowerShell and the -contains operator

PowershellOperatorsString Matching

Powershell Problem Overview


Consider the following snippet:

"12-18" -Contains "-"

You’d think this evaluates to true, but it doesn't. This will evaluate to false instead. I’m not sure why this happens, but it does.

To avoid this, you can use this instead:

"12-18".Contains("-")

Now the expression will evaluate to true.

Why does the first code snippet behave like that? Is there something special about - that doesn't play nicely with -Contains? The documentation doesn't mention anything about it.

Powershell Solutions


Solution 1 - Powershell

The -Contains operator doesn't do substring comparisons and the match must be on a complete string and is used to search collections.

From the documentation you linked to:

> -Contains > Description: Containment operator. Tells whether a collection of reference values includes a single test value.

In the example you provided you're working with a collection containing just one string item.

If you read the documentation you linked to you'll see an example that demonstrates this behaviour:

Examples:

PS C:\> "abc", "def" -Contains "def"
True

PS C:\> "Windows", "PowerShell" -Contains "Shell"
False  #Not an exact match

I think what you want is the -Match operator:

"12-18" -Match "-"

Which returns True.

Important: As pointed out in the comments and in the linked documentation, it should be noted that the -Match operator uses regular expressions to perform text matching.

Solution 2 - Powershell

-Contains is actually a collection operator. It is true if the collection contains the object. It is not limited to strings.

-match and -imatch are regular expression string matchers, and set automatic variables to use with captures.

-like, -ilike are SQL-like matchers.

Solution 3 - Powershell

You can use like:

"12-18" -like "*-*"

Or split for contains:

"12-18" -split "" -contains "-"

Solution 4 - Powershell

  • like is best, or at least easiest.
  • match is used for regex comparisons.

Reference: About Comparison Operators

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontnwView Question on Stackoverflow
Solution 1 - PowershellKevView Answer on Stackoverflow
Solution 2 - PowershellErisView Answer on Stackoverflow
Solution 3 - PowershellEsperento57View Answer on Stackoverflow
Solution 4 - PowershellMaximojoView Answer on Stackoverflow