# -Contains isn't -Like

Oh, if I had a nickel for every time I've seen this:

![image015.png](https://4171205829-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LA8qNDXlOx79WorVawZ%2F-LA8qTOf8jN0CwdQbcQM%2F-LA8qUblpRiDUX5ZzpJG%2Fimage015.png?generation=1523804930446492\&alt=media)

I get how this happens. The -contains operator seems like it should be checking to see if a process' name contains the letters "notepad." But that isn't what it does.

The correct approach is to use the -like operator, which in fact does do a wildcard string comparison:

![image017.png](https://4171205829-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LA8qNDXlOx79WorVawZ%2F-LA8qTOf8jN0CwdQbcQM%2F-LA8qUbtomM0cV4_UEYr%2Fimage017.png?generation=1523804931072816\&alt=media)

I'll let pass the thought that the really correct answer is to just run Stop-Process -name \*notepad\*, because I was aiming for a simple example here. But... don't overthink things. Sometimes a script and a ForEach loop isn't the best approach.

So anyway, what does -contains (and its friend, -notcontains) actually do? They're similar to the -in and -notin operators introduced in PowerShell v3, and those operators cause more than a bit of confusion, too. What they do is check to see if a collection of objects contains a given single object. For example:

![image019.png](https://4171205829-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LA8qNDXlOx79WorVawZ%2F-LA8qTOf8jN0CwdQbcQM%2F-LA8qUcN8QlqQXFFqMW-%2Fimage019.png?generation=1523804930395996\&alt=media)

In fact, that example is probably the best way to see it work. The trick is that, when you use a complex object instead of a simple value (as I did in that example), -contains and -in look at every property of the object to make a match. If you think about something like a process, they're always changing. From moment to moment, a process' CPU and memory, for example, are different.

![image021.png](https://4171205829-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LA8qNDXlOx79WorVawZ%2F-LA8qTOf8jN0CwdQbcQM%2F-LA8qUchtJDBEyOj7Rv5%2Fimage021.png?generation=1523804930449204\&alt=media)

In this example, I've started Notepad. I've put its process object into $single\_proc, and you can see that I verified it was there. But when I run Get-Process and check to see if its collection contained my Notepad, I got False. That's because the object in $single\_proc is out of date. Notepad is running, but it now looks different, so -contains can't find the match.

The -in and -contains operators are best with simple values, or with objects that don't have constantly-changing property values. But they're not wild card string matching operators. Use -like (or -notlike) for that.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://devops-collective-inc.gitbook.io/the-big-book-of-powershell-gotchas/contains-isnt-like.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
