Clean Visual Studio solution using PowerShell

At development sometimes I need to clean Visual Studio solution and remove all builded files of all build configurations (Debug, Release and other custom). To do this task manually means a lot of clicking in Visual Studio UI. To automate this boring task I created following PowerShell script:

param
(
    [string] $solution = "MyProduct.sln"
)

Set-Alias msbuild "${env:ProgramFiles(x86)}\MSBuild\14.0\Bin\MsBuild.exe"

$configurations = @("Debug", "Release")

foreach ($configuration in $configurations)
{
    msbuild $solution /t:clean /p:configuration=$configuration /v:minimal | Out-Null
}

$directories = @("bin", "obj")

foreach ($directory in $directories)
{
    Get-ChildItem -Directory -Recurse | ? { $_.Name -eq $directory } | Remove-Item -Recurse -Confirm:$false
}


Udemy course: Improve your productivity with PowerShell

Script uses following cmdlets:

  • Set-Alias – creates alias for msbuild
  • Out-Null – hides the command output
  • Get-ChildItem – gets the child items (directories) in specified location
  • Remove-Item – removes the specified items (directories)

If you are interested in PowerShell automation, take my Udemy course Improve your productivity with PowerShell.

Leave a Reply

Your email address will not be published. Required fields are marked *