New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggestion: Improve Compare-Object by adding set operations (union, intersection, symmetric difference, relative complement) #4316
Comments
I like this quite a bit. Compare-Object has long been... lacklustre... in implementation. I would personally also prefer if rather than Currently the default display is actually surprisingly difficult to make sense of in my opinion with any appreciably large comparison sets. It may even make more sense for the default display to actually do a |
Good point re default output format, @vexx32. Can I suggest you create a new issue to propose the grouped output? As for the As an aside: It's unfortunate that the property wasn't defined as an Personally it took me a while to remember the logic of |
Oh, I get that, but it's not clear at a glance which side is actually which; you have to examine your objects' input data and see which is I'll type up an issue on it. Whether or not it's actually used (which, frankly, I very rarely see because of its obscure and unclear implementation) it needs to change. |
I don't think Compare-Object -Group -Union $A $B
vs
Compare-Object -Union $A $B | Group-Object Back to using Sets, it would be nicer to have one parameter, like |
Thanks, @dragonwolf83. Re Re |
How many set operations exist out of the propose so that we want |
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
1 similar comment
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes. |
This suggestion is the result of a conversation between @iSazonov and me - see #4293.
The idea is to introduce new parameter sets that:
.SideIndicator
propertyBelow are examples of each desired new parameter (parameter set) that would be mutually incompatible and also incompatible with the current parameter sets.
Aside from referring to the desired set operation, their desired behavior is expressed as s command using
Compare-Object
's current capabilities:Compare-Object -Union $A $B
: union (A ∪ B)Compare-Object $A $B -IncludeEqual -PassThru
Compare-Object -Intersection $A $B
: intersection (A ∩ B):Compare-Object $A $B -IncludeEqual -ExcludeDifferent -PassThru
Compare-Object -SymmetricDifference $A $B
: symmetric difference (A ∆ B) - the same as the current default behavior, but without the wrapper objectsCompare-Object $A $B -PassThru
Compare-Object -Complement $A $B
: relative complement (A ∖ B) - getting objects unique to$B
Compare-Object $A $B | ? SideIndicator -eq '=>' | % InputObject
Syntax note: @dragonwolf83 proposes using a single parameter such as
-SetOperation <operation>
(e.g.,-SetOperation Intersection
or-SetOperation Union
) instead of distinct switches (e.g.,-Intersection
or-Union
), which makes for easier implementation (no need for a distinct parameter set for each operation) and better discoverability, though is slightly more cumbersome to type for experienced users who already know what they want.Note that all commands above (effectively) suppress the custom-object wrapper with the
.SideIndicator
property and return the selected input objects directly (or, with-Property
specified, the resulting[pscustomobject]
instance would lack the.SideIndicator
property).One thing to note is the order in which objects are output - this is not currently documented (from a set perspective, order doesn't matter, but for subsequent processing it may), and I haven't dug into the source to verify, but from what I can tell, it is:
==
(identical) objects first>=
right-side-only objects next<=
left-side-only objects lastOn a related note, adding a new switch would make sense in order to return a hashtable of original objects grouped by what is currently the
.SideIndicator
value.Two names have been proposed for this switch:
-Group
-AsHashtable
-AsHashtable
has the advantage of being familiar fromGroup-Object
, although there it doesn't indicate a fundamental change in output structure.The following example uses
-Group
for now:The above would yield the equivalent of:
Environment data
The text was updated successfully, but these errors were encountered: