Changing the entries in a CSV file with MD5 hashes

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:

#Getting the MD5 hash of the source and storing it a csv format
$SourcePath = Get-ChildItem -Path 'C:\source\Folder1' -Recurse
$SourceHash = foreach ($File in $InstallerPath) 
{
    Get-FileHash $File.FullName -Algorithm MD5 -ErrorAction SilentlyContinue
}
$SourceHash | Export-Csv -Path C:\Users\Admin\Desktop\Exports\SourceHash.csv

#Getting the MD5 hash of the destination and storing it in a csv format
$DestinationPath = Get-ChildItem -Path "C:\destination\Folder1" -Recurse
$DestinationHash = foreach ($File in $DestinationPath) 
{
    Get-FileHash $File.FullName -Algorithm MD5 -ErrorAction SilentlyContinue 
}
$DestinationHash | Export-Csv -Path C:\Users\Admin\Desktop\Exports\DestinationHash.csv

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.

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.

$myhash.hash

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

$myhash | select-object -expandproperty hash

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.

function Hashit ($source, $destination)
{
    Get-ChildItem -Path $Source -file -Recurse |
        Select-Object @{n="Hash";e={Get-FileHash $_.FullName -Algorithm MD5 | Select-Object -ExpandProperty Hash}},
                      @{n="FileName";e={$_.FullName.Replace($Source,"")}} |
                        Export-Csv -Path $destination
} #function Hashit

Hashit -source 'C:\source\Folder1' -destination 'C:\Users\Admin\Desktop\Exports\SourceHash.csv'
Hashit -source 'C:\destination\Folder1' -destination 'C:\Users\Admin\Desktop\Exports\DestinationHash.csv'