question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggestion: Add parameters to Compare-Object that directly support retrieval of left-side-only or right-side-only objects

See original GitHub issue

When 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:closed
  • Created 6 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mklement0commented, Jul 20, 2017

I see it as parameter sets.

Yes, absolutely - they would complement the old parameter sets (which could then be considered deprecated).

Maybe -AsHashTable?

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.

0reactions
mklement0commented, Jul 21, 2017

Thanks for tagging, @SteveL-MSFT, but I’m closing this in favor of the rewritten, more focused #4316

Read more comments on GitHub >

github_iconTop Results From Across the Web

Compare-Object (Microsoft.PowerShell.Utility)
The Compare-Object cmdlet compares two sets of objects. One set of objects is the reference, and the other set of objects is the...
Read more >
Powershell Compare-Object Doesn't work properly
I thought that i can create index of mails from both sides. each item in the index includes 4 attributes - senton, subject,...
Read more >
Comparing two objects
Hi all, I need to compare two objects in a microflow. There are two productparts associated with a producthead.
Read more >
Comparing Java objects with equals() and hashcode()
In the first comparison, equals() compares the current object instance with the object that has been passed. If the two objects have the...
Read more >
How to Compare Objects in JavaScript | by Simon Ugorji
Today, I am going to show you a basic way to write a function that compares two objects, and also retrieves their matching...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found