PowerShell Get-FileHash: Get SHA256/MD5 Hashes and Export a Folder to CSV

スポンサーリンク
PowerShell Get-FileHash: Get SHA256/MD5 Hashes and Export a Folder to CSV File Operations
スポンサーリンク

PowerShell’s Get-FileHash gives you a file’s hash (SHA256 by default) in one line. Below: the basics for a single file, comparing two files, checking a download against a published hash, then exporting the hashes of every file in a folder to CSV.

At work I check whether prepared materials (config files and so on) have changed behind my back by comparing hashes, so I export folder-wide hash lists regularly. Short answer for that: Get-ChildItem -File -Recurse piped to Get-FileHash and Export-Csv.

Basic usage

Pass a file path and you get the algorithm, the hash and the path.

Get-FileHash C:\work\sample.zip

Choose the algorithm

Without options it uses SHA256. Use -Algorithm for another one: SHA1, SHA256, SHA384, SHA512 and MD5 are available.

Get-FileHash C:\work\sample.zip -Algorithm MD5

Microsoft’s documentation notes that MD5 and SHA1 are no longer considered secure: use them only for simple change detection, not to protect against tampering.

Get just the hash, compare two files

For only the hash string, add .Hash:

(Get-FileHash C:\work\sample.zip).Hash

To check whether two files are identical, compare their hashes. You get True when they match, False otherwise.

(Get-FileHash C:\work\a.txt).Hash -eq (Get-FileHash C:\work\b.txt).Hash

Checking a download against the hash its publisher lists works the same way. PowerShell’s -eq is case-insensitive, so a lowercase published hash compares fine.

(Get-FileHash C:\work\sample.zip).Hash -eq "paste-the-published-hash-here"

Export hashes for a whole folder to CSV

This one line hashes every file under a folder, subfolders included, and saves the result as CSV. Change the two paths.

Get-ChildItem -File -Recurse "C:\work\target" | Get-FileHash | Export-Csv "C:\work\hashes.csv" -NoTypeInformation -Encoding UTF8

The CSV has Algorithm, Hash and Path columns. If you give only a file name for the CSV, it’s created in the current folder.

Add created and modified dates

When a hash changes, knowing when the file was created and last modified helps track down what happened. This version adds those columns and puts the date and time in the CSV file name, so each run keeps its own list. Feel free to use it.

$target = "C:\work\target"
$csv    = "C:\work\hashes_$(Get-Date -Format 'yyyyMMdd_HHmmss').csv"

Get-ChildItem -File -Recurse $target | ForEach-Object {
    $h = Get-FileHash -Path $_.FullName
    [PSCustomObject]@{
        Algorithm     = $h.Algorithm
        Hash          = $h.Hash
        CreationTime  = $_.CreationTime
        LastWriteTime = $_.LastWriteTime
        Path          = $_.FullName
    }
} | Export-Csv $csv -NoTypeInformation -Encoding UTF8
Write-Host "Saved: $csv"

Hashing reads every file to the end, so large folders take a while.

To compare two of these lists, or two folders directly, and see what was added, removed or changed, see PowerShell: compare two folders by file hash.

Notes

  • If running a saved .ps1 gives “running scripts is disabled on this system”, check the execution policy (about_Execution_Policies - Microsoft Learn)
  • In Windows PowerShell 5.1, -Encoding UTF8 writes UTF-8 with a BOM, so Excel shows non-English file names correctly. In PowerShell 7, use -Encoding utf8BOM

Reference: Get-FileHash - Microsoft Learn

コメント

Copied title and URL