Creating MD5 File Hashes

MD5 file hashes uniquely identify file content and can be used to check whether file content is identical. In PowerShell 5, there is a new cmdlet that creates the hashes for you. The code below looks for all PowerShell scripts in your user profile, and generates MD5 hashes for each file:

Get-ChildItem -Path $home -Filter *.ps1 -Recurse |
  Get-FileHash -Algorithm MD5 |
  Select-Object -ExpandProperty Hash

A better approach would associate the hash with the original path:

Get-ChildItem -Path $home -Filter *.ps1 -Recurse |
  ForEach-Object {
    [PSCustomObject]@{        
        Hash = ($_ | Get-FileHash -Algorithm MD5).Hash
        Path = $_.FullName
    }
  }

The output would now look similar to this:

 
Hash                             Path                                          
----                             ----                                          
2AE5CA30DCF6550903B994E61A714AC0 C:\Users\tobwe\.nuget\packages\Costura.Fody...
46CB505EECEC72AA8D9104A6263D2A76 C:\Users\tobwe\.nuget\packages\Costura.Fody...
2AE5CA30DCF6550903B994E61A714AC0 C:\Users\tobwe\.nuget\packages\Costura.Fody...
46CB505EECEC72AA8D9104A6263D2A76 C:\Users\tobwe\.nuget\packages\Costura.Fody...
930621EE040F82392017D240CAE13A97 C:\Users\tobwe\.nuget\packages\Fody\2.1.2\T...
39466FE42CE01CC7786D8B446C4C11C2 C:\Users\tobwe\.nuget\packages\MahApps.Metr...
2FF7910807634C984FC704E52ABCDD36 C:\Users\tobwe\.nuget\packages\microsoft.co...
C7E3AAD4816FD98443A7F1C94155954D C:\Users\tobwe\.nuget\packages\microsoft.co... 
...
 

Twitter This Tip! ReTweet this Tip!