Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate and insert SHA in filename in Powershell?

Tags:

powershell

I wish to calculate and filter files by SHA values to find duplicate files. I have over 6000 image files that I wish to perform this on in a single folder.

I would Like to calculate AND Insert the SHA value for a file into their filename.

This is what I am trying:

 get-childitem * rename-item -NewName {$_.Name -replace '*' '*'+ Get-FileHash *./}

Where the + is where I wish to add the SHA value. Here are the filenames and the errors I am getting. https://i.sstatic.net/MjWGN.jpg

like image 283
obadul024 Avatar asked Dec 29 '25 04:12

obadul024


1 Answers

$_ refers to the current file inside the -NewName block, which is everything you need to calculate the hash:

Get-ChildItem -Path C:\Folder\With\Files\ -File | Rename-Item -NewName {
  # pipe current file to Get-FileHash, grab resulting hash string
  $hash = $_ |Get-FileHash -Algorithm SHA1 |Select -Expand Hash
  # rename from `basename.ext` to `basename_SHA1HASH.ext`
  $_.BaseName + '_' + $hash + $_.Extension
}

That being said, if you're trying to identify duplicates in the same folder (or folder hierarchy) there's no need to rename the files, you can group the resulting hashes in-memory and filter for duplicates that way instead:

# Create dictionary to keep track of Hash->FilePath(s) relation:
$FilesBySHA1Sum = @{}

# Enumerate all files 
Get-ChildItem -Path C:\folder\with\target\files -File |ForEach-Object {
  # Calculate individual file hash
  $SHA1Sum = $_ |Get-FileHash -Algorithm SHA1
  
  if($SHA1Sum){
    # Add result to hash table
    $FilesBySHA1Sum[$SHA1Sum.Hash] += @($SHA1Sum.Path)
  }
}

# Figure out which entries in the hash table has more than one path
$Duplicates = $FilesBySHA1Sum.GetEnumerator() |Where-Object {$_.Value.Count -gt 1}

$Duplicates is now a list duplicate hashes and the associated file paths.

You could achieve the same or similar with Get-ChildItem |Group-Object { $_ |Get-FileHash |Select -Expand Hash }, but Group-Object in Windows PowerShell is exponentially slow when tracking the input elements, so might not the best fit for analyzing many files at once.

like image 63
Mathias R. Jessen Avatar answered Dec 30 '25 22:12

Mathias R. Jessen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!