Cleaning Document Folders

by Feb 27, 2009

Often, in your document folders a lot of files exist, and most of the time they are not really organized. With the help of a little PowerShell script, you can easily clean up and organize your files based on file type.

Warning: The following script will move file content into organized subfolders. Do this only on folders that contain documents. Never try and "clean up" the windows folder or a program folder or else programs will be damaged. Try at your own risk!

The script cleans up your documents folder. It will only clean up file types listed in the $types array. It creates a subfolder for each type listed in $types and then moves the files into that subfolder.

(Note: the folder $homeDocuments exists on Vista and above only. On XP, adjust the path accordingly)

$folder = "$homeDocuments"
$types = ".bmp", ".jpg", ".doc", ".ps1", ".ocx", ".pdf",
".docx", ".txt", ".vbs", ".xls", ".zip", ".htm", ".bat"

# get a list of files grouped by extension
$files= dir $folder | Where-Object { -not $_.PSisContainer } |
Group-Object Extension

# limit files to only the types listed in $types:
$files = $files | Where-Object { $types -contains $_.Name }

# create a subfolder for each type if necessary
$files | ForEach-Object { New-Item -itemType Directory -path `
"$folder$($_.Name)" -ea SilentlyContinue }

# move files into the appropriate subfolder
$files | ForEach-Object { $_.Group | Move-Item -destination `
"$folder$($_.Extension)$($_.Name)" }

To clean up your downloads folder, simply change $folder to $folder = "$homeDownloads".