powershellcompareobject

How to get compare-object output in file along with record count


I am trying to append the output of compare-object of 2 rhash files to a file as well as count the records. The out-file was previously created in the script to capture a start time and eventually an end and elapsed time. This is my code for the compare and counter:

Compare-Object @objects | 
    % {$differences+=1} | 
        out-file -FilePath $resultsDir\CFRunResults-$runTimeStamp.txt -append

This produces the file but it is empty except for the start time written to it previously, and the $differences variable updates correctly but it doesn't appear in the file either. How would I go about getting the default compare-object output in a file as well as append to the output file the count of number of differences? Actually to be precise, in my application there will always be 2 differences I don't care about and since the remaining differences would be hash values for the same file list, differences on one side of the compare will have an equal number on the other. I plan to compute the value written to output file as $differences = ($differences - 2)/2.


Solution

  • Building on the helpful comments: