Skip to content

Instantly share code, notes, and snippets.

@neuralpain
Last active June 30, 2024 15:09
Show Gist options
  • Save neuralpain/93f3c99fad7d1571ecb1b90a49f93894 to your computer and use it in GitHub Desktop.
Save neuralpain/93f3c99fad7d1571ecb1b90a49f93894 to your computer and use it in GitHub Desktop.
Get hash values of files as a batch job (multiple files at once)
<#
.SYNOPSIS
Hashes all files in a directory and sub-directories.
.DESCRIPTION
Hashes all files in a directory and sub-directories.
.PARAMETER Path
Path to the directory to hash.
.PARAMETER Algorithm
Select hash algorithm.
.EXAMPLE
HashGroup.ps1 -Path C:\Users\User\Documents
HashGroup.ps1 -Path C:\Users\User\Documents -Algotithm MD5
.NOTES
Author: neuralpain
Date created: 2024-06-26
Last modified: 2024-06-30
Version history:
0.1.0 - Initial hash function
0.2.0 - Allow user to select a specific hashing algorithm
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $false)]
[string]$Path = $pwd,
[Parameter(Mandatory = $false)]
[string]$Algorithm
)
$HashMD5 = 'NULL'
$HashSHA1 = 'NULL'
$HashSHA256 = 'NULL'
$HashSHA512 = 'NULL'
# clean up and reset
if (Test-Path "$Path\#HashGroup") { Remove-Item "$Path\#HashGroup\*" -Force -Recurse }
else { New-Item -Path $Path -Name "#HashGroup" -ItemType Directory | Out-Null }
# get files
$Files = Get-ChildItem -Path $Path -Recurse -File
$Files = $Files | Where-Object { $_.Name -notlike "*_Hashes.txt" -and $_.Name -notlike "HashGroup.ps1" } # ignore hash output files and script if present
if ($Files.Count -eq 0) { return } # exit if no files found
foreach ($File in $Files) {
switch ($Algorithm) {
'MD5' { $HashMD5 = ((Get-FileHash $File -Algorithm MD5 | Select-Object Hash) -split " ").Trim("@{Hash=}") }
'SHA1' { $HashSHA1 = ((Get-FileHash $File -Algorithm SHA1 | Select-Object Hash) -split " ").Trim("@{Hash=}") }
'SHA256' { $HashSHA256 = ((Get-FileHash $File -Algorithm SHA256 | Select-Object Hash) -split " ").Trim("@{Hash=}") }
'SHA512' { $HashSHA512 = ((Get-FileHash $File -Algorithm SHA512 | Select-Object Hash) -split " ").Trim("@{Hash=}") }
default {
$HashMD5 = ((Get-FileHash $File -Algorithm MD5 | Select-Object Hash) -split " ").Trim("@{Hash=}")
$HashSHA1 = ((Get-FileHash $File -Algorithm SHA1 | Select-Object Hash) -split " ").Trim("@{Hash=}")
$HashSHA256 = ((Get-FileHash $File -Algorithm SHA256 | Select-Object Hash) -split " ").Trim("@{Hash=}")
$HashSHA512 = ((Get-FileHash $File -Algorithm SHA512 | Select-Object Hash) -split " ").Trim("@{Hash=}")
}
}
# outout to text file
$OutFile = "$Path\#HashGroup\$($File.Name)_Hashes.txt"
if (Test-Path $OutFile) { continue } # skip if file already exists
else {
"MD5: $HashMD5" | Out-File -FilePath $OutFile -Append -Encoding utf8
"SHA1: $HashSHA1" | Out-File -FilePath $OutFile -Append -Encoding utf8
"SHA256: $HashSHA256" | Out-File -FilePath $OutFile -Append -Encoding utf8
"SHA512: $HashSHA512" | Out-File -FilePath $OutFile -Append -Encoding utf8
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment