Powershell Script to delete bin,obj,debug,backup… folder

It was really long time ago since my last post blog was published because I really had no time more for writing. I am really busy with my work and completely tired after 8 hours at company. So that is some information about me now. Today I would like to introduce a Powershell script to delete all bin,obj,… folders of your projects. Why do I need this script is a long story.
I code at home for my hobby and would like to back up my code in internet so that when something goes wrong with my hard disk I won’t lose anything. I use Syncplicity to synchronize all of my source code. This software is really good. It allows us to exclude folders which we do not want to synchronize. For example, the bin, obj… folder

Syncplicity exclude folders

We can also set the time span for synchronizing to determine how often the program should synchronize. It’s really comfortable. Last Thursday my hard disk stopped working and I lost all of my data on this hard disk. I replace a new one and get the data back from the Syncplicity Cloud and I found one problem that many bin,obj …. folders were stored in cloud too. When I let the program synchronize, he must download these folders and it lasts for ever. I forgot to exclude them from synchronized list and they were all uploaded to cloud. It’s really nonsense for making backup these folders and I do not want to exclude these folders manually each time I make new project.
So I decide to make a small script to delete all unnecessary folders. I can run this script whenever I want to clean up all of projects and Syncplicity will automatically remove these folders from cloud. This script is a Powershell script so you’ll need Windows Power Shell to run it. If you are using Windows 7, you do not need to download and install. Just type “power” in your search box you’ll see it in result list.
Windows Powershell

Start Powershell, browse to the top folder where contains all of your projects and paste this script into it.

Get-ChildItem .\ -include bin,obj,Backup,_UpgradeReport_Files,Debug,ipch -Recurse | foreach ($_) { remove-item $_.fullname -Force -Recurse }

Windows Power Shell

All of bin,obj… folder will be recursively deleted. To save this script for next time, at the top of your folder which contains all of your projects, create a “.ps1” file with Notepad. For example, “DelBinObj.ps1” and save the script to this file. In next time, just double click the file to run it. May be you will meet this error when running .ps1 file

“File D:\Synchronize\ToolTuCode\DelBinObj.ps1 cannot be loaded because the execution of sc
ripts is disabled on this system. Please see “get-help about_signing” for more details.
At line:0 char:0″

Enable execution policy

You just need to change the execution policy so that file can be executed.
– To change the Execution Policy to Unrestricted, type the following command in Powershell

    Set-ExecutionPolicy Unrestricted

– To change the Execution Policy to RemoteSigned (to run your own scripts), type the following command in Powershell

    Set-ExecutionPolicy RemoteSigned