Build your Visual Studio Solution without Opening Visual Studio

by on February 18, 2019

I have written a Powershell script in the Github gist below that will take your *.sln file and perform a Nuget Restore and a Rebuild of your project without having to open Visual Studio.

You can find it here.

YOU STILL NEED VISUAL STUDIO INSTALLED!

Why use it?

Lets say you are a front end developer that just doesn't want to deal with Visual Studio at all. It has too many features that you don't plan on using. With this Powershell script you can just execute it and that's it, the project is built.

So really all you need is this script and your favorite code editor for your front end development.

How it works?

First we get the path of the .sln file using a For Each loop to search through the files.

Get-ChildItem -Path .\ -Filter *.sln -Recurse -File | ForEach-Object { 
    $solution = $_.FullName 
}

We then need to trigger a nuget restore and a rebuild of the project. 

To do this we call up Command Prompt to run MsBuild.exe  which should be located in your Visual Studio install directory.

cmd /c "`"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MsBuild.exe`" `"$solution`" /t:Restore"

We then add on the path of the .sln file and invoke the Restore option of MsBuild. It's the restore option that starts the nuget restore.

We then start the Rebuild of the project using nearly all of the same syntax except we tell it to Rebuild instead of Restore, like so:

cmd /c "`"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MsBuild.exe`" `"$solution`" /t:Rebuild"

Can we go further?

Yes! Pair this up with my other script called iis-builder and you could do a lot more development without needing to open Visual Studio. IIS Builder basically automates your local IIS by building the sites for you, setting up bindings and local SSL for those bindings too.

You can find it here.

Additionally I wrote a blog post on Moriyama's site an Umbraco Gold Partner. Automate you local IIS Development Environment

Matt, do you hate Visual Studio?

No. But our front end developer did. They would much rather get down to writing their HTML, CSS and JS in their own editor rather than open Visual Studio.