Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between `-Contains` and `-In` in PowerShell?

Tags:

powershell

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?

like image 761
Jacob Krall Avatar asked Aug 06 '15 20:08

Jacob Krall


People also ask

What does contains do in PowerShell?

In PowerShell, the contains operator, doesn't do substring comparison. But instead, it's a collection operator. This means that it will test if a collection contains the matching value. It can't be used to find a character or substring inside a string.

What does %% mean in PowerShell?

% is an alias for the ForEach-Object cmdlet. An alias is just another name by which you can reference a cmdlet or function.

What does GT mean in PowerShell?

-gt. Greater than. -ge. Greater than or equal. You don't need an if statement to test the result of a comparison operation.

What does ampersand mean in PowerShell?

The ampersand tells PowerShell to execute the scriptblock expression. In essence, it's telling PowerShell that the stuff between the curly braces is code and to run it as code. Passing Arguments to Scriptblocks. Like functions, you can pass "parameters" to scriptblocks also known as arguments.


1 Answers

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.

like image 171
briantist Avatar answered Oct 22 '22 12:10

briantist