Powershell

How to Use the Get-FileHash PowerShell Cmdlet

The “Get-FileHash” cmdlet computes the hash algorithm of a file, string, or application with the aid of the specified algorithm. The hash values are utilized to make a comparison between two files to examine whether they have the same data or not. In case the hash values of both files are the same, then the content inside the files is the same. Moreover, it is also helpful when checking the hash algorithm of the file downloaded from the internet.

The following blog will observe the techniques to get the hash values of files.

How to Use the Get-FileHash PowerShell Cmdlet?

The cmdlet “Get-FileHash” cmdlet can get the hash values of a string, file, or application. For further understanding, go through the below-provided examples.

Example 1: Get/Retrieve the Hash Value of a Specified String

This example will get the hash value of a string mentioned by the user:

$stringAsStream = [System.IO.MemoryStream]::new()

$writer = [System.IO.StreamWriter]::new($stringAsStream)

$writer.write("Linux Hint")

$writer.Flush()

$stringAsStream.Position = 0

Get-FileHash -InputStream $stringAsStream | Select-Object Hash

According to the above code:

  • First, initialize a variable and then specify the “new()” constructor with the “[System.IO.MemoryStream]” class.
  • After that, initialize “$writer” variable and then attach the “new()” constructor with the variable “$stringAsStream” inside it.
  • Then, assign it the “[System.IO.StreamWriter]” class.
  • After that, concatenate the “$Writer” variable with the “write()” method and add the string “Linux Hint” inside the “Write()” method.
  • In the next line, concatenate the “$Writer” variable with the “Flush()” method.
  • Then, concatenate the “$stringAsStream” variable with the “Position” and assign it the value “0”.
  • After that, specify the “Get-FileHash” cmdlet, then define the “-InputStream” parameter and assign it the “$stringAsStream” variable.
  • Lastly, add the pipeline “|” and define the “Select-Object” cmdlet followed by the “Hash” value:

Example 2: Get the Hash Value of a File

This example will demonstrate about getting the hash value of the specified file:

Get-FileHash C:\Doc\File.txt

According to the above code, first, add the “Get-FileHash” cmdlet and assign the file path to it:

Example 3: Get the Hash Value of the Notepad Application

The following example will retrieve the hash value of the Notepad application:

Get-FileHash C:\Windows\notepad.exe

That was all about getting the file hash in PowerShell.

Conclusion

The cmdlet “Get-FileHash” is specially designed to get the hash algorithm of a specified file by the user. Moreover, it can also get the hash value of a string or an application. The hash value is used to compare whether or not two files have the same content. This blog has covered major information about the “Get-FileHash” cmdlet and its usage.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.