Get FileHash - PowerShell


Overview

The Get-FileHash command calculates and retrieves hash values for files. It is an essential tool for verifying file integrity, detecting tampering, and ensuring data authenticity.

Syntax

Get-FileHash [-Path] <String[]> [-Algorithm] <String> [-AsJob] [-Force] [-Recurse]

Options/Flags

  • -Path (Required): Specifies the path(s) to the files whose hashes need to be calculated.
  • -Algorithm (Optional): Specifies the hash algorithm to use. Default is “SHA256”. Available options include:
    • “MD5”
    • “SHA1”
    • “SHA256”
    • “SHA384”
    • “SHA512”
  • -AsJob (Optional): Runs the command as a background job, allowing other operations to continue.
  • -Force (Optional): Suppresses confirmation prompts.
  • -Recurse (Optional): Calculates hashes for files in subdirectories as well.

Examples

Example 1: Calculate the SHA256 hash of a single file:

Get-FileHash -Path "C:\path\to\file.txt"

Example 2: Calculate the MD5 hashes of multiple files using a background job:

Get-FileHash -Path "C:\path\to\file1.txt" "C:\path\to\file2.txt" -Algorithm MD5 -AsJob

Example 3: Recursively calculate the SHA1 hashes of files in a directory:

Get-FileHash -Path "C:\directory\path" -Algorithm SHA1 -Recurse

Common Issues

  • File permissions: Ensure you have sufficient permissions to access the specified files.
  • Invalid path: Verify that the specified file paths are correct and exist.
  • Unsupported algorithm: Ensure the specified hash algorithm is supported by the system.

Integration

Get-FileHash can be integrated into scripts or command chains for various tasks, such as:

  • Data validation: Verify the integrity of downloaded files or data stored on different systems.
  • Forensic analysis: Collect hash values as evidence for security investigations.
  • Configuration management: Ensure that files deployed to servers have not been tampered with.
  • Compare-FileHash: Compares hash values of two or more files.
  • CertUtil -hashfile: Generates a hash value using the CertUtil tool.
  • FileCheckSum: Calculates hashes for files using the FileCheckSum utility.