Get-FileHash (Microsoft.PowerShell.Utility) - PowerShell (2024)

  • Reference
Module:
Microsoft.PowerShell.Utility

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

Syntax

Get-FileHash [-Path] <String[]> [[-Algorithm] <String>] [<CommonParameters>]
Get-FileHash [-LiteralPath] <String[]> [[-Algorithm] <String>] [<CommonParameters>]
Get-FileHash [-InputStream] <Stream> [[-Algorithm] <String>] [<CommonParameters>]

Description

The Get-FileHash cmdlet 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 identifyingthe contents of a file by its file name, extension, or other designation, a hash assigns a uniquevalue to the contents of a file. File names and extensions can be changed without altering thecontent of the file, and without changing the hash value. Similarly, the file's content can bechanged without changing the name or extension. However, changing even a single character in thecontents 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 contentsof a file have not been changed. While some hash algorithms, including MD5 and SHA1, are no longerconsidered secure against attack, the goal of a secure hash algorithm is to render it impossible tochange the contents of a file -- either by accident, or by malicious or unauthorized attempt -- andmaintain the same hash value. You can also use hash values to determine if two different files haveexactly the same content. If the hash values of two files are identical, the contents of the filesare also identical.

By default, the Get-FileHash cmdlet uses the SHA256 algorithm, although any hash algorithm thatis supported by the target operating system can be used.

Examples

Example 1: Compute the hash value for a file

This example uses the Get-FileHash cmdlet to compute the hash value for the/etc/apt/sources.list file. The hash algorithm used is the default, SHA256. The output ispiped to the Format-List cmdlet to format the output as a list.

Get-FileHash /etc/apt/sources.list | Format-ListAlgorithm : SHA256Hash : 3CBCFDDEC145E3382D592266BE193E5BE53443138EE6AB6CA09FF20DF609E268Path : /etc/apt/sources.list

Example 2: Compute the hash value for an ISO file

This example uses the Get-FileHash cmdlet and the SHA384 algorithm to compute the hash valuefor an ISO file that an administrator has downloaded from the internet. The output is piped to theFormat-List cmdlet to format the output as a list.

Get-FileHash C:\Users\user1\Downloads\Contoso8_1_ENT.iso -Algorithm SHA384 | Format-ListAlgorithm : SHA384Hash : 20AB1C2EE19FC96A7C66E33917D191A24E3CE9DAC99DB7C786ACCE31E559144FEAFC695C58E508E2EBBC9D3C96F21FA3Path : C:\Users\user1\Downloads\Contoso8_1_ENT.iso

Example 3: Compute the hash value of a stream

For this example, we get are using System.Net.WebClient to download a package from thePowershell release page. The releasepage also documents the SHA256 hash of each package file. We can compare the published hash valuewith the one we calculate with Get-FileHash.

$wc = [System.Net.WebClient]::new()$pkgurl = 'https://github.com/PowerShell/PowerShell/releases/download/v6.2.4/powershell_6.2.4-1.debian.9_amd64.deb'$publishedHash = '8E28E54D601F0751922DE24632C1E716B4684876255CF82304A9B19E89A9CCAC'$FileHash = Get-FileHash -InputStream ($wc.OpenRead($pkgurl))$FileHash.Hash -eq $publishedHashTrue

Example 4: Compute the hash of a string

PowerShell does not provide a cmdlet to compute the hash of a string. However, you can write astring to a stream and use the InputStream parameter of Get-FileHash to get the hash value.

$stringAsStream = [System.IO.MemoryStream]::new()$writer = [System.IO.StreamWriter]::new($stringAsStream)$writer.write("Hello world")$writer.Flush()$stringAsStream.Position = 0Get-FileHash -InputStream $stringAsStream | Select-Object HashHash----64EC88CA00B268E5BA1A35678A1B5316D212F4F366B2477232534A8AECA37F3C

Parameters

-Algorithm

Specifies the cryptographic hash function to use for computing the hash value of the contents of thespecified file or stream. A cryptographic hash function has the property that it is infeasible tofind two different files with the same hash value. Hash functions are commonly used with digitalsignatures and for data integrity. The acceptable values for this parameter are:

  • SHA1
  • SHA256
  • SHA384
  • SHA512
  • MD5

If no value is specified, or if the parameter is omitted, the default value is SHA256.

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

Type:String
Accepted values:SHA1, SHA256, SHA384, SHA512, MD5
Position:1
Default value:SHA256
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-InputStream

Specifies the input stream.

Type:Stream
Position:0
Default value:None
Required:True
Accept pipeline input:False
Accept wildcard characters:False

-LiteralPath

Specifies the path to a file. Unlike the Path parameter, the value of the LiteralPathparameter is used exactly as it is typed. No characters are interpreted as wildcard characters. Ifthe path includes escape characters, enclose the path in single quotation marks. Single quotationmarks instruct PowerShell not to interpret characters as escape sequences.

Type:String[]
Aliases:PSPath, LP
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-Path

Specifies the path to one or more files as an array. Wildcard characters are permitted.

Type:String[]
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:True

Inputs

String

You can pipe a string containing a path to a file to this cmdlet.

Outputs

Microsoft.PowerShell.Utility.FileHash

