Suggestion: Add parameters to Compare-Object that directly support retrieval of left-side-only or right-side-only objects
See original GitHub issueWhen comparing two sets of objects, it is currently somewhat cumbersome to retrieve only those objects exclusive to one side of the comparison:
$left = 1, 2, 3, 4
$right = 1, 3, 4, 5
# Left-side only: -> 2
(Compare-Object $left $right | Where-Object SideIndicator -eq '<=').InputObject
# Right-side only: -> 5
(Compare-Object $left $right | Where-Object SideIndicator -eq '=>').InputObject
Wishful thinking:
$left = 1, 2, 3, 4
$right = 1, 3, 4, 5
# Left-side only: -> 2
Compare-Object $left $right -LeftOnly -PassThru
# Right-side only: -> 5
Compare-Object $left $right -RightOnly -PassThru
-
-LeftOnly
and-RightOnly
would be mutually exclusive and incompatible with-ExcludeDifferent
-
The existing
-PassThru
switch omits the custom-object wrapper with the side indicator that is created by default (despite the documentation claiming “Returns an object representing the item with which you are working. By default, this cmdlet does not generate any output.” - see https://github.com/PowerShell/PowerShell-Docs/issues/1461). -
To achieve left-“join” and right-“join” logic, simply add the existing
-IncludeEqual
switch.
Note: I’m using “join” in double quotes, because no joining (merging of data) in the usual sense happens - only different or identical objects are returned.
Note that the left / right terminology is not currently part of Compare-Object
, but I feel it is descriptive and intuitive (cf. SQL join terminology).
Thus, additionally, the following parameter aliases could be introduced:
-LeftObject
as an alias for-ReferenceObject
-RightObject
as an alias for-DifferenceObject
Environment data
PowerShell Core v6.0.0-beta.4
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (5 by maintainers)
Yes, absolutely - they would complement the old parameter sets (which could then be considered deprecated).
While that nicely parallels
Group-Object
, the difference is that there you always get a group, and-AsHashTable
simply changes the format used to represent that group.Here, we’re actually changing the structure of the output fundamentally.
That said, perhaps the
Group-Object
analogy is more helpful, so I’m open to the idea, though it’s certainly something that can be decided later.I’ll write up a summary of our conversation, presumably as a new issue (I’ll close this one). Thanks for all your input.
Thanks for tagging, @SteveL-MSFT, but I’m closing this in favor of the rewritten, more focused #4316