Contains and in

The -contains, -notcontains, -in, and -notin operators are used to test the content of arrays.

When using -contains or -notcontains, the array is expected to be on the left-hand side of the operator:

1, 2 -contains 2       # Returns true 
1, 2, 3 -contains 4    # Returns false 

When using -in or -notin, the array is expected to be on the right-hand side of the operator:

1 -in 1, 2, 3    # Returns true 
4 -in 1, 2, 3    # Returns false                   
Contains or in?
When using comparison operators, I tend to write the subject on the left, the object on the right. Comparisons to null are an exception to the rule.
The subject is the variable or property I am testing; the object is the thing I test against. For example, I might set the subject to a user in Active Directory:
$subject = Get-ADUser -Identity $env:USERNAME -Properties department, memberOf
I use contains where the subject is an array, and the object is a single value:
$subject.MemberOf -contains 'CN=Group,DC=domain,DC=example'
I use in where the subject is a single value, and the object is an array:
$subject.Department -in 'Department1', 'Department2'
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.14.93