powershell automation, clean desktop

I love saving to desktop, but not the clutter it creates over time.  Saving to a specific folder isn't always needed and it's nice to have a little sandbox for temporary files. This PowerShell script moves everything into an “Archive” folder every day at 6 PM (or whenever) —automatically!


# Check if "Archive" folder exists on Desktop. If not, create it.
$desktopPath = [Environment]::GetFolderPath("Desktop")
$archivePath = Join-Path $desktopPath "Archive"
if (!(Test-Path $archivePath)) {
    New-Item -ItemType Directory -Path $archivePath
}

# Move all files (excluding the Recycle Bin and Archive folder) to the Archive folder
Get-ChildItem -Path $desktopPath | Where-Object { $_.Name -ne '$Recycle.Bin' -and $_.FullName -notlike "$archivePath\*" } | Move-Item -Destination $archivePath

# Create a scheduled task to run this script every day at 6 PM
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-ExecutionPolicy Bypass -File "$PSCommandPath""
$trigger = New-ScheduledTaskTrigger -Daily -At "6:00 PM"
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries
$task = New-ScheduledTask -Action $action -Trigger $trigger -Settings $settings
Register-ScheduledTask -TaskName "ArchiveDesktopFiles" -InputObject $task -Force

Set it and forget it—and enjoy a cleaner desktop without changing your habits.