Compare folders and delete dublicates at sourch


I have an issue, where I have a folder with a large number of images,
These images will be reduced in size by a batch job and be saved to another location.

Now i would like to compare the filenames in both locations and then delete the source files if they are older then x number of days.

Have you done anything on this at all? Please post your script.

Just some pseudocode and hopefully it can help you to start

$a=ls c:\source
$b=ls c:\dest

foreach ( $c in $a) {


if ( test-path $destfile ){

$destfiletime=Get-ItemProperty $destfile
if($c.lastwritetime -lt ($destfiletime.AddDays(-10))){
  remove-item $sourcefile



#batch job and copy $c to the destination folder



I managed to solve part of my problem, but it seems like it only compares the first level folder in the directories and not the files. And so far i have not implemented the date check yet.

$source = Get-ChildItem Y:\sourcedir
$dest = Get-ChildItem V:\destinationdir

$matches = (Compare-Object -ReferenceObject $source  -DifferenceObject $dest -Property Name -ExcludeDifferent -IncludeEqual)

foreach ($file in $matches)
        write-host "Deleting file $File"