Powershell

What is Powerful PowerShell Compare Arrays

An array is the collection/group of similar/identical data items located in close proximity to each other in memory. Arrays can be compared in PowerShell using the “Compare-Object” cmdlet and the “-Contains” operator. The cmdlet “Compare-Object” in PowerShell is used to compare the two sets of objects. First set of objects is “reference” and the second set of objects is the “difference”. On the other hand, the “-Contains” operator checks whether an array contains a specific object or not.

The following post will cover the details about the comparison of PowerShell arrays.

What is Powerful PowerShell Compare Arrays?

These approaches will be considered while comparing arrays:

Method 1: Compare Arrays by Utilizing the Cmdlet “Compare-Object”

The cmdlet “Compare-Object” in PowerShell is used to compare the two sets of objects. One set of objects is “reference” and the second set is the “difference”.

Example 1: Use the “Compare-Object” Cmdlet to Compare Two Arrays

The below example will check if the array contains a specific object or not:

Compare-Object -ReferenceObject (Get-Content -Path C:\Doc\Ref_File.txt) -DifferenceObject (Get-Content -Path C:\Doc\Dif_File.txt)

 

According to the above code:

  • First, write the “Compare-Object” cmdlet.
  • Then, add the “-ReferenceObject” parameter and assign the referenced array.
  • After that, write the “-DifferenceObject” parameter and specify the difference array:

Example 2: Use the “-IncludeEqual” Parameter Along With the “Compare-Object” Cmdlet

The following example will display the array values which exist in both arrays by adding the “-IncludeEqual” parameter at the end:

Compare-Object -ReferenceObject (Get-Content -Path C:\Doc\Ref_File.txt) -DifferenceObject (Get-Content -Path C:\Doc\Dif_File.txt) -IncludeEqual

 

Method 2: Compare Arrays Using the “-Contains” Operator

The “-Contains” operator in PowerShell checks whether the specific item is available in the collection or not. The stated operator does not understand the collections by default. However, the code created by the user helps the “-Contains” operator to understand the collection of objects.

Example 1: Check Whether an Array Contains Specified Color or Not

The following demonstration will check whether an array contains a certain object or not:

$colors = @('blue','red','white','yellow')
$colors -contains 'white'

 

In the above code snippet:

  • First, initialize a variable and assign an array containing a list of colors.
  • After that, write a variable followed by the “-Contains” operator and assign the color object to be found:

Example 2: Use the “ForEach-Object” to Check if the First Array Exists in the Second Array or Not

This illustration will compare two arrays and then decide if the one array exists in the second or not:

$colors1 = @('yellow','violet','green','pink')
$colors2 = @('blue','violet','black','orange')
$colors1 | ForEach-Object {
    if ($colors2 -contains $_) {
        Write-Host "`$colors2 contains the `$colors1 string [$_]"
    }
}

 

According to the above-stated code:

  • First, initialize two variables and assign two arrays, respectively.
  • After that, write the first array assigned variable and then add the “|” pipeline to pass the output of the previous command to the next.
  • Then, add the “ForEach-Object” cmdlet, followed by the “If” condition containing.
  • The “If” condition will check whether the second array contains the first array or not.
  • Lastly, add the “Write-Host” cmdlet to display the string:

That was all about comparing arrays in PowerShell.

Conclusion

The PowerShell arrays can be compared using various methods or cmdlets. These methods include using the “-Contains” operator or the “Compare-Object” cmdlet. This post has elaborated on the various procedures to compare arrays in PowerShell.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.