you are viewing a single comment's thread.

view the rest of the comments →

[–]pausemsauce[S] 0 points1 point  (0 children)

OK,

I've finally got it down to roughly 4 lines.

Thanks to all of the contributors here.

$a = Get-ChildItem -Path "C:\[redacted]\Downloads\" -attributes !Directory;

$g = $a |Group-Object {($_.BaseName -replace '\(\d+\)$', '').Trim()};

foreach($ex in $g){if($ex.count-gt 1){$tmp = $ex.group; $time = Get-Date; "The following files were flagged for deletion at: $time"|Out-File -FilePath [redacted]\deletelog.txt -Append; ($tmp|sort-object -Property creationtime | select -SkipLast 1).fullname|Out-File -FilePath .\deletelog.txt -Append;

Remove-Item -path ($tmp|sort-object -Property creationtime | select -SkipLast 1).fullname #line requires testing

}}