Table of Contents
In this post, we will see how to compare Arrays in PowerShell.
PowerShell supports arrays with one or more dimensions and zero to many elements in each dimension. Elements inside a dimension are numbered from 0
in ascending integer order. In addition, the array subscript operator []
allows access to any particular element. PowerShell may be used in a variety of ways to compare arrays. Following are a few of them.
Compare Arrays of Strings
If you have two arrays containing strings, we may compare them with PowerShell in one of the simplest ways possible. We can compare the strings in the arrays in a few different ways when we find ourselves in this situation; below are a few methods to compare arrays of strings.
- Using
Where-Object
Cmdlet with in operator - Using the
Compare-Object
Cmdlet - Using the
-contains
Operator - Using the
-in
Operator
Use Where-Object
Cmdlet with in operator
Use the Where-Object
cmdlet to compare arrays in PowerShell.
1 2 3 4 5 |
$array1 = "Monday", "Tuesday", "Wednesday", "Thursday" $array2 = "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" $array1 | Where-Object -FilterScript { $_ -in $array2 } |
1 2 3 4 |
Wednesday Thursday |
The sole purpose of the PowerShell Where-Object
cmdlet was to filter a command’s output so that only the data you desire is returned. A script block is an anonymous function. Therefore, you would use the -FilterScript
parameter to employ a script block as a filter. This parameter enables you to write a script block, which can then be passed to the FilterScript
and performed in the where- object
cmdlet.
The-in
operator means the value is in a collection and returns property value if a match is found. The pipeline variable $_
is a particular variable in PowerShell used to represent the current object. So we got the output Wednesday
and Thursday
from both arrays where the match was found.
Use the Compare-Object
Cmdlet
Use the Compare-Object
cmdlet to compare arrays in PowerShell. The Compare-Object cmdlet compares two sets of objects. One set of objects is the reference, and the other set of objects is the difference.**
1 2 3 4 5 |
$array1 = "Monday", "Tuesday", "Wednesday", "Thursday" $array2 = "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" Compare-Object -ReferenceObject $array1 -DifferenceObject $array2 |
1 2 3 4 5 6 7 8 9 |
InputObject SideIndicator ----------- ------------- Friday => Saturday => Sunday => Monday <= Tuesday <= |
We used the Compare-Object
cmdlet in PowerShell to compare the specified arrays. Using a -ReferenceObject
and a DifferenceObject
, this cmdlet can determine which elements are present in each array and which are not. The =>
is the SideIndicator
property indicates that the returned InputObject
property is in the DifferenceObject
value rather than the ReferenceObject
value, while the <=
means the opposite.
Use the -Contains
Operator
Use the -Contains
operator to check if the specified string is part of the other array in PowerShell.
1 2 3 4 |
$array = @('potato', 'onion', 'peas', 'carrot') $array -contains 'onion' |
1 2 3 |
True |
PowerShell’s -Contains
operator enables you to determine whether an item is a part of a collection. Although the -Contains
operator doesn’t, by default, understand collections, you can write code to make it perform what you want. PowerShell can use this operator to check a single item in an array. It returns True
if it is present in the array; else, it returns False
.
Let’s create two arrays to check the command strings of both arrays.
1 2 3 4 5 6 7 8 9 |
$array1 = @('potato', 'onion', 'carrot') $array2 = @('peas', 'cauliflower', 'potato') $array1 | ForEach-Object { if ($array2 -contains $_) { Write-Host "<code>$array2 has the </code>$array1 string [$_]" } } |
1 2 3 |
$array2 has the $array1 string [potato] |
The ForEach-Object
cmdlet operates on every item in a collection of input objects. The input objects can also be piped to the cmdlet or specified using the InputObject
parameter. We used it to iterate over the array1
. We used the if
statement with the -contains
operator for each iteration to check if array2
also has that element. If so, we used the Write-Host
cmdlet to print the customized output, as shown above.
Use the -in
Operator with ForEach-Object
Use the -in
operator to check if the specified string is part of the other array in PowerShell.
1 2 3 4 5 6 7 8 9 |
$array1 = @('potato', 'onion', 'carrot') $array2 = @('peas', 'cauliflower', 'potato') $array1 | ForEach-Object { if ($_ -in $array2) { Write-Host "<code>$array2 has the </code>$array1 string [$_]" } } |
1 2 3 |
$array2 has the $array1 string [potato] |
This code is similar to the previous example but uses the -in
operator for this code, which is equivalent to the -contains
operator but has the opposite syntax. The array was defined on the left side when the -contains
operator was used. This time, the array is specified on the right side using the -in
operator, as seen above.
Further reading:
Compare Arrays of custom object
Use Compare-Object
to compare an array of custom objects. The Compare-Object cmdlet compares two sets of objects. One set of objects is the reference, and the other set of objects is the difference.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$ArrayObject1 = @( [PSCustomObject]@{Name = "John"; Age = 30}, [PSCustomObject]@{Name = "Martin"; Age = 25} ) $ArrayObject2 = @( [PSCustomObject]@{Name = "John"; Age = 30}, [PSCustomObject]@{Name = "Sam"; Age = 40} ) Compare-Object -ReferenceObject $ArrayObject1 -DifferenceObject $ArrayObject2 -Property Name, Age |
1 2 3 4 5 6 7 8 |
Name Age SideIndicator ---- --- ------------- Name Age SideIndicator ---- --- ------------- Sam 40 => Martin 25 <= |
Above code will provide you difference between two arrays.
<=
denotes if element is only present in ReferenceObject
=>
denotes if element is only present in DifferenceObject
That’s all about how to compare Arrays in PowerShell.