I am using Compare-Object to compare 2 bidimensional arrays issued by Get-ChildItem.
I am asking it to compare using the Name property using -Property 'Name'
This returns a table with the Name property and SideIndicator.
The problem is that I would like to know not only the name of the file, but also its complete path; if I ask Compare-Object ... -Property @{ Expression = {"$($_.Directory)\$($_.Name)"} }, it compares using the path, which is not what I want either (this tool I'm coding will be to sync up 2 folders, so obviously the paths are different, hence getting all the files scored as different).
Please let me know if you need more details !
If you use
Compare-Objectwith-Propertyin order to compare the two input collections by one or more specific property values only, by default the output objects contain those properties only, alongside the.SideIndicatorproperty that signals which of the two input collections a given difference came from:-Property- resulting in whole-object comparisons (which with complex objects are typically meaningless) - do you get the whole input objects by default, namely in the.InputObjectproperty of the[pscustomobject]instances that are emitted.To instead pass the whole original objects through, use the
-PassThruswitch.That is, instead of emitting
[pscustomobject]instances with a.SideIndicatorproperty,-PassThrucauses the input objects themselves to be emitted.However, so that you can still determine which side (input collection) a given object came from, the original objects are decorated with a
.SideIndicatorproperty, using PowerShell's ETS (Extended Type System).Caveats:
If you combine
-PassThruwith-IncludeEqual, it is invariably only the LHS (-ReferenceObject) object that is passed through for objects that compare as equal (i.e. whose.SideIndicatorproperty contains==); that is, if two objects compare equal with respect to the given properties but differ with respect to others, you'll only have access to those other properties from the LHS objects.The ETS
.SideIndicatorproperty is usually invisible, such as in the output from the example above, but may situationally unexpectedly surface, such as when you pass a decorated object toConvertTo-Json(pipe to
Select-Object -Property * -ExcludeProperty SideIndicatorto avoid this).[1][1] In PowerShell (Core) 7.2+,
ConvertTo-Jsonnow automatically and invariably ignores ETS properties of[string]and[datetime]instances, specifically. See GitHub issue #5797 for the original discussion that led to this change.