Skip to content

Instantly share code, notes, and snippets.

@kamsar
Last active April 11, 2024 02:14
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save kamsar/0db8de55ebed0435cde5c6e1e3dfbd79 to your computer and use it in GitHub Desktop.
Save kamsar/0db8de55ebed0435cde5c6e1e3dfbd79 to your computer and use it in GitHub Desktop.
PowerShell to resolve MSBuild.exe on VS2017 or VS2015 (or with Build Tools 2015)
function Resolve-MsBuild {
$msb2017 = Resolve-Path "${env:ProgramFiles(x86)}\Microsoft Visual Studio\*\*\MSBuild\*\bin\msbuild.exe" -ErrorAction SilentlyContinue
if($msb2017) {
Write-Host "Found MSBuild 2017 (or later)."
Write-Host $msb2017
return $msb2017
}
$msBuild2015 = "${env:ProgramFiles(x86)}\MSBuild\14.0\bin\msbuild.exe"
if(-not (Test-Path $msBuild2015)) {
throw 'Could not find MSBuild 2015 or later.'
}
Write-Host "Found MSBuild 2015."
Write-Host $msBuild2015
return $msBuild2015
}
$msBuild = Resolve-MsBuild
# e.g. & $msBuild .\Foo.sln
@alan-null
Copy link

hey @kamsar

I would add
return $msb2017 | Select-Object -First 1

because it can return more than one result

λ  Resolve-Path "${env:ProgramFiles(x86)}\Microsoft Visual Studio\*\*\MSBuild\*\bin\msbuild.exe" -ErrorAction SilentlyContinue       
                                                                                                                                     
Path                                                                                                                                 
----                                                                                                                                 
C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\bin\MSBuild.exe                                          
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\bin\MSBuild.exe                                          

Updated fork

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment