Duplicate File Finder With Powershell!

 

here is a little script I was playing with to find duplicate files on your disk

 

here is the code

 

 

#Define the results path
$outputdirectory = $env:userprofile + "\Desktop"
$outputfile = "duplicatefileresults.txt"
$output = $outputdirectory + "\" + $outputfile

$testoutput = test-path $output
if ($testoutput -eq $true)
{
  remove-item $output
}

$erroractionpreference = "silentlycontinue"
cls
# Define the file types you can compare for on the system
[array]$filetypes = "DOC"
$filetypes += "XLS"
$filetypes += "PPT"
$filetypes += "PDF"
$filetypes += "EXE"
$filetypes += "DOCX"

write-host "Welcome To The File Compare Script" -fore green -back black
write-host "This script will help you located duplicate files on your system" -fore green -back black
write-host "`n"
write-host "Please select from the current file types" -fore green -back black
$count = 0
foreach ($filetyp in $filetypes)
{
    write-host $count ":`t" $filetyp -fore green -back black
    $count++
}

while ($fileselect.length -eq 0)
{
    $fileselect = read-host "Please Enter The File Type You Wish To Compare"
}
write-host "You have selected " $filetypes[$fileselect]

$filetypesearch = $filetypes[$fileselect]
 
$localdisk = get-wmiobject -class Win32_logicaldisk |where {$_.Description -eq "Local Fixed Disk"}

if ($localdisk.count -gt 1)
{
 
 write-host "We have detected more then one local disk on your system" -fore yellow -back black
 write-host "would you like to search all volumes" -fore yellow -back black
     $ans = $null
 while ($ans.length -eq 0)
     {
  $ans = Read-host "Please Press Y or N and press enter"
     }
       
        if ($ans -eq "Y")
 {
  $allvolumes = $true
        }
 else
 {
  $allvolumes = $false
  write-host "Please select from the following volumes on your computer" -fore yellow -back black
  $count = 0

  Foreach ($local in $localdisk)
  {
     write-host $count":`t" $local.deviceid
     $count++
  }
         $whichvol = $null
  while($whichvol.length -eq 0)
  {
  $whichvol = read-host "Please enter the number of the volume you wish to select and press enter"
  }
  
 }        

}

if ($allvolumes -eq $true)
{

  foreach ($local in $localdisk)
  {
 $searchdisk = $local.deviceid
 write-host "Searching Volume:`t" $searchdisk -fore green -back black
 $searchdisk = $searchdisk + "\"
 set-location $searchdisk

 $searchfiletype = "*." + $filetypesearch
 Write-host "Gather All Files of type: " $searchfiletype
 $allfiles = get-childitem $searchfiletype -recurse

 write-host "You have " $allfiles.count $filetypesearch " to compare"
 write-host "Finding Name Duplicates"
 cls
 $i=0
 foreach ($file in $allfiles)
 {
  $searchdisk = $local.deviceid
  $filename = (($file.name).split("."))[0]
  $dups = get-wmiobject CIM_Datafile -filter "Drive='$searchdisk' and FileName='$fileName' and extension='$filetypesearch'"
  
  if ($dups.count -gt 1)
  {
     #write-host "File " $file.fullname " Has a Duplicate"
     $dups |ft Name -wrap |out-file $output -append
     }
 write-progress -activity "Finding Duplicates..." -status "Percent Complete" -PercentComplete (($i / $allfiles.count) * 100) 
 $i++
        }
  }

}
elseif ($allvolumes -eq $false)
{

  $local = $localdisk[$whichvol]
  $searchdisk = $local.deviceid
  write-host "Searching Volume:`t" $searchdisk
  $searchdisk = $searchdisk + "\"
  set-location $searchdisk

  $searchfiletype = "*." + $filetypesearch
  Write-host "Gather All Files of type: " $searchfiletype
  $allfiles = get-childitem $searchfiletype -recurse

  write-host "You have " $allfiles.count " " $filetypesearch " to compare"
  write-host "Finding Name Duplicates"
 $i = 0
  cls 
  foreach ($file in $allfiles)
  {
 $searchdisk = $local.deviceid
 $filename = (($file.name).split("."))[0]
 $dups = get-wmiobject CIM_Datafile -filter "Drive='$searchdisk' and FileName='$fileName' and Extension='$filetypesearch'"
  
 if ($dups.count -gt 1)
 {
    #write-host "File " $file.fullname " Has a Duplicate"
    $dups |ft Name -wrap |out-file $output -append
 }
 write-progress -activity "Finding Duplicates..." -status "Percent Complete" -PercentComplete (($i / $allfiles.count) * 100) 
 $i++
 
  
   }
}

Comments

  • Anonymous
    June 21, 2012
    your title should be more clear,  this is a duplicate file "name" finder, not really a duplicate file finder.

  • Anonymous
    July 02, 2013
    Nice post. I also use "DuplicateFilesDeleter" alternative tool for finding and deleting clones.

  • Anonymous
    October 07, 2015
    I put together something super simple in 5 min, compares files without extensions (for media):

    $files = Get-ChildItem -Recurse -File

    foreach ($file in $files){
    if ((($files).BaseName -match ($file).BaseName).Count -gt 1){
    Write-Host ($file).FullName
    }
    }

  • Anonymous
    April 12, 2016
    I would recommend you to try Duplicate Files Deleter program.