Remove Desktop Clutter On Windows 7 With PowerShell

Friday, January 27, 2012 posted by CSch

All of us know the habit of using the desktop as temporary working directory for all kinds of tasks, afterwards leaving most of the files there thinking you might still need them the next day. The day you use them again however never comes and so they remain on your desktop, eventually filling it up to the bottom right corner.
But for lazy people, there are lazy solutions to delay the task of cleaning and tidying up your computer! Just create a folder called Desktop on your Desktop and stuff everything you don't need into it! Here comes a PowerShell script that does this for you (replace the path to the desktop with the one to yours):

cd C:\Users\howtoforge\Desktop
$path = "C:\Users\howtoforge\Desktop"
$index = 0
$run = 1
while ($run -eq 1) {
if (test-path ($path + "\Desktop_" + $index)) {
$index++
}
else {
mkdir ($path + "\Desktop_" + $index)
$run = 0
}
}
gci $path |
? {$_.Name -notlike "Desktop_*" -and $_.Name -notlike "meta.ps1"} |
Foreach-Object { move-item $_ -destination ($path + "\Desktop_" + $index)}

This script creates a numbered "Desktop_x" folder, takes all the stuff from your desktop except system folders such as the recycle bin, shortcuts and the things you exclude in the script and throws them all inside the created folder.
The only thing you might want to adjust are the items the script excludes - these are given in the line

? {$_.Name -notlike "Desktop_*" -and $_.Name -notlike "meta.ps1"} |

Up to now, the script excludes all the items whose names begin with Desktop_, which is because the script is not supposed to crap its own folders, and the "meta.ps1" file, which is the name I saved the script under on my desktop.
Copy the script into a notepad and save it as .ps1 file - if you get an error message about converting to a different character set, cancel the saving. This happens upon copying the script from the website to notepad - to solve the issue, look through the script in your notepad for double-quotes and replace those that look odd with new ones. You should now be able to save the document without problems.
To add more files to exclude, copy and paste the bold part of the line (including the space character in front of it) right after itself and replace the name (which is meta.ps1) with the file you want to exclude (you may use wildcards (*) to select multiple files).
To run the script, right-click it and select Run with PowerShell.

Be Sociable, Share!



One Response to “Remove Desktop Clutter On Windows 7 With PowerShell”

  1. Brian says:

    Could you email me….I’m trying to get this script to work, it looks great! But the output is complaining that the script is missing a “terminatorexpectedatendofstring” and I’m a script newb so I don’t know how to parse through this.

    Thanks,
    Brian

Leave a Reply