Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compare-object left or right side only

Quick Question

Is there a better (i.e. more efficient / more concise) way to do this?

compare-object $a $b | ?{$_.SideIndicator -eq '<='}

Detail

Compare-Object gives paramenters -excludeDifferent and -includeEqual to allow you to amend which results you get.

  • using both gives you an inner join
  • using just -includeEqual gives you a full outer join
  • using just -excludeDifferent is pointless; as by default equal items are excluded, so it will now exclude everything.

There are no options for -includeLeft, -excludeLeft or similar.

Currently to do a left outer join where the right side is null (i.e. items in the reference object which are not in the difference object) I need to filter the results manually, as per the code above.

Have I missed something / is there a better way?

http://ss64.com/ps/compare-object.html

like image 685
JohnLBevan Avatar asked Feb 05 '15 10:02

JohnLBevan


People also ask

How does compare-object work in PowerShell?

The Compare-Object cmdlet compares two sets of objects. One set of objects is the reference, and the other set of objects is the difference. Compare-Object checks for available methods of comparing a whole object.

How do I compare two variables in PowerShell?

To check to see if one object is equal to another object in PowerShell is done using the eq operator. The eq operator compares simple objects of many types such as strings, boolean values, integers and so on. When used, the eq operator will either return a boolean True or False value depending on the result.

How do I compare numbers in PowerShell?

PowerShell has two operators to compare two values to determine whether they are greater than ( –gt ) or less than ( -lt ) each other.


2 Answers

there is no option like that for that cmdlet, however you could create a filter (in your profile for example) and then use it to filter the result : something like

filter leftside{
param(
        [Parameter(Position=0, Mandatory=$true,ValueFromPipeline = $true)]
        [ValidateNotNullOrEmpty()]
        [PSCustomObject]
        $obj
    )

    $obj|?{$_.sideindicator -eq '<='}

}

usage

compare-object $a $b | leftside
like image 130
Loïc MICHEL Avatar answered Oct 14 '22 04:10

Loïc MICHEL


You can also add -property SideIndicator and use an if statement for it.

$Missing = compare-object $Old $new -Property Name,SideIndicator
     ForEach($Grp in $Missing) {
          if($grp.sideindicator -eq "<=") {     
          # Do Something here
          }
     }
like image 23
AngryBlossom Avatar answered Oct 14 '22 05:10

AngryBlossom