Compare-Object: PassThru parameter modifies the input variable
The easiest way to explain this problem is 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 (seems to be the same in PS versions 3 and 5, haven't tested others) ...
I'm trying to figure out why turning on the -PassThru parameter changes one of the input variables by adding an additional parameter.
In addition to trying to understand why this is happening, I would like to avoid it. Is there a way to specify an output variable for the Compare-Object cmdlet without changing the input variables?
Thanks in advance.
EDIT: It is now clear to me that this question is not clear enough. To make it easier ....
$y = Compare-Object -ReferenceObject $refObj -DifferenceObject $diffObj -PassThru -IncludeEqual
With the above method, how do I stop the SideIndicator being added to $ refObj and instead add it instead of $ y? There seems to be no -OutVariable option for Compare-Object, is there any other way to do this?
EDIT 2: It turns out there is a -OutVariable option for Compare-Object, but it doesn't work ...
Compare-Object -ReferenceObject $refObj -DifferenceObject $diffObj -IncludeEqual -PassThru -OutVariable $objComparisonResults
I would expect data in $ objComparisonResults, but no data is stored in this variable.
source to share
Compare-Object
always adds SideIndicator
NoteProperty. Look at the objects produced. PowerShell creates an object, not flat text for analysis. Usage -PassThru
just 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
source to share
Found a solution. Instead of trying to store the result of the Compare-Object in a variable this way ...
$y = Compare-Object -ReferenceObject $refObj -DifferenceObject $diffObj -PassThru -IncludeEqual
... can store the results this way ...
$y = (Compare-Object -ReferenceObject $file1 -DifferenceObject $file2 -Property $properties1 -IncludeEqual)
This second way, the output is stored in $ y, and since -PassThru was skipped, we should avoid changes that are saved back to the input variable.
As an additional refinement, Tee-Object can be used to store the output in a variable, which also allows us to control whether the Compare-Object output is automatically displayed on the screen (Out-Null in the command below means that this output is suppressed):
(Compare-Object -ReferenceObject $refObj -DifferenceObject $diffObj -IncludeEqual) | Tee-Object -Variable y | Out-Null
source to share