organize_downloads.ps1

<#PSScriptInfo
 
.VERSION 1.0.0
 
.GUID 4f8b92c1-3d7a-4e6b-8a19-2c5d7e9f0134
 
.AUTHOR waresoft
 
.COMPANYNAME
 
.COPYRIGHT (c) 2026 waresoft. All rights reserved.
 
.TAGS Downloads Organizer FileManagement Cleanup Automation Silent ScheduledTask
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
Initial release: Silently organizes the Downloads folder into categorized subfolders and purges stale files older than 30 days from Installers, Archives, and Virtual Disks with zero UI or console output.
 
.PRIVATEDATA
 
#>


<#
.SYNOPSIS
    Silently organizes the Downloads folder into categorized subfolders and purges old installer and archive files.
 
.DESCRIPTION
    Designed for scheduled, unattended background execution with no UI or console output. Automatically sorts files in the user's Downloads directory into structured category folders (Documents, Archives, Media, Installers, Virtual Disks, Scripts, Code & Data, and Fonts) based on file extension, then removes files older than a configurable retention period (default: 30 days) from clutter-heavy subfolders (Installers, Archives, and Virtual Disks). Locked or actively downloading files are skipped silently and handled on subsequent runs.
 
 
.PARAMETER Path
    Path to the Downloads folder to organize. Defaults to the current user's Downloads folder.
 
.PARAMETER RetentionDays
    Number of days to keep files in the Installers, Archives, and Virtual Disks folders before deletion. Defaults to 30.
 
.EXAMPLE
    .\Organize-Downloads.ps1
    Silently organizes the default Downloads folder and deletes files older than 30 days in Installers, Archives, and Virtual Disks.
 
.EXAMPLE
    .\Organize-Downloads.ps1 -Path "D:\Downloads" -RetentionDays 14
    Silently organizes a custom Downloads folder and removes stale files older than 14 days.
#>


[CmdletBinding()]
param(
    [Parameter()]
    [string]$Path,

    [Parameter()]
    [ValidateRange(1, 3650)]
    [int]$RetentionDays = 30
)

# Suppress all UI, prompts, progress bars, and console output streams
$ErrorActionPreference   = 'SilentlyContinue'
$WarningPreference       = 'SilentlyContinue'
$InformationPreference   = 'SilentlyContinue'
$VerbosePreference       = 'SilentlyContinue'
$ProgressPreference      = 'SilentlyContinue'
$ConfirmPreference       = 'None'

if ([string]::IsNullOrWhiteSpace($Path)) {
    $UserShellFolders = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders'
    $DownloadsGuid    = '{374DE290-123F-4565-9164-39C4925E467B}'
    $RegPath          = (Get-ItemProperty -LiteralPath $UserShellFolders -Name $DownloadsGuid -ErrorAction SilentlyContinue).$DownloadsGuid

    if (-not [string]::IsNullOrWhiteSpace($RegPath)) {
        $Path = [Environment]::ExpandEnvironmentVariables($RegPath)
    }

    if ([string]::IsNullOrWhiteSpace($Path)) {
        $Path = Join-Path -Path $HOME -ChildPath 'Downloads'
    }
}

if (-not (Test-Path -LiteralPath $Path)) {
    exit 0
}

$AgeLimit = (Get-Date).AddDays(-$RetentionDays)

$Categories = @{
    "Documents"     = @(".pdf", ".docx", ".doc", ".xlsx", ".xls", ".pptx", ".ppt", ".txt", ".csv", ".rtf", ".odt", ".ods", ".odp", ".gdoc", ".gsheet")
    "Archives"      = @(".zip", ".7z", ".rar", ".tar", ".gz", ".bz2", ".xz", ".cab")
    "Media"         = @(".jpg", ".jpeg", ".png", ".webp", ".gif", ".svg", ".bmp", ".mp4", ".mkv", ".mov", ".avi", ".webm", ".m4v", ".mp3", ".flac", ".wav", ".aac", ".ogg", ".m4a")
    "Installers"    = @(".exe", ".msi", ".msix", ".msu")
    "Virtual Disks" = @(".iso", ".vhdx", ".vhd", ".img", ".ova", ".ovf")
    "Scripts"       = @(".bat", ".cmd", ".ps1", ".psm1", ".psd1", ".vbs", ".py", ".sh", ".js", ".ts")
    "Code & Data"   = @(".json", ".xml", ".yaml", ".yml", ".sql", ".md", ".html", ".css", ".ini", ".cfg", ".conf")
    "Fonts"         = @(".ttf", ".otf", ".woff", ".woff2")
}

# 1. Silently sort files into category folders
Get-ChildItem -LiteralPath $Path -File | ForEach-Object {
    $File = $_
    $Ext  = $File.Extension.ToLowerInvariant()

    foreach ($Category in $Categories.Keys) {
        if ($Categories[$Category] -contains $Ext) {
            $TargetFolder = Join-Path -Path $Path -ChildPath $Category
            try {
                if (-not (Test-Path -LiteralPath $TargetFolder)) {
                    [void](New-Item -ItemType Directory -Path $TargetFolder -Force -ErrorAction Stop)
                }
                Move-Item -LiteralPath $File.FullName -Destination $TargetFolder -Force -Confirm:$false -ErrorAction Stop
            }
            catch {
                # Skip locked or actively downloading files silently
            }
            break
        }
    }
}

# 2. Silently purge stale files in clutter-heavy subfolders
$FoldersToPurge = @("Installers", "Archives", "Virtual Disks")

foreach ($FolderName in $FoldersToPurge) {
    $TargetFolder = Join-Path -Path $Path -ChildPath $FolderName
    if (Test-Path -LiteralPath $TargetFolder) {
        Get-ChildItem -LiteralPath $TargetFolder -File |
            Where-Object { $_.LastWriteTime -lt $AgeLimit } |
            ForEach-Object {
                try {
                    Remove-Item -LiteralPath $_.FullName -Force -Confirm:$false -ErrorAction Stop
                }
                catch {
                    # Skip locked files silently
                }
            }
    }
}

exit 0