Skip to content

Instantly share code, notes, and snippets.

@kevinCefalu
Last active January 24, 2020 20:33
Show Gist options
  • Save kevinCefalu/2bf8691742a9a3bcd3b2810627889b37 to your computer and use it in GitHub Desktop.
Save kevinCefalu/2bf8691742a9a3bcd3b2810627889b37 to your computer and use it in GitHub Desktop.
A function to calculate hashes on files, using one or more algorithm types.
function Get-MultiFileHash
{
[CmdletBinding(DefaultParameterSetName = 'Path')]
param (
[Parameter(
Mandatory,
ParameterSetName = 'Path'
)]
[string[]] $Path,
[Parameter(
Mandatory,
ParameterSetName = 'LiteralPath',
ValueFromPipelineByPropertyName
)]
[Alias("PSPath")]
[string[]] $LiteralPath,
[Parameter(
Mandatory,
ParameterSetName = 'Stream'
)]
[IO.Stream] $InputStream,
[Parameter()]
[ValidateSet(
'SHA1', 'SHA256', 'SHA384', 'SHA512',
'MACTripleDES', 'MD5', 'RIPEMD160'
)]
[string[]] $Algorithm = 'SHA256'
);
begin
{
$Output = [Collections.Generic.List[PSCustomObject]]::new();
}
process
{
foreach ($Alg in $Algorithm)
{
$PSBoundParameters['Algorithm'] = $Alg;
$Output.Add((Get-FileHash @PSBoundParameters));
}
}
end
{
Write-Host;
$Output `
| Group-Object -Property Path `
| ForEach-Object `
{
Write-Host "File: $($_.Name)";
$_.Group `
| Select-Object `
-Property Algorithm, Hash `
| Out-String `
| Write-Host;
};
return $Output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment