I’m trying to find duplicate files, hopefully no matter what the “text” filename (that’s less important, though), and found this code online:
Get-ChildItem -Path $source -File -Recurse |
Group-Object -Property Length |
Where-Object Count -gt 1 |
Select-Object -Expand Group |
Get-FileHash |
Group-Object -Property Hash |
Where-Object Count -gt 1 |
ForEach-Object {$_.group | Select-Object Path}
The output for this on a test file is;
Path
----
X:TestDupTest FIle 6.avi
X:TestDupFolder 2Test FIle 6.avi
X:TestDupTest FIle 5.txt
X:TestDupFolder 2Test FIle 5.txt
X:TestDupTest FIle 2.mp4
X:TestDupFolder 1Test FIle 2 (Different) MP4
X:TestDupFolder 1Test FIle 2.mp4
X:TestDupFolder 2Test FIle 2.mp4
X:TestDupFolder 3Test FIle 2.mp4
X:TestDupTest FIle 1.txt
X:TestDupFolder 1Test FIle 1.txt
X:TestDupFolder 2Test FIle 1.txt
X:TestDupFolder 3Test FIle 1.txt
X:TestDupFolder 1Test 6 & 7 are different than other folder.txt
X:TestDupFolder 1Test 6.txt
X:TestDupFolder 1Test 7.txt.txt
X:TestDupFolder 2Test 3 in this folder is a copy of Test 2.mp4 in this folder, Test 3 in other locations.txt
X:TestDupFolder 3Test 5 in this folder is empty, not the same as Test 5 in other locations.txt
X:TestDupFolder 3Test 6 is a copy of Test 4.MP$, not the same as Test 3 in other locations .txt
X:TestDupFolder 3Test FIle 5.txt
X:TestDupTest FIle 3.mp4
X:TestDupFolder 2Test FIle 3.mp4
X:TestDupFolder 3Test FIle 3.mp4
X:TestDupFolder 3Test FIle 6.avi
But, that’s almost useless. Yes, these might be duplicates, but there’s no way to easily distinguish which file is a duplicate of which (I purposefully designed the test files to use different names for the same content – that might have defeated the comparison, but I needed to know the limits of the command).
Is there a way to get a blank line between groups (blocks of which files are duplicates of each other)? I’d tried other code segments, most of which included a HASH key in the output. That listed the same files, but while clearly showing that they were grouped by HASH, there was still no obvious break between groupings.
Also, I’m getting an Access Denied error on each folder, and sub-folder, and yet the duplicate files are still reported.
Get-FileHash: .FindDupFiles-DiffExt.ps1:29:21
Line |
29 | Get-FileHash |
| ~~~~~~~~~~~~
| Access to the path 'X:TestDupFolder 3' is denied.
Path
----
X:TestDup...
The test file is not open anywhere else (I’ve rebooted and run the script without opening Explorer – same error). I don’t get an access denied error with other scripts, and I can’t find anything to explain why this particular segment encounters the error.
Thank you
NewbieBAT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.