MoveDuplicates.ps1

# Set the source directory and subdirectories $sourceDir = "C:\Sources $duplicatesSubdir = "C:\Sources\Duplicates" # Create the duplicates subdirectory if it doesn't exist if (!(Test-Path $duplicatesSubdir)) { New-Item -ItemType Directory -Path $duplicatesSubdir | Out-Null } # Get a list of files in the source directory $files = Get-ChildItem -Path $sourceDir -File # Create an empty hash table to store file hashes $fileHashes = @{} # Loop through each file and calculate its hash foreach ($file in $files) { $hashBytes = Get-FileHash -Path $file.FullName -Algorithm SHA256 $fileHash = $hashBytes.Hash # If the file hash is already in the hash table, move it to duplicates subdirectory if ($fileHashes.ContainsKey($fileHash)) { Move-Item -Path $file.FullName -Destination "$duplicatesSubdir\$file.Name" -Force Write-Host "Moved duplicate file: $($file.FullName)" } else { # Otherwise, add the hash and file path to the hash table $fileHashes.Add($fileHash, $file.FullName) } }
Find Files With Duplicate Content, Move to "Duplicates" Subdirectory (Powershell)

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.