Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare-Object: PassThru parameter alters input variable

Tags:

powershell

Easiest to explain this issue with a code snippet...

Clear-Host

$refObj = New-Object System.Object
$refObj | Add-Member -MemberType NoteProperty -Name ObjectMember1 -Value 123

$diffObj = New-Object System.Object
$diffObj | Add-Member -MemberType NoteProperty -Name ObjectMember1 -Value 456


Write-Output 'Before 1st comparison...'
$refObj | Get-Member | ? { $_.MemberType  -eq 'NoteProperty' } | Select *


$x = Compare-Object -ReferenceObject $refObj -DifferenceObject $diffObj -IncludeEqual

Write-Output 'After 1st comparison...'
$refObj | Get-Member | ? { $_.MemberType  -eq 'NoteProperty' } | Select *


$y = Compare-Object -ReferenceObject $refObj -DifferenceObject $diffObj -PassThru -IncludeEqual

Write-Output 'After 2nd comparison...'
$refObj | Get-Member | ? { $_.MemberType  -eq 'NoteProperty' } | Select *

... and the results (appears to be the same in PS versions 3 and 5, haven't tested any others) ...

Query results

What I'm struggling to understand is why the inclusion of the -PassThru parameter is altering one of the input variables, by adding on an additional parameter.

In addition to struggling to understand why this is happening, I'd like to avoid it. Is there a way of specifying the output variable for the Compare-Object cmdlet without altering the input variables?

Thanks in advance.

EDIT: It's clear to me now that the question wasn't clear enough. To make this more straightforward....

$y = Compare-Object -ReferenceObject $refObj -DifferenceObject $diffObj -PassThru -IncludeEqual

With the above, how do I stop SideIndicator being added to $refObj, and add it to $y instead? There doesn't seem to be an -OutVariable option for Compare-Object, is there another way I can do this?

EDIT 2: Turns out there is an -OutVariable option for Compare-Object, but it doesn't seem to work...

Compare-Object -ReferenceObject $refObj -DifferenceObject $diffObj -IncludeEqual -PassThru -OutVariable $objComparisonResults

I would expect data in $objComparisonResults, but no data is saved in this variable.

like image 961
ZenoArrow Avatar asked May 26 '26 13:05

ZenoArrow


1 Answers

Compare-Object always adds the SideIndicator NoteProperty. Have a look at the objects produced. PowerShell produces object, not flat text to be parsed. Using -PassThru simply caused it to appear in the console output.

Clear-Host
$logfile = 'C:\src\t\pt.txt'
if (Test-Path -Path $logfile) { Remove-Item -Path $logfile }

$refObj = New-Object System.Object
$refObj | Add-Member -MemberType NoteProperty -Name ObjectMember1 -Value 123

$diffObj = New-Object System.Object
$diffObj | Add-Member -MemberType NoteProperty -Name ObjectMember1 -Value 456


'Before 1st comparison...' | Out-File -FilePath $logfile -Append -Encoding Ascii

$refObj | Get-Member | ? { $_.MemberType  -eq 'NoteProperty' } | Select * | gm |
    Out-File -FilePath $logfile -Append -Encoding Ascii


$x = Compare-Object -ReferenceObject $refObj -DifferenceObject $diffObj -IncludeEqual | gm |
    Out-File -FilePath $logfile -Append -Encoding Ascii

'After 1st comparison...' | Out-File -FilePath $logfile -Append -Encoding Ascii

$refObj | Get-Member | ? { $_.MemberType  -eq 'NoteProperty' } | Select * | gm |
    Out-File -FilePath $logfile -Append -Encoding Ascii


$y = Compare-Object -ReferenceObject $refObj -DifferenceObject $diffObj -PassThru -IncludeEqual | gm |
    Out-File -FilePath $logfile -Append -Encoding Ascii

'After 2nd comparison...' | Out-File -FilePath $logfile -Append -Encoding Ascii

$refObj | Get-Member | ? { $_.MemberType  -eq 'NoteProperty' } | Select * | gm |
    Out-File -FilePath $logfile -Append -Encoding Ascii
like image 137
lit Avatar answered May 28 '26 09:05

lit