How to use the command Get-FileHash (with examples)

How to use the command Get-FileHash (with examples)

The Get-FileHash command is a useful PowerShell tool that calculates the cryptographic hash value for a specified file. This hash value is a fixed-size string of characters generated from the file’s contents, ensuring data integrity and authenticity. It is widely used for verifying file integrity, determining duplicates, and ensuring files have not been tampered with. With PowerShell, users not only get a simple way to hash files but also the flexibility to choose from various algorithms that cater to different security needs.

Calculate a hash for a specified file using the SHA256 algorithm

Code:

Get-FileHash path\to\file

Motivation:

When dealing with file integrity checks or cryptographic applications, you often need a dependable method to generate hashes. The SHA256 algorithm is one of the most commonly used cryptographic functions due to its balance of security and performance. By default, Get-FileHash uses SHA256 to compute the hash of a file, which is ideal for ensuring that the file has not been altered or corrupt over time. This use case is particularly beneficial for verifying downloaded files against a vendor-provided hash to ensure files are authentic and untouched.

Explanation:

  • Get-FileHash: This is the command used to start the process of generating the hash value of a specified file.
  • path\to\file: This argument specifies the path to the file for which the hash is being calculated. You need to provide the full or relative path to the target file. As no algorithm is specified, the command defaults to using the SHA256 algorithm.

Example Output:

Algorithm       Hash                                                                   Path
---------       ----                                                                   ----
SHA256          6A2C1F5BB12E874D9D2B7EF8E49ABB7CD58FC2E3C44AB25A9A2F8E69BD2C879F       C:\Users\Example\Documents\example.txt

Calculate a hash for a specified file using a specified algorithm

Code:

Get-FileHash path\to\file -Algorithm SHA1

Motivation:

Different scenarios may call for different cryptographic hash functions due to varying levels of security and performance needs. For instance, if you are working in an environment where compatibility with older systems is required, you might need to use SHA1, an earlier algorithm that is still common despite being weaker than its successors. Get-FileHash offers flexibility by allowing you to specify various algorithms such as SHA1, SHA384, SHA512, and MD5, accommodating diverse use cases from legacy systems to security-critical applications.

Explanation:

  • Get-FileHash: This command initiates the hashing process.
  • path\to\file: The path to the particular file for which the hash will be computed.
  • -Algorithm SHA1: This specifies the cryptographic hash function that will be used. In this example, SHA1 is selected as the desired algorithm. This argument allows users to specify alternatives to the default (SHA256), giving them the ability to choose based on security needs or compatibility requirements.

Example Output:

Algorithm       Hash                                                                   Path
---------       ----                                                                   ----
SHA1            B054B000B1665DB4D4E82B2E8DF453D4A9F2E3A3                               C:\Users\Example\Documents\example.txt

Conclusion

The Get-FileHash PowerShell command is a versatile tool for computing cryptographic hashes with capabilities to support a variety of algorithms. By verifying files against hash values, users can ensure file integrity, guard against corruption, and avoid tampering. This article illustrated how to compute file hashes using default and specified algorithms, showcasing the command’s practicality in diverse IT environments, from simple integrity checks to complex integrations with legacy systems.

Related Posts

How to Use the Command 'fwupdmgr' (with Examples)

How to Use the Command 'fwupdmgr' (with Examples)

The fwupdmgr command is a powerful tool used for managing firmware updates on Linux-based systems.

Read More
How to Use the `unsquashfs` Command (with Examples)

How to Use the `unsquashfs` Command (with Examples)

The unsquashfs command is a powerful utility for dealing with SquashFS filesystems, which are highly compressed and read-only filesystems typically used for software distribution and deployment in Linux environments.

Read More
Exploring the `coproc` Command in Bash (with examples)

Exploring the `coproc` Command in Bash (with examples)

The coproc command in Bash enables users to create interactive asynchronous subshells.

Read More