How to calculate and insert SHA in filename in Powershell?

Viewed 247

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://imgur.com/a/FJ8v0SG

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.

Related