To generate a file hash in PowerShell, use the Get-FileHash cmdlet followed by the path to your file. This command creates a unique alphanumeric string (a checksum) that allows you to verify that a file has not been corrupted or maliciously altered during a download or transfer.
What You'll Need
- A Windows computer with PowerShell installed (Windows PowerShell 5.1 or PowerShell 7+).
- The file you wish to hash.
- The expected hash value (usually provided by the software developer on their download page) for comparison.
Step-by-Step
- Open PowerShell: Click the Start menu, type "PowerShell," and select the application. Depending on your system version, labels may differ slightly, but the core functionality remains the same.
- Locate your file: Find the file you want to verify in File Explorer. Right-click the file and select "Copy as path" to make entering the location easier.
- Run the basic hash command: Type the following command and press Enter:
Get-FileHash "C:\path\to\your\file.exe"(Replace the path inside the quotes with the actual path to your file). - Review the output: PowerShell will return a table containing the Algorithm (default is SHA-256), the Hash value, and the Path.
- Change the algorithm (Optional): If the website provides an MD5 or SHA-1 hash instead of SHA-256, specify the algorithm using the
-Algorithmparameter:Get-FileHash "C:\path\to\your\file.exe" -Algorithm MD5The supported algorithms are SHA1, SHA256, SHA384, SHA512, and MD5. - Compare the results: Manually compare the string of characters produced by PowerShell with the one provided by the source. If they match exactly, the file is authentic and intact.
Common Mistakes
- Forgetting Quotes: If your file path contains spaces (e.g.,
C:\Users\Name\Downloads\My File.zip), the command will fail unless the path is enclosed in double quotes. - Using the Wrong Algorithm: Comparing a SHA-256 hash from PowerShell against an MD5 hash from a website will result in a mismatch, even if the file is perfect. Always ensure the algorithms match.
- Misreading Characters: Hashes are case-insensitive in most contexts, but a single character difference (such as a '0' instead of an 'O') means the file is different.
Tips
- Fast Comparison: Instead of eyeing the long string, you can pipe the result into a comparison command. For example:
(Get-FileHash "C:\path\to\file.exe").Hash -eq "EXPECTED_HASH_HERE"PowerShell will returnTrueif they match andFalseif they do not. - Hashing Multiple Files: You can use wildcards to hash all files in a folder:
Get-FileHash C:\MyFolder\* - Tab Completion: You don't have to type the whole path. Type the first few letters of a folder and press the Tab key to let PowerShell autocomplete the name.
Troubleshooting
If the command does not go as planned, try these steps in order:
- Check File Permissions: If you get an "Access Denied" error, ensure the file isn't open in another program or try running PowerShell as an Administrator.
- Verify Path Accuracy: Ensure the path is correct. If you see "Cannot find path," check for typos or ensure you used quotes around paths with spaces.
- Restart PowerShell: If the console is hanging or behaving unexpectedly, close the window and open a new session.
- Check File Existence: Ensure the file hasn't been moved or deleted since you copied the path.
Frequently Asked Questions
What is a file hash?
A hash is a "digital fingerprint" created by a mathematical algorithm. Any change to the original file, even a single bit, will result in a completely different hash value.
Is SHA-256 better than MD5?
Yes. SHA-256 is significantly more secure and less prone to "collisions" (where two different files produce the same hash) than MD5, which is now considered cryptographically broken.
Does hashing a file change the file itself?
No. The Get-FileHash command only reads the data within the file to calculate the string; it does not modify, move, or rewrite the file.
Why does my hash not match the website's hash?
This usually happens for three reasons: the download was corrupted, the file was modified by a third party (security risk), or you are using a different hashing algorithm than the one listed on the site.
Can I use this for folders?
No, Get-FileHash works on individual files. To hash a folder, you must either zip the folder into a single archive first or use a loop script to hash every file inside the directory.