MSBuild csproj - run Powershell script before rebuild

Petr Kostelanský | 16 November 2019
Sometimes you will need to run PowerShell or another script before the Rebuild process. To do that you can extend *.csproj file with Exec node and place your script call into Command property.

There is an *.csproj example of how to run Powershell script in BeforePrebuild state:

<Project Sdk="Microsoft.NET.Sdk">
  ...
  <Target Name="PreBuildCustomCommand" BeforeTargets="BeforeRebuild" Condition="'$(Configuration)'=='Debug'">
    <Exec Command="powershell.exe -command &quot;&amp; {.\MyScript.ps1 '$(SolutionDir)'}&quot;" />
  </Target>    
  ...
</Project>

Target - targets are used to divide build process into smaller units (more about targets)

Name - the name of the defined target

BeforeTragets - before which target run this target. There are other possibilities for how order targets run (like AfterTargets and others).

AfterTragets - after which target run this target

 

Exec - Exec task is one of the predefined MSBuild Tasks to run a program or command. (How to write own MSBuild Task)

 

If you would like to know more about MSBuild you can read about MSBuild concepts.

 

Loading ads...