Get-FileHash

Compute the hash value for a file by using a specified hash algorithm.

Syntax
      Get-FileHash [-Path] String[]
         [-Algorithm {SHA1 | SHA256 | SHA384 | SHA512 | MACTripleDES | MD5 | RIPEMD160}]
            [CommonParameters]

      Get-FileHash -LiteralPath String[]
         [-Algorithm {SHA1 | SHA256 | SHA384 | SHA512 | MACTripleDES | MD5 | RIPEMD160}]
            [CommonParameters]

      Get-FileHash -InputStream Stream
         [-Algorithm {SHA1 | SHA256 | SHA384 | SHA512 | MACTripleDES | MD5 | RIPEMD160}]
            [CommonParameters]
Key
   -Algorithm String
      The cryptographic hash function to use for computing the hash value of the contents of the specified file.
      A cryptographic hash function includes the property that it is not possible to find two distinct inputs
      that generate the same hash values. Hash functions are commonly used with digital signatures and for data
      integrity.
      Accepted values:
      SHA1, SHA256, SHA384, SHA512, MD5, MACTripleDES, RIPEMD160
      The default value is SHA256.

      For security reasons, MD5 and SHA1, which are no longer considered secure, should only be used for simple change
      validation, and should not be used to generate hash values for files that require protection from attack or tampering.

      MACTripleDES and RIPEMD160 are no longer mentioned in the MS docs, but do still appear to work. MACTripleDES is a
      reversible encryption algorithm which will give a different hash each time it is run.

   -InputStream Stream
      The input stream.

   -LiteralPath String[]
      The path to a file. Unlike the -Path parameter, the value of -LiteralPath is used exactly as it is typed.
      No characters are interpreted as wildcard characters. If the path includes escape characters, enclose the path in
      single quotation marks. Single quotation marks instruct PowerShell not to interpret characters as escape sequences.

   -Path String[]
      The path to one or more files as an array. Wildcard characters are permitted.

Standard Alias for Get-FileHash: n/a

Get-FileHash computes the hash value for a file by using a specified hash algorithm. A hash value is a unique value that corresponds to the content of the file. Rather than identifying the contents of a file by its file name, extension, or other designation, a hash assigns a unique value to the contents of a file.

File names and extensions can be changed without altering the content of the file, and without changing the hash value. Similarly, the file’s content can be changed without changing the name or extension. However, changing even a single character in the contents of a file changes the hash value of the file.The purpose of hash values is to provide a cryptographically-secure way to verify that the contents of a file have not been changed.

While some hash algorithms, including MD5 and SHA1, are no longer considered secure against attack, the goal of a secure hash algorithm is to render it impossible to change the contents of a file-either by accident, or by malicious or unauthorized attempt-and maintain the same hash value. You can also use hash values to determine if two different files have exactly the same content. If the hash values of two files are identical, the contents of the files are also identical. By default, the Get-FileHash cmdlet uses the SHA256 algorithm, although any hash algorithm that is supported by the target Operating System can be used.

Examples

Compute the SHA256 hash value for a PowerShell.exe file:

PS C:\> Get-FileHash $pshome\powershell.exe | Format-List
Algorithm : SHA256
Hash : 6A785ADC0263238DAB3EB37F4C185C8FBA7FEB5D425D034CA9864F1BE1C1B473
Path : C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Compute the SHA512 hash value for an ISO file:

PS C:\> Get-FileHash C:\demo\sample.iso -Algorithm SHA512 | Format-List

Compute the default SHA256 hash value for all the files in a folder:

PS C:\> Get-ChildItem 'c:\demo' | Get-FileHash | Select-Object Hash,Path

Compute the hash value of a stream:

$testfile = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
    
## open $testfile as a stream
$testfilestream = [System.IO.File]::Open(
    $testfile,
    [System.IO.FileMode]::Open,
    [System.IO.FileAccess]::Read)
    
Get-FileHash -InputStream $testfilestream -Algorithm SHA512

“Poets don’t draw. They unravel their handwriting and then tie it up again, but differently” ~ Jean Cocteau

Related PowerShell Cmdlets

Unblock-File - Unblock files downloaded from the Internet.
CMD: File Checksum Integrity Verifier (FCIV.exe) - 2019 download, compute and verify cryptographic hash values (MD5 or SHA1) of files.
WINGET hash - Generate a SHA256 hash for an installer file.


 
Copyright © 1999-2024 SS64.com
Some rights reserved