Skip to content

Instantly share code, notes, and snippets.

@kiwi-cam
Last active July 15, 2019 01:17
Show Gist options
  • Save kiwi-cam/db74b4c7ca0078dcece8a7bbac49bea3 to your computer and use it in GitHub Desktop.
Save kiwi-cam/db74b4c7ca0078dcece8a7bbac49bea3 to your computer and use it in GitHub Desktop.
Single .ps1 to clean up files stored in Downloads. It removes items opens to view (e.g. JPG and PDF) and Clears Duplicates. Useful for Google Chrome users to clear files downloaded only to view - e.g. PDFs. Based on Winston McMiller Duplicate File Finder and Remover
<#
.SYNOPSIS
Finds and removes file duplicates from the Downloads folder
.DESCRIPTION
Single .ps1 to clean up files stored in Downloads. It removes items opens to view (e.g. JPG and PDF) and Clears Duplicates. Based on Winston McMiller Duplicate File Finder and Remover
.PARAMETER DownloadPath
Specifies the folder used to store Chrome's Downloads. Default: %USERPROFIKE%\Downloads
.PARAMETER ViewedFileTypes
An array of file types that should be treated as viewed files - files opened to view, not to store. These will be removed.
Default: "*.rdp", "*.tcx", "*.gpx", "*.kml", "*.qif", "*.pdf", "*.png", "*.jpg", "*.jpeg", "*.csv", "*.doc", "*.docx", "*.xls", "*.xlsx"
.INPUTS
None
.OUTPUTS
None
.EXAMPLE
.\Start-DownloadsCleanup.ps1 "D:\Downloads"
Run with a specified Download folder, e.g. D:\Downloads
.EXAMPLE
.\Start-DownloadsCleanup.ps1 -DownloadPath "D:\Downloads" -ViewedFileTypes @("*.pdf", "*.png", "*.jpg", "*.jpeg", "*.csv", "*.doc", "*.docx", "*.xls", "*.xlsx")
Run with a specified Download folder, and an Array of view-only file extensions
.LINK
Winston McMiller's original Script:
https://gallery.technet.microsoft.com/scriptcenter/Duplicate-File-Finder-and-78f40ae9
.NOTES
Version: 1.0
Author: Cameron McConnochie
Creation Date: 17 May 2018
Purpose/Change: Initial script development
Based on Winston McMiller Duplicate File Finder and Remover
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$False,Position=1)]
[string]$DownloadPath = "$($Env:USERPROFILE)\Downloads",
[Parameter(Mandatory=$False)]
[string[]]$ViewedFileTypes = @("*.rdp","*.tcx", "*.gpx", "*.kml", "*.qif", "*.pdf", "*.png", "*.jpg", "*.jpeg", "*.csv", "*.doc", "*.docx", "*.xls", "*.xlsx", "*.ics")
)
#Standardise the Path format
$DownloadPath = $DownloadPath.TrimEnd('\')
Write-Host "Clearing Downloads folder $($DownloadPath)" -ForegroundColor Blue
## Remove viewing files
# Array-ise this as a result of 1 item still needs to be processed (duplicates will never have a single result)
$ViewFiles = @(Get-ChildItem -path "$($DownloadPath)\*" -Include $ViewedFileTypes | Select-Object FullName, Length)
##Remove unique record by size (different size = different hash)
$RUBySize = Get-ChildItem -path $DownloadPath -Exclude $ViewedFileTypes | Where-Object {( ! $_.ISPScontainer)} | Group-Object Length | Where-Object {$_.Count -gt 1} | Select-Object -Expand Group | Select-Object FullName, Length
##Remove unique record by hash (generates SHA-1 hash and removes unique records)
$RUByHash = foreach ($i in $RUBySize) {Get-FileHash -Path $i.Fullname -Algorithm SHA1}
#Handle file dupicates
If (($RUByHash.count -gt 0) -or ($ViewFiles.Count -gt 0)) {
# Gather File stats
$ViewSavings = ($ViewFiles | Measure-Object -property length -sum).Sum
$DupSavings = ($RUBySize |select-Object -Skip 1 | Measure-Object -property length -sum).Sum
#Output GridView display
#$RUByHash|Out-GridView -Title "$Title1 Duplicate Files with a potential savings of $Savings bytes" -PassThru
Write-Host "View only files found:" -ForegroundColor Cyan
$ViewFiles | Format-Table
Write-Host "Duplicate files found:" -ForegroundColor Cyan
$RUByHash | Format-Table Path, Hash
Write-Host "Found data savings of $([math]::Round($ViewSavings/1024/1024,2))MB from viewing files and $([math]::Round($DupSavings/1024/1024,2))MB from Duplicates" -ForegroundColor Green
#Delete viewing files
Foreach ($file in $ViewFiles) {Remove-Item -Path $file.FullName -Force}
Write-Host "$($ViewFiles.Count) viewing files deleted"
#Delete duplicate files
$RUByHash|select-Object -Skip 1|Remove-Item
Write-Host "$($RUByHash.count) duplicate files deleted"
} else {
Write-Host "No files found that can be deleted"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment