Skip to content

Instantly share code, notes, and snippets.

@Badgerati
Created May 14, 2015 14:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Badgerati/78a3366dce87a25c1c8a to your computer and use it in GitHub Desktop.
Save Badgerati/78a3366dce87a25c1c8a to your computer and use it in GitHub Desktop.
PowerShell script for cleaning multiple projects/solutions in one command line via MSBuild
###########################################################################
# Author: Matthew Kelly (@Badgerati)
# Date: May 14 2015
#
# MSBuildPath: Path to where your MSBuild.exe file resides
# Projects: Array of Project/Solution files
# CleanDebug: Switch to clean build in debug mode
# CleanRelease: Switch to clean build int release mode
#
# Example:
# > .\CleanMSBuilds.ps1 -MSBuildPath path\to\msbuild.exe -Projects Example1.sln, Example2.sln -CleanDebug -CleanRelease
###########################################################################
param (
[Parameter(Mandatory=$true)]
[string] $MSBuildPath,
[Parameter(Mandatory=$true)]
[string[]] $Projects,
[switch] $CleanDebug,
[switch] $CleanRelease
)
function Clean-Build($project, $config) {
$command = "$MSBuildPath $config /t:Clean $project"
Write-Host "Executing: $command"
cmd /c "$command"
if (! $?) {
throw "Clean of '$project' failed."
}
}
if (!(Test-Path $MSBuildPath)) {
Write-Error "Cannot find MSBuild at '$MSBuildPath'"
}
foreach ($project in $Projects) {
if (!(Test-Path $project)) {
Write-Error "Cannot find project/solution at '$project'"
}
}
Write-Host "Cleaning Projects/Solutions:"
foreach ($project in $Projects) {
Write-Host " - $project"
}
foreach ($project in $Projects) {
if ($CleanDebug.IsPresent) {
Clean-Build $project '/p:Configuration=Debug'
}
if ($CleanRelease.IsPresent) {
Clean-Build $project '/p:Configuration=Release'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment