I would use Compare-Object for this.
$x = [PSCustomObject]@{
foo = 111
bar = 222
}
$y = [PSCustomObject]@{
foo = 111
bar = 222
}
$collection = [System.Collections.Arraylist]@()
[void]$collection.Add($x)
if (Compare-Object -Ref $collection -Dif $y -Property foo,bar | Where SideIndicator -eq '=>') {
[void]$collection.Add($y)
}
Explanation:
Comparing a custom object to another using comparison operators is not trivial. This solution compares the particular properties you care about (foo
and bar
in this case). This can be done simply with Compare-Object
, which will output differences in either object by default. The SideIndicator
value of =>
indicates the difference lies in the object passed into the -Difference
parameter.
The [System.Collections.Arraylist]
type is used over an array to avoid the inefficient +=
typically seen when growing an array. Since the .Add()
method produces an output of the index being modified, the [void]
cast is used to suppress that output.
You could be dynamic with the solution regarding the properties. You may not want to hard code the property names into the Compare-Object
command. You can do something like the following instead.
$x = [PSCustomObject]@{
foo = 111
bar = 222
}
$y = [PSCustomObject]@{
foo = 111
bar = 222
}
$collection = [System.Collections.Arraylist]@()
[void]$collection.Add($x)
$properties = $collection[0] | Get-Member -MemberType NoteProperty |
Select-Object -Expand Name
if (Compare-Object -Ref $collection -Dif $y -Property $properties | Where SideIndicator -eq '=>') {
[void]$collection.Add($y)
}