Array Contains in PowerShell [With Examples]

One common task in PowerShell is to check if an array contains a certain value. This can be done using different methods, and in this PowerShell tutorial, we will explore various methods related to array contains in PowerShell.

To check if an array contains a specific value in PowerShell, use the -contains operator. For example, $array -contains ‘value’ will return $true if ‘value’ is in $array, otherwise $false. For more complex checks, such as within arrays of objects, you might use Where-Object or other filtering cmdlets.

Array Contains in PowerShell

There are different methods by which we can check if an array contains a value in PowerShell. Here are those:

Method 1: The -contains Operator

The simplest way to check if an array contains a certain value in PowerShell is by using the -contains operator. This operator returns $true if the array contains the specified value, and $false otherwise.

Here’s an example:

$myArray = @(1, 2, 3, 4, 5)
$valueToCheck = 3

if ($myArray -contains $valueToCheck) {
    Write-Host "The array contains the value $valueToCheck."
} else {
    Write-Host "The array does not contain the value $valueToCheck."
}

This will output:

The array contains the value 3.

I executed the PowerShell script using VS code, and you can see the output in the screenshot below:

Array Contains in PowerShell

Method 2: The .Contains() Method

Another way to check if an array contains a certain value is by using the .Contains() method of an array in PowerShell. This method works similarly to the -contains operator.

Example:

$myArray = @(1, 2, 3, 4, 5)
$valueToCheck = 3

if ($myArray.Contains($valueToCheck)) {
    Write-Host "The array contains the value $valueToCheck."
} else {
    Write-Host "The array does not contain the value $valueToCheck."
}

The output will be the same as the previous example.

You can see the output in the screenshot below:

PowerShell array contains

Method 3: The Where-Object Cmdlet

You can also use the Where-Object cmdlet to filter the PowerShell array and check if it contains a certain value. This is a bit more verbose but can be useful in more complex scenarios.

Here’s how you can use it:

$myArray = @(1, 2, 3, 4, 5)
$valueToCheck = 3

$containsValue = $myArray | Where-Object { $_ -eq $valueToCheck }
if ($containsValue) {
    Write-Host "The array contains the value $valueToCheck."
} else {
    Write-Host "The array does not contain the value $valueToCheck."
}

This will give you the same result as the other methods.

You can see the output in the screenshot below:

Array Contains in PowerShell examples

Method 4: The ForEach-Object Cmdlet

The ForEach-Object cmdlet can be used to iterate over each element in the array and check for the value in PowerShell. This is not the most efficient method for simply checking if an array contains a value, but it’s good to know how to iterate over an array.

Example:

$myArray = @(1, 2, 3, 4, 5)
$valueToCheck = 3
$containsValue = $false

$myArray | ForEach-Object {
    if ($_ -eq $valueToCheck) {
        $containsValue = $true
    }
}

if ($containsValue) {
    Write-Host "The array contains the value $valueToCheck."
} else {
    Write-Host "The array does not contain the value $valueToCheck."
}

Again, this will output that the value 3 is contained within the array.

Method 5: The -in Operator

The -in operator is similar to the PowerShell -contains, but the syntax is reversed. Instead of specifying the array first, you specify the value you’re looking for first.

Here’s an example:

$valueToCheck = 3
$myArray = @(1, 2, 3, 4, 5)

if ($valueToCheck -in $myArray) {
    Write-Host "The array contains the value $valueToCheck."
} else {
    Write-Host "The array does not contain the value $valueToCheck."
}

The result will be the same as with the -contains operator.

Conclusion

Checking if an array contains a certain value in PowerShell is a very common requirement. In this PowerShell tutorial, I have explained five different methods for array contains in PowerShell.

  • The -contains Operator
  • The .Contains() Method
  • The Where-Object Cmdlet
  • The ForEach-Object Cmdlet
  • The -in Operator

The -contains operator and the .Contains() method are straightforward and easy to use. The Where-Object and ForEach-Object cmdlets provide more flexibility and are useful in more complex filtering scenarios. Lastly, the -in operator offers an alternative syntax that some may find more readable.

You may also like: