How to run a command before or after a build in Visual Studio

Contents

Visual study.

Visual Studio has a useful function to run commands before or after compilations. This can be used to automate part of your development procedure, how to check in dependencies during the pre-build stage or push build artifacts to development environments.

Pre-build and post-build command settings

From Visual Studio, right click the project and click “Properties (edit)”.

  Visual Studio, right click on the project, click on "Properties (edit)".

Subsequently, on “Build events”, you will find the controls to configure the commands that are executed before and after compilation. This is a default CMD flag, so if you want to run Powershell or Bash commands, you will need to start Powershell or WSL respectively and pass it in a script.

Note that this command runs in the build artifacts directory. This may vary depending on your configuration, but in general it is project/bin/ProfileName/. You will need to add relative references to run commands in upper subdirectories.

Running pre and post-build events.

On the bottom, you can set some controls for post build command. If you don't intend to do anything with a failed build, set it to “In successful compilation”, and if you don't intend to send duplicate builds, set it to “When the build is updated”.

If you click “Edit post-compilation”, you will see a window listing all the variables that you can pass to your script.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.