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
- -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 acceptsMACTripleDESandRIPEMD160; PowerShell 7 removed them. - The output object has three properties:
Algorithm,Hash(uppercase hex) andPath. - Available in PowerShell 4.0 and later, including PowerShell 7 on macOS and Linux.
Hash a file: SHA-256, MD5, SHA-1
Those are the real hashes of a zero-byte file. To get just the hash string, for a script or the clipboard:
If the table cuts off long paths, add | Format-List.
Several files and wildcards
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:
The CSV has Algorithm, Hash and Path columns. To add file size and modification time, build the rows yourself:
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:
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:
<= 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:
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 SilentlyContinueon bothGet-ChildItemandGet-FileHash. - Paths with brackets (
file[1].txt): use-LiteralPath, because-Pathtreats 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:
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.