How to use the command Get-FileHash (with examples)
- Windows
- December 25, 2023
The Get-FileHash command is a PowerShell command used to calculate a hash for a file. It supports multiple hashing algorithms such as SHA1, SHA384, SHA256, SHA512, and MD5. By calculating a hash for a file, you can verify the integrity of the file and ensure that it has not been tampered with.
Use case 1: Calculate a hash for a specified file using the SHA256 algorithm
Code:
Get-FileHash path\to\file
Motivation:
One possible motivation for using this example is to verify the integrity of a downloaded file. By calculating the hash of the file and comparing it to the expected hash provided by the source, you can ensure that the file has not been modified during the download process.
Explanation:
- Get-FileHash: This is the command name that instructs PowerShell to calculate a hash for a file.
- path\to\file: This is the path to the file for which you want to calculate the hash.
Example output:
Algorithm Hash Path
--------- ---- ----
SHA256 B520E4400CB6B588E7A934D69F0221015698C166B79862D82C15DE4C7E40D3F1 path\to\file
This output shows the algorithm used (SHA256), the calculated hash, and the path to the file.
Use case 2: Calculate a hash for a specified file using a specified algorithm
Code:
Get-FileHash path\to\file -Algorithm SHA1|SHA384|SHA256|SHA512|MD5
Motivation:
The motivation for using this example is to calculate a hash for a file using a specific hashing algorithm. Different algorithms provide different levels of security and speed, so choosing the right algorithm can be important for your specific use case.
Explanation:
- Get-FileHash: This is the command name that instructs PowerShell to calculate a hash for a file.
- path\to\file: This is the path to the file for which you want to calculate the hash.
- -Algorithm: This argument specifies the hashing algorithm to be used. You can choose one of the following options: SHA1, SHA384, SHA256, SHA512, or MD5.
Example output:
Algorithm Hash Path
--------- ---- ----
SHA1 9E107D9D372BB6826BD81D3542A419D6EBE83F1B path\to\file
This output shows the algorithm used (SHA1), the calculated hash, and the path to the file.
Conclusion:
The Get-FileHash command is a useful tool for calculating the hash of a file. By using the appropriate algorithms, you can verify the integrity and authenticity of files, ensuring that they have not been tampered with. Whether you need to confirm the integrity of downloaded files or perform other hash-related operations, Get-FileHash is a powerful command in your PowerShell toolkit.