Search code examples
powershellhashmd5sha256

How to export to CSV the Hash of each file (SHA-256 & MD5) that are inside a folder in Windows?


I receive files from the client and I need to make sure those files are healthy even after I process these files. What I want to beware of is a situation of possible tampering with already processed files. A customer error may occur for sending files with missing information and the customer wants to edit the original file to say the information was there. That's why I had the idea of ​​producing a report with the hash of these files that are in the reception directory. I tried hard to make everything clearer and more intelligible, even though I didn't speak English.

Scene 1: I have a directory with several text files and I need to export the SHA-256 and MD5 hash calculations from each file to a CSV somewhere.

Scene 2: I have a directory with several subfolders with customer names and within these customers, other subfolders. How to extract SHA-256 & MD5 Hash from all files in these directories and subdirectories?


Solution

  • Something like the following should get you started. This finds the SHA256 hash for every file inside the current directory (not including subdirectories)

    Get-ChildItem | Get-FileHash | Export-CSV -Path "C:\Temp\summary.csv"  
    
    PS C:\Users\Jan\Dropbox\py\advent_of_code> cat "C:\Temp\summary.csv"
    #TYPE Microsoft.Powershell.Utility.FileHash
    "Algorithm","Hash","Path"
    "SHA256","8E8FF02948E62C54EF782373500CD0D97B8A2DA0F1655A6134B37284CF5BCE79","C:\Users\Jan\Dropbox\py\advent_of_code\20191201.in"
    "SHA256","63E68322B7E8131CDDEFB77492EC7E1B8B8C46696772CE561850C854E4E8B6EA","C:\Users\Jan\Dropbox\py\advent_of_code\20191201.py"
    "SHA256","FFF1D3F7F7FBDDC4CDC90E123D9BC7B0B7A450DC28F3A4F2D786701B4A9B279D","C:\Users\Jan\Dropbox\py\advent_of_code\20191204.py"
    "SHA256","DE3662893F2779446AFC78B20E63F5250826D5F52206E5718E1A2713F876941E","C:\Users\Jan\Dropbox\py\advent_of_code\20191206.in"
    "SHA256","B9381E11A442DDC8CF802F337F0487E9269800B145106FFDDB0E6472D8C6F129","C:\Users\Jan\Dropbox\py\advent_of_code\20191206.py"    
    

    If you really need both SHA256 and MD5:

    $h = @(Get-ChildItem | Get-FileHash)
    $h2 = ($h | Get-Item | Get-FileHash -Algorithm MD5)  
    for ($i=0; $i -lt $h.Length; $i++) { 
        $h[$i] = [PSCustomObject]@{Path=$h[$i].Path; SHA256=$h[$i].Hash; MD5=$h2[$i].Hash} 
    }
    $h | Export-Csv "C:\Temp\expo.txt" 
    
    PS C:\Users\Jan\Dropbox\py\advent_of_code> cat "C:\Temp\expo.txt"
    #TYPE System.Management.Automation.PSCustomObject
    "Path","SHA256","MD5"
    "C:\Users\Jan\Dropbox\py\advent_of_code\20191201.in","8E8FF02948E62C54EF782373500CD0D97B8A2DA0F1655A6134B37284CF5BCE79","11689BA3058C306DAA3651562621BE20"
    "C:\Users\Jan\Dropbox\py\advent_of_code\20191201.py","63E68322B7E8131CDDEFB77492EC7E1B8B8C46696772CE561850C854E4E8B6EA","3F50E29C797ED4BD65122F8DA1208D4D"
    "C:\Users\Jan\Dropbox\py\advent_of_code\20191204.py","FFF1D3F7F7FBDDC4CDC90E123D9BC7B0B7A450DC28F3A4F2D786701B4A9B279D","41094A3E067669F46C965D0E34EA5CA6"
    "C:\Users\Jan\Dropbox\py\advent_of_code\20191206.in","DE3662893F2779446AFC78B20E63F5250826D5F52206E5718E1A2713F876941E","1A7571873E6B9430A2BFD846CA0B8AB7"
    "C:\Users\Jan\Dropbox\py\advent_of_code\20191206.py","B9381E11A442DDC8CF802F337F0487E9269800B145106FFDDB0E6472D8C6F129","0CBB058D315944B7B5183E33FC780A4D"