Skip to content

MsBuild cheat sheet

January 10, 2012

Target = the smallest unit of execution in msbuild

Examples use a target to print stuff to the console. Use msbuild myFile.proj /t:PrintStuff


These are your scalar variables. They behave in the same way as in imperative languages (C like ones at least).




<Target Name="PrintStuff">

<Message Text="The value of Foo is: $(Foo)" /> <!-- this is how you access the var -->


All properties and items are parsed before targets are executed.

Env variables can be called in the same way.

To edit a property, redefine it inside a task.


These are your arrays. They work best when holding file paths, since you get the * wildcard and the ** which means “all subdirectories”.


<MyFiles Include="pics/sunny.png;pics/rainy.png" />

<MyFiles Include="pics/others/car.png" />

<MyFiles Include="pics/**/*.png" />  <!-- use wildcards to define the same thing -->


<Target Name="PrintStuff">

MyFiles has: @(MyFiles) /> <!-- all 3 elements will be here -->


Include is like the Add method in C#. You can also use Remove to, well.., remove an element. Makes sense since you can use them inside targets.

Custom metadata

These are your objects with a twist…


<Person Include="Person1" >




<Person Include="Person2" >





<Target Name="PrintStuff" Outputs="%(Person.Identity)"> <!-- this is executed in batches, i.e. a for loop is at the top-->

<Message Text="We have these people: @(Person)" /> 
 <Message Text="Details: %(Person.Fname) is  %(Person.Age)" />

Msbuild does give you some metadata to play with. Again this is oriented towards files. Details here. You can see that Identity is the value specified in the Include attribute of the item.


<!-- define an item group named Src with some files and a property Dest for the destination dir -->
<Target Name="CopyFiles">
<Copy SourceFiles="@(Src)" DestinationFiles="@(Src->'$(Dest)%(Filename)%(Extension)')" />

The example is pretty self explanatory – the destination files is defined by transforming the Src item group. Filename and Extension are well-known metadata.

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: