Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

-contains VS -in

Microsoft's documentation has some duplicate containment operators, -contains vs -in & -notContains vs -notIn. The description makes it sound like these are basically identical, -contains is described as "Returns true when reference value contained in a collection" while -in is described as "Returns true when test value contained in a collection". What exactly is the difference between "reference value" and "test value" in this context?

And, are the options available all the way back to PS 2.0, or did -in & -notIn get introduced later? Given the many changes, I really wish MS would include this info in their documentation, rather than just assuming everyone is on the current version. But I digress. :)

like image 987
Gordon Avatar asked Jan 30 '26 10:01

Gordon


1 Answers

This is for ease of use, since PowerShell is designed to be as comfortable to type (and read) in the shell as possible.

For -contains the collection is on the left-hand side:

(1, 2) -contains 1

The opposite case is the -in operator where the collection is on the right-hand side:

1 -in (1, 2)

This for is the very same reason why the negated versions exist too, simply because this:

1 -notin (1, 2)

is easier to type than this:

-not (1 -in (1, 2))
like image 144
marsze Avatar answered Feb 02 '26 07:02

marsze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!