Powershell To Clean Visual Studio Bin/Obj folders

One of my old WPF Disciple buddies William Kempf read my last post, and alerted me to yet another useful PowerShell Command to clean all the BIN and OBJ folders of a Visual Studio solution.

Here is Williams version

gci -inc bin,obj -rec | rm -rec -force

In Williams own words

That wipes out all of the “bin” and “obj” directories in the current directory and every subdirectory. Super useful to run in your workspace directory to get to a “clean” state, especially when someone messes up and there’s something that a Clean or Rebuild inside the IDE doesn’t catch.

For those of you reading that may not know, PowerShell supports command aliases, here it is rewritten again not using the aliases

Get-ChildItem -inc bin,obj -rec | Remove-Item -rec -force

NOTE : You should have this stored in a PowerShell file and place that file at the root of your solution (where the .sln file resides), and then run it when you want a proper clean (not the micky mouse one that VisualStudio does, and reports success too).

If you want to know a commandlet alias you can use the following PowerShell commandlet to find the full commandlet name:

Get-Alias gci

Which when run for this example will give something like this:



2 thoughts on “Powershell To Clean Visual Studio Bin/Obj folders

  1. Yeah, when typing at the command line I abbreviate a ton, including aliases. To be pedantic, the “proper” way to write this would actually be:

    PS> Get-ChildItem -Include bin,obj -Recurse | Remove-Item -Recurse -Force

    I was leveraging both aliases and the PowerShell argument parser that allows you to type only the number of characters required to disambiguate it from other arguments. Both of these are wonderful at the command line, but in documentation (including blog posts) or in scripts you should fully spell things out. It’s clearer and safer (for instance, a user may repurpose an alias for something else and thus break your script, while shortened parameters may become ambiguous when new parameters are added to a command). My recommendation is to learn the shortcuts and use them liberally in interactive mode, but never use them in scripts.

    If you’re doing a series on PowerShell you should start with all of the things that will help a user to figure things out on their own. You started that here by pointing out Get-Alias. Get-Help (help) is a far more important thing to learn as a beginner. Also, Get-Member (gm) is nearly as useful for figuring things out as Get-Help is. Piping the result of a commandlet through Get-Member will reveal all sorts of information about the object(s) returned. Get-Command, Get-Module and a few others are handy to use while exploring as well. Knowing some of these can actually help you to learn about PowerShell far more quickly than anything else (blogs, books, etc.). They are also handy little things to use even when you know PoserShell in and out.

  2. While I like the readability of the PowerShell snippet above, the following cmd command does exactly the same in a fraction of the time:

    for /d /r . %%d in (bin,obj) do @if exist “%%d” rd /s/q “%%d”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s