How to Run a Command Before or After a Build in Visual Studio

Visual Studio.

Visual Studio has a handy feature for running commands before or after builds. This can be used to automate some of your development process, such as pulling in dependencies during the prebuild phase, or sending build artifacts to development environments.

Setting Up Prebuild and Post-build Commands

From Visual Studio, right click on the project, and click “Properties.”

 Visual Studio, right-click project, click "Properties."

Then, under “Build Events,” you’ll find the controls for setting the commands that run before and after the build. This is a default CMD prompt, so if you want to execute Powershell or Bash commands, you’ll need to launch Powershell or WSL respectively, and pass it in a script.

Note that this command is executed in the directory of the build artifacts. This can vary depending on your configuration, but it’s generally project/bin/ProfileName/. You’ll need to add relative references to run commands at higher subdirectories.

Running prebuild and postbuild events.

At the bottom, you can set some controls for the post-build command. If you don’t want to do anything with a failed build, set this to “On Successful Build,” and if you don’t want to send duplicate builds, set this to “When the build updates.”

If you click “Edit Post-Build,” you’ll see a window listing all the variables you can pass to your script.

 "Edit Post-Build" variables.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.