PS: Find-DuplicatesFromHash

This script analyzes SHA hash files to find duplicates in a given folder structure.


Version History:

  • 1.0.0 - Initial release

Download the script:

>> Version 1.0.0 (current)
(MD5: 731e7e997c06c760e0d4472aa358d223)
(SHA1: 9aa8705c3a82429fce58446e5c1611c231228a1d)


Script code:

	
##------------------------------------------------------------------------------------------------
##
##  Find-DuplicatesFromHashes.ps1
##
##   Version 1.0.0
##
##
##   Copyright (c) 2016 Martin Mueller - www.sh-soft.com
##   
##   Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
##   and associated documentation files (the "Software"), to deal in the Software without 
##   restriction, including without limitation the rights to use, copy, modify, merge, publish, 
##   distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
##   Software is furnished to do so, subject to the following conditions:
##   
##   The above copyright notice and this permission notice shall be included in all copies or 
##   substantial portions of the Software.
##   
##   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
##   BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
##   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
##   DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
##   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
##   (The MIT License (MIT))
##
##------------------------------------------------------------------------------------------------

<#
.SYNOPSIS
This script analyzes SHA hash files to find duplicates in a given folder structure


.DESCRIPTION
This script analyzes SHA hash files to find duplicates in a given folder structure



.PARAMETER SearchPath

[required] The base path for the search operation




#>

#------------------------------------------------------------------------------------------------
# Parameter block
#------------------------------------------------------------------------------------------------
param ( 
	[Parameter(Mandatory=$true,HelpMessage='The base path for the search operation',Position=0)]
	[string]$SearchPath
)


##------------------------------------------------------------------------------------------------
##  main block...
##------------------------------------------------------------------------------------------------

PROCESS {
	Write-Host -ForegroundColor Green "`n`nSearching for hash files ..."
	$HashFiles = Get-ChildItem -Path "$SearchPath" -Filter *.sha* -Recurse | Select-Object -Property FullName, Hash
	Write-Host -ForegroundColor yellow "`n`t$($HashFiles.Count)" -NoNewline
	Write-Host -ForegroundColor Green " files found"


	if ($HashFiles.Count -gt 1) {
		Write-Host -ForegroundColor Green "`n`nReading hash file content ..."
		foreach ($Hash in $HashFiles) {
			$Hash.Hash = (Get-Content -Path $Hash.FullName).split(" ")[0]
		}

		Write-Host -ForegroundColor Green "`n`nComparing hashes ..."
		if ($HashFiles.Count -gt ($HashFiles | Select-Object -Property Hash -Unique | Measure-Object | Select-Object -ExpandProperty Count)) {
			Write-Host -ForegroundColor Red "`n`nDuplicates Found!`n`n"
			$HashFiles | Group-Object -Property Hash | Where-Object {$_.Count -gt 1} | ForEach-Object {
				Write-Host -ForegroundColor Red "Hash:`t$($_.Name)"
				foreach ($Name in $_.Group.FullName) {
					Write-Host "`t- $Name"
				}
			}
		}
		else {
			Write-Host -ForegroundColor Green "`n`nNo Duplicates found."
			Write-Host -ForegroundColor Yellow "`n`t$($HashFiles.Count)" -NoNewline
			Write-Host -ForegroundColor Green " unique files have been analyzed." -NoNewline
		}
	}
	else {
		Write-Host -ForegroundColor Green "`n`nNo Duplicates found ;-)"
	}
}

	

>> syntax highlighting powered by highlight.js