Skip to content

Instantly share code, notes, and snippets.

@Katzenwerfer
Last active April 19, 2023 06:01
Show Gist options
  • Save Katzenwerfer/7f9862ce22bbec74e39547b573cd81e1 to your computer and use it in GitHub Desktop.
Save Katzenwerfer/7f9862ce22bbec74e39547b573cd81e1 to your computer and use it in GitHub Desktop.
Wrapper function for the certutil.exe program to compute the hash of a file
function Get-Hash {
[Alias("hash")]
param (
[Parameter(
Mandatory,
Position = 0,
ValueFromPipeline = $true,
HelpMessage = "Path to a file."
)]
[ValidateNotNullOrEmpty()]
[string]
[System.IO.FileInfo]$Path,
[Parameter(
Mandatory,
Position = 1,
HelpMessage = "Available hashing algorithms:`n|MD2|MD4|MD5|SHA1|SHA256|SHA384|SHA512|"
)]
[ValidateSet(
"MD2",
"MD4",
"MD5",
"SHA1",
"SHA256",
"SHA384",
"SHA512"
)]
[string]
$HashType,
[switch]
$PrintVars
)
if ($PrintVars) {
Write-Host "HashType = |$HashType|" -ForegroundColor Red
Write-Host "Path = |$Path|" -ForegroundColor Red
Write-Host "VerbosePreference = $VerbosePreference" -ForegroundColor Red
Write-Host "DebugPreference = $DebugPreference" -ForegroundColor Red
}
if ($VerbosePreference = "Continue") {
certutil.exe -v -hashfile "$Path" $HashType
}
elseif ($VerbosePreference = "SilentlyContinue") {
certutil.exe -hashfile "$Path" $HashType
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment