How to Check if an Array Contains Another Array in PowerShell?

In this PowerShell tutorial, I will explain how to check if an array contains another array in PowerShell using various methods with examples.

To check if an array contains another array in PowerShell, you can use the Compare-Object cmdlet to compare both arrays. If the smaller array is a subset of the larger one, Compare-Object will return $null when using the -IncludeEqual and -ExcludeDifferent parameters. For example, $result = Compare-Object -ReferenceObject $largeArray -DifferenceObject $smallArray -IncludeEqual -ExcludeDifferent; $containsArray = $result -eq $null will return $true if $largeArray contains all elements of $smallArray, otherwise $false.

Check if an Array Contains Another Array in PowerShell

Suppose you have two PowerShell arrays like below:

$Array1 = 'Red', 'Green', 'Blue'
$Array2 = 'Green', 'Blue'

Let us check a few methods on how to check if an array contains another array in PowerShell.

Method 1: Using Compare-Object

The Compare-Object cmdlet is a built-in PowerShell command that compares two sets of objects. It can be used to check if an array contains all elements of another array.

Here’s how you can use Compare-Object to check array containment:

$Array1 = 'Red', 'Green', 'Blue'
$Array2 = 'Green', 'Blue'

$result = Compare-Object -ReferenceObject $Array1 -DifferenceObject $Array2 -IncludeEqual

if ($result.SideIndicator -contains '==') {
    Write-Host "Array2 is contained in Array1"
} else {
    Write-Host "Array2 is not fully contained in Array1"
}

In this example, Compare-Object compares $Array1 and $Array2. The -IncludeEqual parameter is used to include equal objects in the output. The SideIndicator property shows whether elements are equal (==) or if they differ (<= or =>). If all elements from $Array2 are in $Array1, it means $Array2 is contained within $Array1.

You can see the output in the screenshot below after I executed the PowerShell script using Visual Studio Code.

Check if an Array Contains Another Array in PowerShell

Method 2: Using -contains Operator

The -contains operator is another way to check if an array contains a specific value in PowerShell. However, checking for an entire array’s containment requires a loop or another form of iteration.

Here’s an example using -contains within a loop:

$Array1 = 'Red', 'Green', 'Blue'
$Array2 = 'Green', 'Blue'
$isContained = $true

foreach ($item in $Array2) {
    if ($Array1 -notcontains $item) {
        $isContained = $false
        break
    }
}

if ($isContained) {
    Write-Host "Array2 is contained in Array1"
} else {
    Write-Host "Array2 is not fully contained in Array1"
}

In this example, we iterate over each item in $Array2 and use the -contains operator to check if $Array1 contains that item in PowerShell. If any item is not found, $isContained is set to $false.

Check out the output in the screenshot below:

How to Check if an Array Contains Another Array in PowerShell

Method 3: Using .Contains() Method

PowerShell arrays have a .Contains() method that can be used to check if an array contains a specific element. Similar to the -contains operator, to check for another array’s containment, you would need to iterate over the elements.

Here’s an example:

$Array1 = 'Red', 'Green', 'Blue'
$Array2 = 'Green', 'Blue'
$isContained = $true

foreach ($item in $Array2) {
    if (-not $Array1.Contains($item)) {
        $isContained = $false
        break
    }
}

if ($isContained) {
    Write-Host "Array2 is contained in Array1"
} else {
    Write-Host "Array2 is not fully contained in Array1"
}

This code snippet works similarly to the -contains operator example, but it uses the .Contains() method to perform the check.

Method 4: Using Custom Functions

You can also create a custom function to encapsulate the logic for checking array containment. This can make your code cleaner and more reusable.

Here’s a custom function example:

function Test-ArrayContains {
    param(
        [object[]]$MainArray,
        [object[]]$SubArray
    )

    foreach ($item in $SubArray) {
        if ($MainArray -notcontains $item) {
            return $false
        }
    }

    return $true
}

$Array1 = 'Red', 'Green', 'Blue'
$Array2 = 'Green', 'Blue'

if (Test-ArrayContains -MainArray $Array1 -SubArray $Array2) {
    Write-Host "Array2 is contained in Array1"
} else {
    Write-Host "Array2 is not fully contained in Array1"
}

This function, Test-ArrayContains, takes two parameters: $MainArray and $SubArray. It returns $true if $SubArray is contained within $MainArray, otherwise $false.

Conclusion

Checking if an array contains another array in PowerShell can be accomplished using various methods, such as Compare-Object, the -contains operator, the .Contains() method, or custom functions.

In this PowerShell tutorial, I will explain how to check if an array contains another array in PowerShell using various methods with examples.

You may also like: