Compare Arrays in PowerShell

Compare Arrays in PowerShell

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.

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.**

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.

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.

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.

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.

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.

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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *