<# .SYNOPSIS Scans a directory and its subdirectories to find duplicate files based on content. .DESCRIPTION This script recursively searches through a specified directory, calculates the SHA256 hash for each file, and then groups files by their hash. It reports any groups of files that have identical hashes, as these are content-based duplicates. .EXAMPLE .\Find-Duplicates.ps1 (After setting the $targetDirectory variable inside the script) #> # --- CONFIGURATION --- # Set the directory you want to scan for duplicates. # IMPORTANT: Replace "C:\Path\To\Your\Directory" with the actual path. $targetDirectory = "C:\Path\To\Your\Directory" # --- SCRIPT --- # Check if the target directory exists if (-not (Test-Path -Path $targetDirectory -PathType Container)) { Write-Host "Error: The directory '$targetDirectory' does not exist." -ForegroundColor Red # Exit the script if the directory is not found return } Write-Host "Scanning for duplicate files in '$targetDirectory'..." -ForegroundColor Yellow Write-Host "This may take a while for large directories..." # 1. Get all files recursively from the target directory. # 2. Calculate the SHA256 hash for each file. # 3. Group the files by their calculated hash. # 4. Filter the groups to find those with more than one file (i.e., duplicates). $duplicateGroups = Get-ChildItem -Path $targetDirectory -Recurse -File | Get-FileHash -Algorithm SHA256 | Group-Object -Property Hash | Where-Object { $_.Count -gt 1 } if ($duplicateGroups) { Write-Host "`nFound duplicate files:" -ForegroundColor Green # Loop through each group of duplicates and display the information $duplicateGroups | ForEach-Object { Write-Host "`n--------------------------------------------------" Write-Host "The following files are identical (Hash: $($_.Name)):" -ForegroundColor Cyan # List all files within the duplicate group $_.Group | ForEach-Object { Write-Host " - $($_.Path)" } } Write-Host "`n--------------------------------------------------" Write-Host "Scan complete." -ForegroundColor Green } else { Write-Host "`nNo duplicate files found in '$targetDirectory'." -ForegroundColor Green }