Search code examples
powershellcompareobject

How to compare two tables with one column that is slightly different in PowerShell?


I would like to compare the version info of files from two different directories.

I could do this:

$files1 = (Get-Item "$path1\*.dll").VersionInfo
$files2 = (Get-Item "$path2\*.dll").VersionInfo
compare-object $files1 $files2

But then I get something like:

InputObject
-----------
File:             path1\AxInterop.ShDocVw.dll...
File:             path1\dom.dll...
(etc...)
File:             path2\AxInterop.ShDocVw.dll...
File:             path2\dom.dll...
(etc...)

I guess I could do something like:

$files1 = (Get-Item "$path1\*.dll").VersionInfo.ProductVersion
$files2 = (Get-Item "$path2\*.dll").VersionInfo.ProductVersion
compare-object $files1 $files2
$files1 = (Get-Item "$path1\*.dll").VersionInfo.FileVersion
$files2 = (Get-Item "$path2\*.dll").VersionInfo.FileVersion
compare-object $files1 $files2

but then if there is a difference, I'd have to go looking for what that difference is. I can't compare the files directly because one set is signed and the other isn't.

What would the best way to do this?

To clarify, the current compare-object cmdlet doesn't meet my needs because it shows the filename as different because it shows that they have different paths. This is irrelevant to me.

I would like to compare rows with the same filename but different version numbers. If a difference in version number is observed for the same filename or a filename doesn't exist in one of the tables, then show the difference.


Solution

  • Use the Compare-Object cmdlet's -Property parameter to compare and output the properties of interest.

    Group-Object allows grouping the resulting objects, and Select-Object can be used to produce a single output object per file name from the group objects:

    $files1 = (Get-Item $path1\*.dll).VersionInfo
    $files2 = (Get-Item $path2\*.dll).VersionInfo
    
    Compare-Object $files1 $files2 -Property { Split-Path -Leaf $_.FileName }, 
                                             ProductVersion, 
                                             FileVersion |
      Group-Object -Property ' Split-Path -Leaf $_.FileName ' | 
        Select-Object Name, 
               @{ n = 'SideIndicator'; e = { $_.Group.SideIndicator } },
               @{ n = 'ProductVersion'; e = { $_.Group.ProductVersion -join ' <-> ' } }, 
               @{ n = 'FileVersion'; e = { $_.Group.FileVersion -join ' <-> ' } }           
    

    Note the use of calculated properties to compare the input objects by file name only, and to later extract information from Group-Object's output via Select-Object.

    Unfortunately, Compare-Object, as of PowerShell [Core] 7.0, doesn't allow you to name calculated properties[1], and the implied property name is the literal contents of the script block ({ ... }),
     Split-Path -Leaf $_.FileName , which is what must be passed to Group-Object -Property.

    The above yields something like the following:

    Name         SideIndicator ProductVersion              FileVersion
    ----         ------------- --------------              -----------
    file1234.exe {=>, <=}      7.0.18362.1 <-> 7.0.18365.0 7.0.18362.1 <-> 7.0.18365.0
    file1235.exe <=            10.0.18362.1                10.0.18362.1
    

    That is, for files present in both locations but with differing version numbers, SideIndicator shows
    {=>, <=}, with the potentially differing version numbers in the *Version properties separated by <->


    [1] Adding the ability to name calculated properties in the context of Compare-Object is the subject of this GitHub feature request.