MSBuild - Building projects and solutions from command line

MSBuild - Building projects and solutions from command line

In this post we are going to see how do we build our projects and solutions from command line. This post is a continuation of my previous posts -

  1. Introduction to MSBuild
  2. Customizing the MSBuild file

I was working in a project set up where we had multiple solutions to be build for different layers in the project like the Presentation Layer, Business Layer and Common Solution etc... There were a number of solutions and projects there were to be build. The developers
used Visual Studio to build projects. There machine configurations was not great and they had to open multiple instances of
VS2019 in order to build their projects. This led to their systems getting hanged and waste of time and efforts. This is the reason
we thought of why not building the projects from command line.
Building projects from command line would be faster and there would not be any need of opening up Visual Studio when you just want to
build some projects without the need of launching them.
As discussed in our previous posts that Visual Studio internally uses MSBuild.exe to build our projects hence we can use the same
MSBuild to build our projects in a set up where we do not have Visual Studio or we do not want to open Visual Studio.


MSBuild should be installed in the system where we are building the projects. If you have Visual Studio installed then MSBuild would have been automatically installed in your system.
Now we are going to set up the PATH of MSBuild so that we can run the msbuild command from command line from anywhere in our system, you can feel free to skip this section in case you already have the PATH of MSBuild set in the environment variables.

Setting up the PATH of MSBuild

Before we start building projects and solutions in our directory through command prompt we need to ensure that the PATH variable
has been updated to locate the MSBuild.exe from the command prompt.
In my case the PATH of MSBuild.exe for VS2019 installed in my system is "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin".
We need to set the above path in the Environment Variables->System Variables section to the PATH variable.
You can find the Environment Variables settings as mentioned below-

  1. Right Click the "This PC" and select properties.
  2. Click on the "Advanced System Settings" link on the right side of the "Settings" window.
  3. Click on Environment Variables button and locate the PATH variable in the System Variables section.
  4. Click on Edit button and add the new path as - "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin"

Verify if the MSBuild is installed successfully

Open the command prompt window in the directory that contains the project and type the below mentioned command.

msbuild --version

If the MSBuild is installed and the PATH is successfully set up then you would see the result in the command window similar
to the one mentioned below. If you are not able to see the version then you need to check if MSBuild is installed correctly and the PATH is set.

C:\Users\Sudhanshu Shekhar>msbuild --version
Microsoft (R) Build Engine version 17.1.0+ae57d105c for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

Now since the PATH is already set and the MSBuild is successfully verified from the command prompt we can start building our

Building the Project

As seen in our previous posts that the .csproj file in our project directory is actually an MSBuild file and MSBuild scans for .csproj file in the directory in which we have opened the command prompt.

Lets us build the project we have created in our previous post from command prompt by using the command.


This will build the project if you have opened the command prompt from the project directory. You can also build the
project from any other location by specifying the path of the .csproj file.

msbuild MSBuildDemoApp/MSBuildDemoApp.csproj

If you are running the command prompt from the set up where there are multiple project files specifying the .csproj
file name that you want to build would build the specific project.

If you want to build multiple projects that are not referenced by each other as project references, you can create a
BAT file and specify the commands to be executed in the BAT file. eg. If you want to build 2 projects App1.csproj and App2.csproj you can create a file as BuildMyApp.bat and write
the below mentioned commands in your bat file to build both the projects.

msbuild App1/App1.csproj
msbuild App2/App2.csproj


Running the above file by double clicking would build the projects in the sequence in which they have been mentioned
in the bat file.

Building the Solution

Most often we do not work with just a single project and we usually have a .sln file (Solution) that contains multiple
projects that we are working with. In such scenarios we can provide the path of the .sln file to the msbuild command.
The Solution file contains the path of the different projects (.csproj) and each of these individual .csproj file contains
the details of the project references. In cases where one project is referenced by another project msbuild first builds the
project on which other projects depend and then the build process continues building the projects mentioned in the
solution file. Lets us consider that the two projects which we have build from msbuild above are present inside a solution
named Demo.sln. The below mentioned command will build the projects App1.csproj and App2.csproj from the command line
using the solution file when it is executed from the folder containing the Domo.sln file.

msbuild Demo.sln

MSBuild Parameters

By default msbuild provides a huge bunch of details on the output window when we build a project on solution. We can provide lots of options from the console to customize the output we expect from the MSBuild. We can specify the target to be executed while building the project and we can also pass the values for different properties used by the projects if we want to. We can set the verbosity level to customize the level of details we want to see in our build result. We can also provide options to generate the log files for the output we want to see after the build is completed.
The structure of our build script present inside the BAT file can be from a very simple set of instructions to a very sophisticated one. This bat file can then be used for CI/CD configurations from the DevOps tools to completely automate our build and deployment process in the project.

Restoring NUGET packages

If is very common that our projects uses some NUGET packages which are restored when building the project. In these
scenarios just building the project with msbuild would fail because the NUGET packages required to build the projects
are not restored. Just like msbuild we can configure the PATH variable of the nuget.exe and use the below mentioned command to restore the
nuget packages before the msbuild command is executed.

nuget restore Demo.sln  

We are restoring the required nuget packages of the Demo Solution by the above mentioned command.
msbuild also provides command line parameters to restore nuget packages. You can always refer to the official documentation of
the msbuild console parameters to explore the wide range of options provided by msbuild.

The link to the office documentation for the MSBuild Command Line Reference is here .You can create your own target like the one we have created in Customizing the MSBuild file and provide the instruction to execute your target from the msbuild command line parameters.

Thank you for reading and see you in the next post !