I have a bad habit of saving files on the desktop. They are usually temporary files like exported CSVs or screenshots. Most of the time these are things that I only need once as input to a PowerShell script or as an attachment in an e-mail. I am however way to lazy to clean up my desktop and the result is that most often it looks like a complete mess.
I wanted a simple non-destructive way to clean my desktop without spending time on it. I created a simple script that I run whenever I feel the desktop is a bit on the cluttered side. The script will identify any files on the desktop that have not been accessed in the specified number of days. The files that are unused will be moved to an archive folder on the desktop containing a sub-folder with the date when the script was run.
#Variables
$Age = 14 #Time since last file access in days
#Set desktop path
$SourcePath = [Environment]::GetFolderPath("Desktop")
#Get all files
$Files = (Get-ChildItem -Path $SourcePath -File)
#Select files that have not been opened since the specified number of days
$OldFiles = $Files | where {$_.LastAccessTime -le ((Get-Date).AddDays(-$Age))}
#Process identified files, if any
if ($OldFiles){
$TargetPath = $SourcePath + "\Archive\" + (Get-Date -Format yyyy-MM-dd)
New-Item -Path $TargetPath -ItemType Directory -Verbose
#Move files
$OldFiles | Move-Item -Destination $TargetPath -Verbose
}
With this script I can clean my desktop in a matter of seconds without removing any data. Happy days!
This is the best thing since sliced bread!
Sure is! 🙂