Skip to content

Instantly share code, notes, and snippets.

@jahands
Last active December 22, 2023 16:26
Show Gist options
  • Save jahands/fd5a2db41d308e83ba6d to your computer and use it in GitHub Desktop.
Save jahands/fd5a2db41d308e83ba6d to your computer and use it in GitHub Desktop.
Get hash of entire directory in powershell.
Function Get-DirHash {
[Cmdletbinding()]
Param(
[Parameter(Mandatory=$true)]
[ValidateScript({
if(Test-Path -Path $_ -ErrorAction SilentlyContinue)
{
return $true
}
else
{
throw "$($_) is not a valid path."
}
})]
[string]$Path
)
$temp=[System.IO.Path]::GetTempFileName()
gci -File -Recurse $Path | Get-FileHash | select -ExpandProperty Hash | Out-File $temp -NoNewline
$hash=Get-FileHash $temp
Remove-Item $temp
$hash.Path=$Path
return $hash
}
@Charles-Isaac
Copy link

Charles-Isaac commented Feb 24, 2023

You don't need a temp file if you use MemoryStreams:
$temp = gci -File -Recurse $Path | Get-FileHash | select -ExpandProperty Hash | Out-String
$hash = Get-FileHash -InputStream ([IO.MemoryStream]::new([char[]]$temp))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment