Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12141

Comparing content of hashes and pscustomobject objects

$
0
0

Results for integer and array comparison seem understandable. It does not appear to be comparing the reference (address) of the object. Is it actually comparing the content?

PS C:\> $Int1 = 1PS C:\> $Int2 = 1PS C:\> $Int3 = 99PS C:\> $null -eq (Compare-Object -ReferenceObject $Int1 -DifferenceObject $Int2)TruePS C:\> $null -eq (Compare-Object -ReferenceObject $Int1 -DifferenceObject $Int3)FalsePS C:\> $Array1 = @(1, 2, 3)PS C:\> $Array2 = @(1, 2, 3)PS C:\> $Array3 = @(1, 2, 99)PS C:\> $null -eq (Compare-Object -ReferenceObject $Array1 -DifferenceObject $Array2)TruePS C:\> $null -eq (Compare-Object -ReferenceObject $Array1 -DifferenceObject $Array3)False

However, comparison of hashes and pscustomobjects does not appear to work the same way. Why are these all comparing as identical?

PS C:\> #How can I compare the contents of the objects?PS C:\> $Hash1 = @{a=1;b=2}PS C:\> $Hash2 = @{a=1;b=2}PS C:\> $Hash3 = @{a=1;b=99}PS C:\> $null -eq (Compare-Object -ReferenceObject $Hash1 -DifferenceObject $Hash2)TruePS C:\> $null -eq (Compare-Object -ReferenceObject $Hash1 -DifferenceObject $Hash3)TruePS C:\>PS C:\> $Obj1 = [PSCustomObject]@{Name = "Sam"; Weight = 145}PS C:\> $Obj2 = [PSCustomObject]@{Name = "Sam"; Weight = 145}PS C:\> $Obj3 = [PSCustomObject]@{Name = "Sam"; Weight = 199}PS C:\> $null -eq (Compare-Object -ReferenceObject $Obj1 -DifferenceObject $Obj2)TruePS C:\> $null -eq (Compare-Object -ReferenceObject $Obj1 -DifferenceObject $Obj3)True

Viewing all articles
Browse latest Browse all 12141

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>