Home » How to Get File Hash in PowerShell

How to Get File Hash in PowerShell

To get file hash in PowerShell, you can use the Get-FileHash cmdlet. This command provides different hash algorithms such as SHA1, SHA256, SHA384 and MD5.

The following method shows how you can do it in PowerShell.

Method 1: Get file hash in PowerShell

# specify the path to the file
$filePath = "C:\temp\log\system_log.txt"

# Get the hash of the file

$fileHash = Get-Hash -Path $filePath -Algorithm SHA256

# Output the file hash
Write-Output $fileHash.Hash

This example will display the hash of the file.

The following example shows how you can use this method.

Get File Hash Using Get-FileHash in PowerShell

You can get file hash in PowerShell by using the Get-FileHash cmdlet. The Get-FileHash command uses the -Algorithm parameter to specify the algorithm to calculate the hash value of the specified file.

# specify the path to the file
$filePath = "C:\temp\log\system_log.txt"

# Get the hash of the file

$fileHash = Get-FileHash -Path $filePath -Algorithm SHA256

# Output the file hash
Write-Output $fileHash.Hash

Output:

PowerShell get file hash
PowerShell get file hash

In this PowerShell script, the $filePath variable stores the path to the file for which you want to calculate the hash.

The Get-FileHash cmdlet calculates the hash of the file specified by $filePath using the algorithm SHA256 and stores the result in the $fileHash variable.

Finally, we can output the hash value of the file using the Write-Output cmdlet.

After running the above script, PowerShell will output the hash value of the specified file.

Conclusion

I hope the above article on how to get the hash value of the file in PowerShell is helpful to you.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.