Array Comparisons in PowerShell

Do you need to compare two arrays in PowerShell? In this PowerShell tutorial, I will explain everything about Array comparisons in PowerShell using various methods with examples. We will explore how to compare arrays in PowerShell.

To compare arrays in PowerShell, the Compare-Object cmdlet is commonly used, which compares two sets of objects by serving one as the reference set and the other as the difference set to identify items that are equal or unique to each array. For a more manual approach, the -contains operator can be used to check if an array contains a specific element by iterating through each item.

Compare Arrays in PowerShell

An array is a data structure that holds a collection of items, which can be of any data type. You can create an array in PowerShell by assigning multiple values to a variable, separated by commas:

$array1 = 1, 2, 3, 4, 5
$array2 = 3, 4, 5, 6, 7

Now, let’s look at different methods to compare these arrays in PowerShell.

Method 1: Compare-Object Cmdlet

The most straightforward way to compare arrays in PowerShell is by using the Compare-Object cmdlet. This cmdlet takes two arrays as input and returns the differences between them.

Compare-Object -ReferenceObject $array1 -DifferenceObject $array2

When you run this command, PowerShell will display items that are unique to each array. By default, the output will include two symbols:

  • <= indicates the item is unique to the reference object ($array1 in this case).
  • => indicates the item is unique to the difference object ($array2 in this case).

Example:

$array1 = 1, 2, 3, 4, 5
$array2 = 3, 4, 5, 6, 7

Compare-Object -ReferenceObject $array1 -DifferenceObject $array2

This will output, you can see, after I executed the PowerShell script using VS code. Check the screenshot below:

Array Comparisons in PowerShell
InputObject SideIndicator
----------- -------------
          6 =>
          7 =>
          1 <=
          2 <=

Method 2: Using Loops for Comparison

Another way to compare arrays in PowerShell is by using loops. You can iterate through each item in one array and check if it exists in the other array.

Example:

$array1 = 1, 2, 3, 4, 5
$array2 = 3, 4, 5, 6, 7

foreach ($item in $array1) {
    if ($array2 -notcontains $item) {
        Write-Host "$item is not in array2"
    }
}

foreach ($item in $array2) {
    if ($array1 -notcontains $item) {
        Write-Host "$item is not in array1"
    }
}

This script will output:

1 is not in array2
2 is not in array2
6 is not in array1
7 is not in array1

Check out the screenshot below for your reference, I have expected the complete script using VS code.

Compare Arrays in PowerShell

Method 3: Using Custom Functions

You might want to create a custom function for more complex comparisons or repetitive tasks. This function can encapsulate the logic you need for array comparison and can be reused throughout your scripts. Here is how to create custom functions for array comparisons in PowerShell.

Example:

function Compare-Array {
    param(
        [array]$array1,
        [array]$array2
    )

    $result = @()

    foreach ($item in $array1) {
        if ($array2 -notcontains $item) {
            $result += "$item is unique to array1"
        }
    }

    foreach ($item in $array2) {
        if ($array1 -notcontains $item) {
            $result += "$item is unique to array2"
        }
    }

    return $result
}

$array1 = 1, 2, 3, 4, 5
$array2 = 3, 4, 5, 6, 7

Compare-Array -array1 $array1 -array2 $array2

Running this function will give you a similar output to the loop example but in a cleaner and more reusable format.

Method 4: Using LINQ in PowerShell

For advanced users, PowerShell can access .NET’s Language-Integrated Query (LINQ) capabilities to perform complex array comparisons.

Example:

$array1 = 1, 2, 3, 4, 5
$array2 = 3, 4, 5, 6, 7

$onlyInArray1 = [System.Linq.Enumerable]::Except([int[]]$array1, [int[]]$array2)
$onlyInArray2 = [System.Linq.Enumerable]::Except([int[]]$array2, [int[]]$array1)

$onlyInArray1
$onlyInArray2

This will output the items that are unique to each array using LINQ’s Except method.

Conclusion

Comparing arrays in PowerShell can be accomplished using various methods, from the simple Compare-Object cmdlet to more complex custom functions like loops or even LINQ.

In this PowerShell tutorial, I have explained how to compare arrays in PowerShell using different methods and examples.

You may also like: