Search code examples
powershellcompareobject

In Powershell, how do I use Compare-Object based on one property but output all properties?


Here is an example of log file inputs which contains MD5 hash and filename (with file size and date time stamp which is fine, what I want).

hashlog1.log:

2E331B30A705D462541398874451A955 \steamapps\appmanifest_1036890.acf 798 20230919_003055
D9A3D74B9F48CC599406F1D74D293261 \steamapps\appmanifest_1046030.acf 795 20231021_213325
2C364B22B4866903ED5E3BD9740B9DFA \steamapps\appmanifest_1086410.acf 762 20230413_034326
88D100F2A7FFC2C912B59B45AED9DC48 \steamapps\appmanifest_1091500.acf 925 20231026_112000
2A03A13E570CDA7424216DEF84C958EA \steamapps\appmanifest_1151640.acf 901 20230512_235832

hashlog2.log:

97CAF30C2E1F73ED121F75D947DB29C3 \steamapps\appmanifest_1036890.acf 798 20231122_123455
D9A3D74B9F48CC599406F1D74D293261 \steamapps\appmanifest_1046030.acf 795 20231021_213325
2C364B22B4866903ED5E3BD9740B9DFA \steamapps\appmanifest_1086410.acf 762 20230413_034326
78CF976F8BA64DAF19319E84B8133598 \steamapps\appmanifest_1091500.acf 687 20231122_125522
2A03A13E570CDA7424216DEF84C958EA \steamapps\appmanifest_1151640.acf 901 20230512_235832

I generate new tables with column headers:

$file1=(Get-Content -Path "hashlog1.log") | Select @{N='Hash'; E={($_ -split ' ',2)[0]}},@{N='File'; E={(($_ -split ' ',2)[1])}}
$file2=(Get-Content -Path "hashlog2.log") | Select @{N='Hash'; E={($_ -split ' ',2)[0]}},@{N='File'; E={(($_ -split ' ',2)[1])}}

I want to compare based on the filename only ("File"). But I would like to output both "Hash" and "File" values in the output.

If I use:

Compare-Object $file1 $file2 -Property File | Select Hash,File,SideIndicator

It seems I should be able to get the proper output but it's only showing the "File" and "SideIndicator" property values, not "Hash"

Hash File                                                   SideIndicator
---- ----                                                   -------------
     \steamapps\appmanifest_1036890.acf 798 20231122_123455 =>
     \steamapps\appmanifest_1091500.acf 687 20231122_125522 =>
     \steamapps\appmanifest_1036890.acf 798 20230919_003055 <=
     \steamapps\appmanifest_1091500.acf 925 20231026_112000 <=

How do I get it to also output the Hash value as well? Thank you in advance for any assistance.


Solution

  • Compare-Object -ReferenceObject $file1 -DifferenceObject $file2 -Property File -PassThru