PowerShell If Array Contains [With Examples]

Do you want to know about the PowerShell if array contains? This PowerShell tutorial will explore different methods to check if an array contains a particular element in PowerShell with examples.

In PowerShell, to check if an array contains a specific value, you can use the -contains operator. For example, $array -contains ‘value’ will return True if ‘value’ is present in $array. This operator is a straightforward and effective way to search for an item within an array.

PowerShell If Array Contains

Now, let us check out PowerShell if array contains, with examples.

Using the -contains Operator

One of the simplest ways to check if an array contains a specific value is by using the -contains operator in PowerShell. This operator tests whether a collection (like an array) includes a certain item.

Here’s an example:

$array = 'apple', 'banana', 'cherry'
$valueToCheck = 'banana'

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

In this example, $array is an array of fruits, and $valueToCheck is the value we’re looking for in the array. The -contains operator returns $true if the value is found, and $false otherwise.

You can see the output in the screenshot below:

PowerShell If Array Contains

Using the .Contains() Method

Another way to check for the presence of an element is using the .Contains() method. This method is a part of the .NET Framework and can be used with arrays in PowerShell.

Here’s how you can use .Contains():

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

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

In this code snippet, we’ve defined an array of integers, and we’re checking to see if it contains the number 3. Just like with -contains, .Contains() will return a Boolean value indicating the presence of the item.

Using the Compare-Object Cmdlet

When you need to compare two arrays and find out if one contains elements of the other, the Compare-Object cmdlet can be very useful. This cmdlet compares two sets of objects.

Here’s an example of using Compare-Object:

$array1 = 'red', 'green', 'blue'
$array2 = 'green', 'yellow'

$result = Compare-Object -ReferenceObject $array1 -DifferenceObject $array2 -IncludeEqual

if ($result.SideIndicator -eq "==") {
    Write-Host "Array1 contains elements of Array2."
} else {
    Write-Host "Array1 does not contain all elements of Array2."
}

In this example, $result will contain the differences between the two arrays. The SideIndicator property indicates whether the elements are equal or which array they belong to.

Using Custom Functions

You might want to create a custom function for more complex checks or to make your code reusable. Here’s a simple function that checks if an array contains a specific value:

function Test-ArrayContains {
    param(
        [array]$Array,
        $Value
    )

    return $Array -contains $Value
}

$array = 10, 20, 30, 40, 50
$valueToCheck = 20

if (Test-ArrayContains -Array $array -Value $valueToCheck) {
    Write-Host "The array contains $valueToCheck."
} else {
    Write-Host "The array does not contain $valueToCheck."
}

This function, Test-ArrayContains, takes two parameters: an array and a value to check for. It uses the -contains operator to determine if the value is in the array and returns the result.

Handling Arrays of Arrays

Sometimes, you might encounter a situation where you need to check if an array of arrays contains a specific sub-array. This is a bit more complex since the -contains operator and .Contains() method check for the presence of a single value, not a collection of values.

To check if an array of arrays contains a specific array, you can use a combination of the Compare-Object cmdlet and a loop. Here’s an example:

$arrayOfArrays = @( @(1, 2), @(3, 4), @(5, 6) )
$subArrayToCheck = @(3, 4)

$containsSubArray = $false

foreach ($subArray in $arrayOfArrays) {
    if (-not (Compare-Object $subArray $subArrayToCheck)) {
        $containsSubArray = $true
        break
    }
}

if ($containsSubArray) {
    Write-Host "The array of arrays contains the sub-array."
} else {
    Write-Host "The array of arrays does not contain the sub-array."
}

In this script, we loop through each sub-array in $arrayOfArrays and use Compare-Object to check if it is equal to $subArrayToCheck. If a match is found, we set $containsSubArray to $true and exit the loop.

Conclusion

PowerShell provides several methods for determining if an array contains a specific value or set of values. Whether you’re using the -contains operator, the .Contains() method, the Compare-Object cmdlet, or writing custom functions, you now have the tools to perform these checks effectively.

In this PowerShell tutorial, I have explained with examples “PowerShell If Array Contains“.

You may also like: