Short answer: Get-FileHash is PowerShell’s built-in cmdlet for file hashes. Get-FileHash .\file.iso returns the SHA-256; add -Algorithm MD5 (or SHA1, SHA384, SHA512) for other hashes. Pipe Get-ChildItem -Recurse -File into it to hash an entire folder.

Syntax and algorithms

PowerShellGet-FileHash [-Path] <string[]> [-Algorithm <string>] Get-FileHash -LiteralPath <string[]> [-Algorithm <string>] Get-FileHash -InputStream <Stream> [-Algorithm <string>]
  • -Path accepts wildcards (*.iso); -LiteralPath takes the path exactly as written (use it for file names containing [ or ]).
  • -Algorithm defaults to SHA256. Also accepted: SHA1, SHA384, SHA512, MD5. Windows PowerShell 5.1 additionally accepts MACTripleDES and RIPEMD160; PowerShell 7 removed them.
  • The output object has three properties: Algorithm, Hash (uppercase hex) and Path.
  • Available in PowerShell 4.0 and later, including PowerShell 7 on macOS and Linux.

Hash a file: SHA-256, MD5, SHA-1

PowerShellGet-FileHash .\empty.txt Algorithm Hash Path --------- ---- ---- SHA256 E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855 C:\Temp\empty.txt Get-FileHash .\empty.txt -Algorithm MD5 # D41D8CD98F00B204E9800998ECF8427E Get-FileHash .\empty.txt -Algorithm SHA1 # DA39A3EE5E6B4B0D3255BFEF95601890AFD80709

Those are the real hashes of a zero-byte file. To get just the hash string, for a script or the clipboard:

PowerShell(Get-FileHash .\setup.exe).Hash (Get-FileHash .\setup.exe -Algorithm MD5).Hash | Set-Clipboard # lowercase, to match sha256sum / vendor pages (Get-FileHash .\setup.exe).Hash.ToLower()

If the table cuts off long paths, add | Format-List.

Several files and wildcards

PowerShellGet-FileHash .\setup.exe, .\driver.sys, .\readme.pdf Get-FileHash C:\Downloads\*.iso -Algorithm SHA256 Get-FileHash C:\Downloads\* | Format-Table Hash, Path -AutoSize

Hash a folder recursively and export to CSV

Get-FileHash has no -Recurse switch of its own. Instead, let Get-ChildItem find the files and pipe them in. -File skips directories, which would otherwise produce errors:

PowerShellGet-ChildItem -Path C:\Evidence -Recurse -File | Get-FileHash -Algorithm SHA256 | Export-Csv -Path C:\Cases\hashes.csv -NoTypeInformation

The CSV has Algorithm, Hash and Path columns. To add file size and modification time, build the rows yourself:

PowerShellGet-ChildItem -Path C:\Evidence -Recurse -File | ForEach-Object { [pscustomobject]@{ Path = $_.FullName Size = $_.Length LastWriteTime = $_.LastWriteTime SHA256 = (Get-FileHash -LiteralPath $_.FullName -Algorithm SHA256).Hash } } | Export-Csv -Path C:\Cases\hashes.csv -NoTypeInformation

Include hidden and system files with -Force on Get-ChildItem. To hash only certain types, add -Include *.exe, *.dll.

Compare against a published checksum

PowerShell’s -eq is case-insensitive, so Get-FileHash’s uppercase output compares correctly with a lowercase checksum from a website:

PowerShell$expected = "paste-the-published-sha256-here" $actual = (Get-FileHash .\setup.exe -Algorithm SHA256).Hash if ($actual -eq $expected) { "OK: hashes match" } else { "MISMATCH: $actual" }

Make sure you use the same algorithm as the publisher: a 32-character value is MD5, 40 is SHA-1, 64 is SHA-256.

Compare two folders

To find files that differ between two copies of a folder (a backup and the original, a clean build and a suspect one), hash both and compare on the Hash property:

PowerShell$a = Get-ChildItem C:\Original -Recurse -File | Get-FileHash $b = Get-ChildItem D:\Copy -Recurse -File | Get-FileHash Compare-Object $a $b -Property Hash -PassThru | Select-Object SideIndicator, Hash, Path

<= means the content exists only in the first folder; => only in the second. Identical files in both don’t appear at all.

Hash a string

Get-FileHash works on streams, so you can hash text without writing a file. The encoding changes the bytes and therefore the hash, so be explicit:

PowerShell$bytes = [System.Text.Encoding]::UTF8.GetBytes("hello") $stream = [System.IO.MemoryStream]::new($bytes) (Get-FileHash -InputStream $stream -Algorithm SHA256).Hash # 2CF24DBA5FB0A30E26E83B2AC5B9E29E1B161E5C1FA7425E73043362938B9824

Locked files and errors

  • “The process cannot access the file because it is being used by another process”: the file is open with an exclusive lock (common for live registry hives, databases and the pagefile). Hash a copy, or a forensic image or Volume Shadow Copy of the disk, instead of the live file.
  • Access denied: run PowerShell as Administrator, or skip unreadable files and keep going with -ErrorAction SilentlyContinue on both Get-ChildItem and Get-FileHash.
  • Paths with brackets (file[1].txt): use -LiteralPath, because -Path treats brackets as wildcards.

Look the hashes up in NIST NSRL

Once you have a folder of hashes, the question is usually which of these files do I already know? The NIST National Software Reference Library catalogs the hashes of 1.5 billion+ files from published software, so matching against it lets you set the operating-system and application files aside and focus on the rest.

Turn the CSV into a list of unique hashes, then look them up with the free hashscanner CLI (Python) or straight from PowerShell with the REST API:

PowerShellImport-Csv C:\Cases\hashes.csv | Select-Object -ExpandProperty Hash | Sort-Object -Unique | Set-Content C:\Cases\hashes.txt pip install hashscanner $env:HASHSCANNER_API_KEY = "hs_xxxx_sk_xxxx" hashscanner bulk C:\Cases\hashes.txt --format csv -o C:\Cases\nsrl-results.csv
PowerShell (single lookup, no install)$headers = @{ Authorization = "Bearer $env:HASHSCANNER_API_KEY" } $hash = (Get-FileHash .\setup.exe).Hash Invoke-RestMethod -Uri "https://api.hashscanner.com/v1/hash/$hash" -Headers $headers -SkipHttpErrorCheck

A hash that isn’t in NSRL comes back as HTTP 404 with "found": false. That’s a normal answer, not a failure; -SkipHttpErrorCheck (PowerShell 7) returns it as data instead of throwing. In Windows PowerShell 5.1, wrap the call in try/catch.

Use Set-Content rather than Out-File or > for the hash list: in Windows PowerShell 5.1 the latter two write UTF-16, which other tools may not read. For just a handful of hashes, paste them into the lookup on the home page. A free account includes 2,500 lookups a month and bulk jobs of up to 100 hashes; paid plans go up to 100,000 per job.

Found means known, not safe. An NSRL match says the file has been cataloged as part of published software. It is not a verdict on whether the file is benign or malicious.

FAQ

What algorithm does Get-FileHash use by default?

SHA-256. Add -Algorithm MD5, SHA1, SHA384 or SHA512 to change it. Windows PowerShell 5.1 also accepts MACTripleDES and RIPEMD160; PowerShell 7 dropped those two.

How do I get an MD5 hash in PowerShell?

Get-FileHash .\file.zip -Algorithm MD5. To print only the hash: (Get-FileHash .\file.zip -Algorithm MD5).Hash.

Can Get-FileHash hash a whole folder?

Not by itself, but it accepts files from the pipeline: Get-ChildItem C:\Folder -Recurse -File | Get-FileHash hashes every file in the folder and its subfolders.

Why are Get-FileHash hashes in uppercase?

That is just how PowerShell formats hex. Hashes are case-insensitive, so an uppercase SHA-256 from Get-FileHash equals the lowercase one from sha256sum. PowerShell’s -eq comparison is case-insensitive too.

Which PowerShell version has Get-FileHash?

PowerShell 4.0 and later, so every supported version of Windows has it, as does PowerShell 7 on macOS and Linux.

Got a list of hashes? Look them up in NIST NSRL

Paste up to 10 on the home page, no account needed. Free accounts get 2,500 lookups a month by API.