Array Contains in PowerShell [With Examples]

Recently, I got one requirement from a client to check if an array contains a value in PowerShell. There are various methods to do this thing in PowerShell. In this PowerShell tutorial, I will show you a few examples of an array contains in PowerShell, including if an array contains a string 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

Check out Add an Element to the Beginning of an Array 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

Check out Add Value to an Array in a Function in PowerShell

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

Check out Add Values to a Multidimensional Array in PowerShell

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.

Read Add Only Unique Values to an Array in PowerShell

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.

Check out Add Values to an Array in PowerShell

Array Contains String in PowerShell

Let me show you an example of how to check if an array contains a string in PowerShell.

To check if a PowerShell array contains a specific string, you can use the -contains operator. Here’s an example:

# Define an array of city names
$cities = @("New York", "Los Angeles", "Chicago", "Houston", "Phoenix")

# Check if the array contains the city "Chicago"
if ($cities -contains "Chicago") {
    Write-Output "The array contains Chicago."
} else {
    Write-Output "The array does not contain Chicago."
}

In this example, the array $cities contains several city names. The -contains operator is used to check if “Chicago” is in the array. If it is, the script outputs “The array contains Chicago.” Otherwise, it outputs “The array does not contain Chicago.”

You can also check out the screenshot below:

powershell array contains string

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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.