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:
$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:
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:
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.