In this PowerShell tutorial, I will explain how to compare an array of objects in PowerShell. PowerShell provides a powerful cmdlet, Compare-Object
, that can be used to compare two arrays of objects.
To compare an array of objects in PowerShell, use the Compare-Object
cmdlet with the -ReferenceObject
and -DifferenceObject
parameters to specify the two arrays you want to compare. You can also use the -Property
parameter to compare specific properties of the objects. For example:
Compare-Object -ReferenceObject $array1 -DifferenceObject $array2 -Property Name, Age
This command will compare the Name
and Age
properties of the objects in $array1
and $array2
and output any differences.
Understanding Objects and Arrays in PowerShell
An object in PowerShell is an instance of a .NET class. It can contain multiple properties and methods. For example, when you get a list of files using Get-ChildItem
, each file is represented as an object with properties like Name
, Length
, and LastWriteTime
.
An array is a collection of items, which can be objects, strings, integers, or any other data type. In PowerShell, you can create an array by assigning multiple values to a variable, separated by commas.
Compare Array Of Objects In PowerShell Using the Compare-Object Cmdlet
The Compare-Object
cmdlet is the go-to command for comparing two sets of objects in PowerShell. You can compare arrays of simple types like integers and strings, as well as arrays of complex objects.
Syntax
Here’s the basic syntax for Compare-Object
:
Compare-Object -ReferenceObject <Object[]> -DifferenceObject <Object[]> [-Property <String[]>] [-PassThru] [-IncludeEqual]
-ReferenceObject
refers to the first set of objects you want to compare (often considered the “original” set).-DifferenceObject
refers to the second set of objects you want to compare (often considered the “changed” set).-Property
lets you specify which properties of the objects you want to compare.-PassThru
outputs the compared objects, not just the differences.-IncludeEqual
includes objects that are equal in the output.
Example: Comparing Custom Objects
Let’s create two arrays of custom objects and compare them using the above method in PowerShell.
# Create the first array of custom objects
$users1 = @(
[PSCustomObject]@{Name='John'; Role='Admin'},
[PSCustomObject]@{Name='Jane'; Role='User'},
[PSCustomObject]@{Name='Doe'; Role='Guest'}
)
# Create the second array of custom objects with some differences
$users2 = @(
[PSCustomObject]@{Name='John'; Role='User'},
[PSCustomObject]@{Name='Jane'; Role='Admin'},
[PSCustomObject]@{Name='Doe'; Role='Guest'}
)
# Compare the two arrays of objects
Compare-Object -ReferenceObject $users1 -DifferenceObject $users2 -Property Name, Role
In this example, Compare-Object
will output the differences in the Role
property for the objects with Name
‘John’ and ‘Jane’, because their roles are different in $users1
and $users2
.
Once you execute the code, you can see the output in the screenshot below.
Compare Array Of Objects in PowerShell with Custom Properties
Sometimes, you may need to compare objects with different property names. In such cases, you can use calculated properties to align the properties before comparison.
Example: Comparing Objects with Different Property Names
Imagine you have two PowerShell arrays of objects where the property names are different but represent the same data. You can create custom properties to make them comparable:
# Array of objects with property 'FirstName'
$users1 = @(
[PSCustomObject]@{FirstName='John'; Occupation='Admin'},
[PSCustomObject]@{FirstName='Jane'; Occupation='User'}
)
# Array of objects with property 'Name'
$users2 = @(
[PSCustomObject]@{Name='John'; Job='User'},
[PSCustomObject]@{Name='Jane'; Job='Admin'}
)
# Compare using custom properties
Compare-Object -ReferenceObject $users1 -DifferenceObject $users2 `
-Property @{Expression={$_.FirstName}; Label='Name'}, `
@{Expression={$_.Occupation}; Label='Job'}
In this example, we use a script block to define an expression for the FirstName
and Occupation
properties and label them as Name
and Job
respectively, to align with the properties in the second array.
Compare Array Of Objects in PowerShell [Custom Comparison]
You might need to write custom code to iterate through the arrays and compare each object individually for more complex comparisons. This approach gives you more control over the comparison logic.
Example: Custom Comparison Logic
Here’s an example of how you might implement a custom comparison:
# Define two arrays of objects
$users1 = # ... (as before)
$users2 = # ... (as before)
# Custom comparison logic
foreach ($user1 in $users1) {
$matchedUser = $users2 | Where-Object { $_.Name -eq $user1.Name }
if ($matchedUser -ne $null) {
# Compare properties
foreach ($property in $user1.PSObject.Properties.Name) {
if ($user1.$property -ne $matchedUser.$property) {
Write-Host "Difference found in user $($user1.Name) for property $property"
}
}
}
else {
Write-Host "User $($user1.Name) not found in second array"
}
}
In this script, we iterate through each object in $users1
, find the matching object in $users2
by name, and then compare each property individually.
Conclusion
Comparing arrays of objects in PowerShell can be as simple as using the Compare-Object
cmdlet for straightforward comparisons.
In this PowerShell tutorial, I have explained how to compare an array of objects in PowerShell using different methods.
You may also like:
- How to Get the Highest Number in an Array in PowerShell?
- How to Pass Objects to Functions in PowerShell?
- How to Get Unique Values from an Array in PowerShell?
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com