Profile picture
Sidharth R
  • Home
  • Posts
  • Journal
  • Home
  • Posts
  • Journal
  • Search

Powershell - Get Hashes of All Files in a Directory

Updated: 13 Sep 2023 ⚬ Page views: 485

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:

  1. For each folder in the variable $folders, get all files and folders in that folder and its subfolders.
  2. For each file, compute its SHA384, SHA256 and MD5 hashes.
  3. 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 pipe
  • add-member -InputObject $_ -NotePropertyName SHA256 -NotePropertyValue $hash_SHA256.Hash - adds sha256 hash to the pipe
  • add-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.

References

  • Get-FileHash Microsoft.PowerShell.Utility - PowerShell

Nerdsid.com

Links
  • Home
  • Contact
  • About
  • Posts
  • Journal
  • Quotes
© 2025 Sidharth R.
All content on this website is licensed under CC BY-NC-SA 4.0.