$ cat "

Compiling Visual Studio Projects from PowerShell without Hard-coding the MSBuild Path


Msbuild makes it quite easy to compile .NET solutions or projects from the command line, but it is not available in the path by default, and the actual path to MSBuild is kind of unfriendly...

To compile a project from a PowerShell script without having to hard-code the path to MSBuild into the script, you can do the following:

Write-Host "Compiling project..."

$ProjectFilePath = "$PSScriptRoot\some\path\relative\to\script\directory.csproj"

# Get the actual MSBuild path from the registry
$MSBuildToolsPathRegistryValue = Get-ItemProperty `
    -Path "HKLM:\SOFTWARE\Microsoft\MSBuild\ToolsVersions\4.0"  `
    -Name "MsBuildToolsPath"
$MSBuildDirectory = $MSBuildToolsPathRegistryValue.MSBuildToolsPath

& "$MSBuildDirectory\msbuild.exe" $ProjectFilePath '/t:Clean;Rebuild' /p:Configuration=Release
Written by Erik Öjebo 2015-10-08 21:02