Skip to content

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

@mklement0

Description

@mklement0

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 Compare-Object's -PassThru switch is documented incorrectly MicrosoftDocs/PowerShell-Docs#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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifIssue-Enhancementthe issue is more of a feature request than a bugWG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions