0

I have generated the list of MD5 checksum values from a directory within my project using Powershell's Get-FileHash function and then I exported the values to a .csv file.

$path = "C:\Users\Krishnaa\Documents\Visual Studio 2012\Projects\NamePrint\NamePrint\obj\Debug"

$hash = Get-FileHash -Path $path\* -Algorithm MD5

$export = $hash | Export-csv $path\hashfile.csv

This is how the output looks like if I call on $hash: https://i.stack.imgur.com/Owi0Q.png

Then I imported the .csv file back to the Powershell console.

$import = Import-csv $path\hashfile.csv | Format-Table

And when I call on $import, it outputs this : https://i.stack.imgur.com/cqvsO.png

When I created a simple function of my own to compare both the contents, I encounter problem whereby it says the the contents do not match. I do understand that each line in a .csv is treated as an object by Powershell. How to compare object-to-object in Powershell?

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
user3056928
  • 55
  • 3
  • 6
  • What's the question here? If you have code that isn't working the way you want you should show that code. – Etan Reisner Jan 26 '15 at 02:46
  • @EtanReisner I used the Get-FileHash to generate a list of MD5 values from a list of files within a directory. It displays them in a table format in Powershell. I then export these values to a .csv file to the same path. When I use the Import-CSV cmdlet to import the .csv file back to the Powershell console, it outputs the MD5 table back albeit in a different width. When I create a simple compareHash function, it is says the values do not match. – user3056928 Jan 26 '15 at 02:53
  • Is the question why your function says they don't match or why the columns have different widths? If the question is about your function you need to show the function. If the question is about the displayed widths then I don't know if that's a real issue it might just be the objects configuration differing between what Get-FileHash objects indicate and what the returned rows objects indicate (you'd have to inspect the objects to see). – Etan Reisner Jan 26 '15 at 02:56

1 Answers1

1

One problem with you above code is your use of Import-CSV. You aren't assigning the objects returned by Import-CSV to $import, you're assigning the array of formatting objects returned by Format-Table. If you drop the Format-Table, you should be able to compare $import.hash with $hash.hash (although you may need to loop through and compare row by row).

Hunter Eidson
  • 1,896
  • 14
  • 23
  • Well I initially used it without Format-Table cmdlet and when I output $import, it used to come out as a list. This happens from time to time. I have to exit and restart Powershell a few times for that problem to go away. So this is what I did now, function compareHash { foreach ($object in $import.Hash) { foreach($object2 in $hash.Hash) { if($object -eq $object2) { echo success } else { echo nope } } } } compareHash And this is my output : http://imgur.com/4JizXZ8 – user3056928 Jan 26 '15 at 03:26
  • To view it, you can definitely pipe it through `Format-Table`, just don't use it during assignments. From your output, I'm having a hard time telling if it matched as often as it should have. I tried it using files on my PC (and adding a "---" before the 2nd to last } in your function to break things up) and it looks like it behaves for me, at least. – Hunter Eidson Jan 26 '15 at 03:46