This cmdlet returns an object representing the path to the specified file, the value of thecomputed hash, and the algorithm used to compute the hash.

  • Format-List
Get-FileHash (Microsoft.PowerShell.Utility) - PowerShell (2024)

FAQs

How to get file hash from PowerShell? ›

If you download the files, what you'd do from PowerShell is run “Get-FileHash” and specify the path to the file you want to be checked. You'll see it generated a hash value of 82F776A89483EB2192FC41637708A820938B9091D5964530A089092CB64AEBFB, and you can compare that to the value on the web page and verify it matches.

How to check file hash value? ›

You can use Windows Powershell to calculate the SHA-256 checksum for a file.
  1. Open Windows Powershell. ...
  2. Type Get-FileHash followed by a space.
  3. Drag the downloaded ZIP file onto the Windows Powershell window after the Get-FileHash command. ...
  4. Press Enter. ...
  5. Compare the calculated hash value with the original hash value.

How to get SHA256 of file in PowerShell? ›

Using Power Shell
  1. Press Windows+R to open the Run box.
  2. Type powershell and click OK.
  3. The Windows PowerShell window will open.
  4. Run the following command: Get-FileHash C:\file\path\my_file.exe -Algorithm SHA256. ...
  5. Compare the generated value to the checksum of the file in Rublon Downloads.
Feb 8, 2024

How to generate hash value for a file? ›

How to Hash a File in One Minute
  1. Open Windows Command Line. In your Start bar, type CMD and press Enter to open Windows Command Line. ...
  2. Decide which hashing algorithm you want to use. Different hash functions (i.e., hashing algorithms) suit different needs and purposes. ...
  3. Hash a file using the certutil -hashfile command.

How to get file hash from cmd? ›

Solution:
  1. Open the Windows command line. Press Windows + R, type cmd and press Enter. ...
  2. Go to the folder that contains the file whose MD5 checksum you want to check and verify. Command: Type cd followed by the path to the folder. ...
  3. Type the command below certutil -hashfile <file> MD5. ...
  4. Press Enter.
Mar 30, 2023

How to retrieve data from hash? ›

A hash table uses a hash function to convert the keys into hashes, and then stores the values in an array or a list at the corresponding index. This way, you can quickly access the value for any key by computing its hash and looking it up in the array or list.

What is a hash file check? ›

It's a way to check the integrity of a file, if the hash is not the same that would probably means that it's corrupted or being modified by third part.

What is the hash total of a file? ›

hash total A number produced by adding together (or otherwise combining) corresponding fields over all the records of a file, when such a total does not have any external meaning but is used solely to verify the records in the file.

How to check hash key in PowerShell? ›

To retrieve a value from a Hashtable using a key in PowerShell, you can use the square bracket notation. For example, if your Hashtable is named $hash and you want to retrieve the value associated with the key “key1”, you can use $hash[“key1”]. This will return the corresponding value.

How to get MD5 hash of a file? ›

Open a terminal window. Type the following command: md5sum [type file name with extension here] [path of the file] -- NOTE: You can also drag the file to the terminal window instead of typing the full path. Hit the Enter key. You'll see the MD5 sum of the file.

What is the tool for file hash? ›

Hash Tool is a utility to calculate the hash of multiple files. A file hash can be said to be the 'signature' of a file and is used in many applications, including checking the integrity of downloaded files. This compact application helps you quickly and easily list the hashes of your files.

Does every file have a hash value? ›

In practice, yes. A hash code (MD5, SHA-1, SHA-256) is of a fixed length, so it cannot be unique for all possible inputs. But all such hash functions are carefully designed to minimize the likelihood of a collision (two distinct files with the same hash value).

What is the hash value of empty file? ›

The file with SHA256 hash e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 represents an empty file, i.e. zero bytes in size. Being empty, it can't exhibit malicious behaviour by itself.

How to get SHA256 hash of file windows? ›

How to
  1. In a command line, run the command: For Windows: certutil -hashfile [file location] SHA256 . For example: certutil -hashfile C:\Users\user1\Downloads\software.zip SHA256. ...
  2. Compare the value returned by the command line with the value from the pop over of the file in the Download Center interface.

How to get the MD5 hash of a file? ›

Open a terminal window. Type the following command: md5sum [type file name with extension here] [path of the file] -- NOTE: You can also drag the file to the terminal window instead of typing the full path. Hit the Enter key.

How do you convert to hash in PowerShell? ›

To convert any command output to the hashtable in PowerShell, we can first convert the output to the JSON format using the ConvertTo-JSON command, and applying the ConvertFrom-JSON command from the first output will produce an output to the Hashtable.

Top Articles
Latest Posts
Article information

Author: Cheryll Lueilwitz

Last Updated:

Views: 5642

Rating: 4.3 / 5 (54 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Cheryll Lueilwitz

Birthday: 1997-12-23

Address: 4653 O'Kon Hill, Lake Juanstad, AR 65469

Phone: +494124489301

Job: Marketing Representative

Hobby: Reading, Ice skating, Foraging, BASE jumping, Hiking, Skateboarding, Kayaking

Introduction: My name is Cheryll Lueilwitz, I am a sparkling, clean, super, lucky, joyous, outstanding, lucky person who loves writing and wants to share my knowledge and understanding with you.