Search code examples
powershellcompareobject

Replace Arrows for SideIndicator in Compare-Object


Can I change the output for SideIndicators as a result of Compare-Object to something more user friendly for standard users?

I have a script that compares the folder of a 2017 version of our software and the current 2018 version.

$var1 = Get-ChildItem -Recurse -path C:\software18\bin
$var2 = Get-ChildItem -Recurse -path C:\software17\bin
Compare-Object -ReferenceObject $var1 -DifferenceObject $var2 > C:\diff.txt

The output looks like this:

InputObject           SideIndicator
-----------           -------------
thing.dll                 =>
stuff.dll                 <=
software.exe              <=

The report is given to testers and it would be cleaner for them if I could change the SideIndicators into text.

My desired output:

InputObject           SideIndicator
-----------           -------------
thing.dll                Not in 18
stuff.dll                Not in 17
software.exe             Not in 17

Or something similiar where they can get the gist of it without having to know which is the reference/difference object.

I have an initial idea but searching for something similar doesn't yield many results. Don't know if I have to do an if loop or I can replace the brackets in PS. Still new to PS so I greatly appreciate the help!


Solution

  • You could loop over each object make a conditional for each side indicator:

    $var1 = Get-ChildItem -Recurse -path C:\software18\bin
    $var2 = Get-ChildItem -Recurse -path C:\software17\bin
    (Compare-Object -ReferenceObject $var1 -DifferenceObject $var2 -PassThru |
        ForEach-Object {
            if ($_.SideIndicator -eq '=>') {
                $_.SideIndicator = 'Not in 18'
            } elseif ($_.SideIndicator -eq '<=')  {
                $_.SideIndicator = 'Not in 17'
            }
            $_
        }) > C:\diff.txt
    

    Or you could just regex replace the strings in one step:

    $var1 = Get-ChildItem -Recurse -path C:\software18\bin
    $var2 = Get-ChildItem -Recurse -path C:\software17\bin
    (Compare-Object -ReferenceObject $var1 -DifferenceObject $var2 -PassThru |
        ForEach-Object {
            $_.SideIndicator = $_.SideIndicator -replace '=>','Not in 18' -replace '<=','Not in 17'
            $_
        }) > C:\diff.txt