Skip to content

Instantly share code, notes, and snippets.

@mitchelldavis
Created February 9, 2015 14:26
Show Gist options
  • Save mitchelldavis/e308abc01ad06c2e2d97 to your computer and use it in GitHub Desktop.
Save mitchelldavis/e308abc01ad06c2e2d97 to your computer and use it in GitHub Desktop.
Get-FileHash
function Get-FileHash
{
param( [string]$filePath,
[string]$alg = 'SHA256')
$stream = $null
$filePath = Resolve-Path $filePath
try
{
$algorithm = [Security.Cryptography.HashAlgorithm]::Create($alg)
$stream = ([IO.StreamReader]$filePath).BaseStream
-join ($algorithm.ComputeHash($stream) | ForEach { "{0:x2}" -f $_ })
}
catch [Exception]{
Write-Error $_
}
finally
{
if($stream -ne $null)
{
$stream.Close()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment