Cleaning up Visual Studio project folders using PowerShell

Over the years I have created a lot of code samples and demo projects using Visual Studios. I keep many of these in a single folder which contains a several subfolders for organizing the code based on the technology being used. As you open, build and close Visual Studio projects a lot of additional files and folders get created. If you are used to creating .NET projects you are likely familiar with the bin and obj folders which contain the compile application. When zipping up projects to share with others it common to delete these folders as they aren’t needed and usually double the size of your zip file. There are several other folders that get created in Visual Studio depending on the type of project you have. JavaScript based Windows Store apps for instance create a bld folder. If you upgrade a Visual Studio project from an older version of Visual Studios you usually end up with a _UpgradeReport_Files folder. Over the years I’ve manually deleted these as needed, but when I was writing my book which consisted of over 35 Visual Studio projects it became a pain having to go through all these every time I wanted to share a copy of the code samples. So, after doing this a couple of times I decided to do something about it. Originally I created a batch file but that was bit painful to get working with all the subfolders. Looking around I had found some good PowerShell examples to do something like this. With a bit of tweaking to a few different scripts I managed to put the following one together.

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

When this script is run it will remove all bin, obj, bld, Backup, _UpgradeReport,_Files, Debug, Release, and ipch folders from the directory the script is in and all sub-directories. To use this script open up Notepad and copy and paste the script into the Notepad window. Once this is done, save the file as CleanVsProjects.ps1. Save this file to the main directory of where you store your Visual Studio projects. To run the script in Windows, simply right click on it and press the Run with PowerShell button. All these folders will be deleted.

Note, that if you create any folders that have the same folder names as those specified in this script, they will also be deleted. Also, if you find that there are some other folders you would like to delete simply add then to the comma delimited list of folder names.