Get all target names from an MsBuild script – with Powershell

by Oliver 31. July 2012 17:27

Developing our web application www.marinas.info using Orchard CMS, I often find myself at the command prompt to run different sets of build actions:

  • in place compilation
  • packaging for stage
  • deploying to a local app instance
  • running spec tests

Sometimes I have trouble remembering the correct target name, so today I set out to attack this simple problem (displaying the names of all target defined in our build script) using Powershell. I’ve long waited for a good occasion to get my hands on it – today was finally the day.

Following this inspiring walk-through by the Windows Scripting guy to parse an XML file, I made my first firm steps in Powershell, and after some guesswork I came up with this one-liner that omits the definition of a variable:

[xml](Get-Content C:\Projects\discoverize\Discoverize.proj) | Foreach-Object {$_.project.target} | % {$_.Name} | sort

It might help to know that % is an alias for the Foreach-Object cmdlet, and $_ references the item currently piped in.

I saved this into a file that from now on I can simply call to get an up-to-date list of build target names.

Comments are closed

About Oliver

shades-of-orange.com code blog logo I build web applications using ASP.NET and have a passion for javascript. Enjoy MVC 4 and Orchard CMS, and I do TDD whenever I can. I like clean code. Love to spend time with my wife and our children. My profile on Stack Exchange, a network of free, community-driven Q&A sites

About Anton

shades-of-orange.com code blog logo I'm a software developer at teamaton. I code in C# and work with MVC, Orchard, SpecFlow, Coypu and NHibernate. I enjoy beach volleyball, board games and Coke.