Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Build your Visual Studio Solution without "Opening" Visual Studio (You still need it installed)
#Place me in the same folder as the sln file
#Get the path of the sln file
Get-ChildItem -Path .\ -Filter *.sln -Recurse -File | ForEach-Object {
$solution = $_.FullName
}
#If you don't have Visual Studio Professional update the path to use a different edition i.e Community or Enterprise. Make sure MsBuild.exe is located there
cmd /c "`"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MsBuild.exe`" `"$solution`" /t:Restore"
cmd /c "`"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MsBuild.exe`" `"$solution`" /t:Rebuild"
@DBQC
Copy link

DBQC commented Aug 2, 2021

Noticed the paths for the MsBuild.exe are looking at 2017 installs of VS. I would suggest a possible improvement would be to use the new mswhere.exe to find the latest version of MsBuild on a machine:
https://github.com/Microsoft/vswhere/wiki/Find-MSBuild

This powershell will return the path of the msbuild which could be used as a variable in your script:
&"${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" -latest -prerelease -products * -requires Microsoft.Component.MSBuild -find MSBuild**\Bin\MSBuild.exe

For systems without VS installed, vswhere is a standalone exe that can be downloaded onto systems and run to find the latest MSBuild.exe if one exists.

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