20

Other than the obvious reversal of the parameter order, what is the difference between the -Contains operator and PowerShell 3.0's new -In operator?

Jacob Krall
  • 28,341
  • 6
  • 66
  • 76

2 Answers2

18

In short, there is no difference that you haven't already described.

The difference is which value is on the left vs. the right side:

$arr = @(4,5,6)

$arr -contains 5
5 -in $arr

The other difference is that -in was introduced in PowerShell 3.0, so it won't work on earlier versions.

It's mostly a style thing; use the one that feels more natural for a specific situation.

briantist
  • 45,546
  • 6
  • 82
  • 127
  • 1
    PowerShell adopts Perl's "TMTOWTDI" wholeheartedly, I guess. – Jacob Krall Aug 07 '15 at 17:22
  • @briantist I know that `-contains` does not work with wildcard notation. Based off your post `-in` does not either? – Speerian Aug 07 '15 at 18:31
  • 1
    @Speerian if you mean using `-like` syntax in a string such as `'apple','grapple','bot' -contains '*pple'`, then that is correct; it doesn't work with `-in` either. – briantist Aug 07 '15 at 18:39
6

same same but different...

-contains check if array contains value:

$Array -contains $value

-in check if value in array:

$value -in $Array

Example:

$Array = 1..5

$Array -Contains 4
True

4 -in $Array
True
Avshalom
  • 8,657
  • 1
  • 25
  • 43