Changing the entries in a CSV file with MD5 hashes

Welcome Forums General PowerShell Q&A Changing the entries in a CSV file with MD5 hashes

Viewing 1 reply thread
  • Author
    Posts
    • #283543
      Participant
      Topics: 1
      Replies: 0
      Points: 12
      Rank: Member

      Hi everyone! I’m new here as well as new to Powershell but I could really use some help here.

      I need to export the MD5 hashes of two directories and match them. When exporting the MD5 hashes in a CSV file, it creates three columns: Algorithm, Hash and Path where Path displays the full path (C:\Users\Admin\Desktop\ExampleFiles\SampleFile.dll for example). I want to change that and make it so that only two columns are created: Hash and File Name. Where Path displays the full path (C:\Users\Admin\Desktop\ExampleFiles\SampleFile.txt for example). However, I want it to only display the name of the file (SampleFile.txt).
      If there are files inside more folders then I want it to look like this (TestFolder1\File1.txt) and not (C:\Users\Admin\Desktop\ExampleFiles\TestFolder1\File1.txt). Please note that in this case, the “ExampleFiles” folder is the folder that I want to deal with and I want to store all the MD5 hashes of all the files in it.
      For now, the code that I have is this:

      In the exported CSV file, I can see that three columns have been created: Algorithm, Hash and Path. I’ve tried using Select-Object -Property FullName, Hash and piping it into Export-Csv but that doesn’t work since the $DestinationHash and $SourceHash do not contain the FullName of the files, only the paths.

      I want the CSV to look like this:

      Hash FileName
      87654897XYZABCD SampleFile.txt
      XYZABCD87654897 TestFolder1\File1.txt

      As I mentioned above, the ExampleFiles folder contains these files. So, the full paths of each of these files would look something like the one that I have mentioned above.
      I would really appreciate help and feedback here. Thank you.

    • #283969
      Participant
      Topics: 5
      Replies: 252
      Points: 999
      Helping Hand
      Rank: Major Contributor

      The Get-FileHash cmdlet returns an object with 3 properties: Algorithm, Hash and Path.  For your case you only want the Hash property, so you have to call that by using the property dereference operator “.” i.e.

      or using the the select-object cmdlet and expanding the hash property i.e.

      The other issue is when you run Get-ChildItem against a directory it may return 2 types of objects, other directories or files.  In your case you just want files, so there is a -file switch you can use to do that (there are other ways, but if you have PS Ver 5.1 or later you should be fine with -file).

      Finally, the file objects returned from Get-ChildItem have numerous properties but none of them contain the relative path of the file which is what you want.  To accomplish this you can use the -replace operator or the replace method of the string to remove the portions of the full path up to the relative location.

      I created a solution below using a function so the code is not repetitive.  If you are going to compare the two csvs after and want to script it, you can use Compare-Object.

       

Viewing 1 reply thread
  • You must be logged in to reply to this topic.