Delete Duplicate Images in the Folder


HI Everyone,

Often we end up having same image multiple times in the same folder. It is very hard to remove. Below script will help you in easing this task.

$Folder = “C:\Users\chaitanya\pictures\Testing”

$Folderobj =get-childitem -Path $Folder -Recurse |where { ! $_.PSIsContainer }

$FolderobjCount = $Folderobj.Count

$ImageObjArr = @();

$Folderobj| ForEach-Object {

$FolderobjCount–;

$ImageHash = $null

$ImageFileName = $_.Name

$ImageFilePath = $_.FullName

$ImageFileCreateDate = $_.CreationTime

$ImageHash = Get-FileHash -Path $ImageFilePath -Algorithm MD5

Write-Host “Creating Hash for Destination Image name $($ImageFileName) and Current Image Count is : $($FolderobjCount) ” -ForegroundColor cyan

if($ImageHash)

{

$ImageObjArr += [pscustomobject]@{ImageFileName=$ImageFileName;ImageFilePath=$ImageFilePath;ImageCreateDate=$ImageFileCreateDate; ImageFileHash=$($ImageHash).Hash;}

}

}

$DeletedImages = @();

$ImageObjArr | Group-Object -Property ImageFileHash | Where-Object {$_.count -gt ‘1’} | ForEach-Object -process{

$DuplicateImageObj = $_.group | Sort-Object -Property ImageCreateDate |select -Skip 1

if($DuplicateImageObj)

{

foreach($dup in $DuplicateImageObj)

{

Write-Host “Started Deleting the Duplicate Image name $($dup.ImageFileName) ” -ForegroundColor Yellow

$DeletedImages += [pscustomobject]@{ImageFileName=$dup.ImageFileName;ImageFilePath=$dup.ImageFilePath;}

get-childitem -Path $dup.ImageFilePath | Remove-Item -Force

Write-Host “Completed Deleting the Duplicate Image name $($dup.ImageFileName) ” -ForegroundColor Green

}

}

}

$DeletedImages |Out-GridView

Note: Please be careful while giving the folder that you want, it does recursive operation and search sub folders and delete the files too apart from Images.

Regards,

Chaitanya

Leave a Reply

Create your own website, free on WordPress.com.Create your own website.
Start free