Compare object with left or right 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 paramaxers -excludeDifferent and -includeEqual so you can change what results you get.

  • using both gives you an inner join
  • using only -includeEqual , you get a full outer join
  • using only -excludeDifferent is pointless; since identical elements are excluded by default, so now it will exclude everything.

There are no options for -includeLeft , -excludeLeft or the like.

Currently, to perform a left outer join, where the right side is null (i.e., elements in the reference object that are not in the difference object), I need to filter the results manually, according to the code above.

Am I missing something / is there a better way?

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

+6
source share
2 answers

there is no such option for this cmdlet, however you can create a filter (for example, in your profile), 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 '<='} } 

using

 compare-object $a $b | leftside 
+3
source

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

 $Missing = compare-object $Old $new -Property Name,SideIndicator ForEach($Grp in $Missing) { if($grp.sideindicator -eq "<=") { # Do Something here } } 
0
source

Source: https://habr.com/ru/post/982035/


All Articles