PowerTip: Does PowerShell Array Contain a Value?
Summary: Learn how to verify if a Windows PowerShell array contains a value.
I have an array that contains various numbers and I want to see if one of the array elements contains
a specific number. Is there an easy way to use Windows PowerShell so I don’t have to iterate through
the array and compare each element to the specific number?
Use the Contains method from an array. In the following example, the $array variable contains an array.
The next line checks to see if the number 2 is in the array. It is, and the method returns True.
Next, the Contains method checks for the number 12. It is not present and the method returns False.
PS C:> $array = 1,2,5,8,3,4,5
PS C:> $array.Contains(2)
PS C:> $array.Contains(12)