PowerShell – Containment operators

PowerShell containment operators are used to verify whether the given value is available in the given input values. These operators always return the boolean value; True or False.

Below are the containment operators PowerShell provides, and these take 2 or more operands; left-side operands hold the input value and the right-side operand is for the value to verify in the input values. Similar to other comparison operators, these operators also take multiple operands as input values. The only difference is, these operators always return the value, True or False; even though we pass collections as input values.

-contains operator

This operator verifies whether any of the input values contains the value given in the right-hand side operand. As mentioned above, input values are left side of the operator, these can be multiple values, and the right-hand side value is the one we are going to verify in the left-side input values. Note that the comparison is not case-sensitive.

Here are some examples, of usage of this operator;

PS C:\> 1 -contains 1
True
PS C:\> "PowerShell" -contains "Shell"
False
PS C:\> "Power", "Shell" -contains "Shell"
True
PS C:\> "Boolean", "Bool" -contains "bool"
True
PS C:\> 11 -contains 1
False
PS C:\> 1, 2, 3 -contains 1
True

-notcontains operator

This operator exactly does the reverse of the above operator. That means, it returns the value True, when the given value does not exist in the input values. All other rules are the same as the above operator.

Here are some examples of this operator;

PS C:\> 1 -notcontains 1
False
PS C:\> 111 -notcontains 111
False
PS C:\> "Python", "R" -notcontains "R"
False
PS C:\> 123 -notcontains 2
True

PowerShell provides below containment operators and which are similar to the operators we have discussed above. And these take 2 or more operands; the right-side operands are for the input values and the left-side operand is for the value to verify in the input values. As above, these operators also take multiple operands as input values; and these always return True or False; even though we pass collections as input values.

-in operator

This operator returns the value True if the left-side operand’s value exists in the set of input values provided through its’ right-side operand. Otherwise, it returns the value, False.

PS C:\> "Apple" -in ("Apple", "Cider", "Vinegar")
True

-notin operator

This operator returns the value False┬áif the left-side operand’s value exists in the set of input values provided through its’ right-side operand. Otherwise, it returns the value, True.

PS C:\> "Salad" -notin ("Fruit", "Salad")
False

[..] David

PowerShell – Containment operators

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top