PowerShell and the -contains operator

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.

4 Answers
4

Leave a Comment