Code
param (
$folders = @("D:\hash\") #case sensitive
)
$allFiles = foreach($folder in $folders) {
Get-Childitem -path $folder -recurse |
Select-Object FullName,Name,Length |
ForEach-Object {
$hash_SHA384 = Get-FileHash -Algorithm SHA384 $_.FullName # compute sha384 hash
$hash_SHA256 = Get-FileHash -Algorithm SHA256 $_.FullName # compute sha256 hash
$hash_MD5 = Get-FileHash -Algorithm MD5 $_.FullName # compute md5 hash
add-member -InputObject $_ -NotePropertyName SHA384 -NotePropertyValue $hash_SHA384.Hash
add-member -InputObject $_ -NotePropertyName SHA256 -NotePropertyValue $hash_SHA256.Hash
add-member -InputObject $_ -NotePropertyName MD5 -NotePropertyValue $hash_MD5.Hash
add-member -InputObject $_ -NotePropertyName File -NotePropertyValue $_.FullName.Replace($folder, '') -PassThru
}
}
Description in 3 lines
The above code does the following:
- For each folder in the variable
$folders
, get all files and folders in that folder and its subfolders. - For each file, compute its SHA384, SHA256 and MD5 hashes.
- Output the results to a file called process.txt
Description in 10 lines
Here is a short explanation for the above code:
Get-Childitem -path $folder -recurse | Select-Object FullName,Name,Length
Get all files in the folder and its subfolders, and select the FullName, Name, and Length properties.ForEach-Object { ... }
Loop through all files, and compute the hash values for each file.$hash_SHA384 = Get-FileHash -Algorithm SHA384 $_.FullName
Compute the SHA384 hash value for the current file. -.$hash_SHA256 = Get-FileHash -Algorithm SHA256 $_.FullName
Compute the SHA256 hash value for the current file.$hash_MD5 = Get-FileHash -Algorithm MD5 $_.FullName
Compute the MD5 hash value for the current file.add-member -InputObject $_ -NotePropertyName SHA384 -NotePropertyValue $hash_SHA384.Hash
- adds sha384 hash to the pipeadd-member -InputObject $_ -NotePropertyName SHA256 -NotePropertyValue $hash_SHA256.Hash
- adds sha256 hash to the pipeadd-member -InputObject $_ -NotePropertyName MD5 -NotePropertyValue $hash_MD5.Hash
- adds md5 hash to the pipe -.add-member -InputObject $_ -NotePropertyName File -NotePropertyValue $_.FullName.Replace($folder, '') -PassThru
- adds file name to the pipe$allFiles = foreach($folder in $folders)
- iterates all folders$allFiles | Format-List File, SHA384, SHA256, MD5 | Out-File -FilePath .\process.txt
- format all the data, write all output to the file process.txt.