Note to self: How to programmatically get the MSBuild path in PowerShell

December 1, 2010

in .Net,.NET 4.0,MSBuild,PowerShell

Other ways I’ve seen have been to get the directory from the registry. I prefer this approach.

Function Get-MSBuild {
    $lib = [System.Runtime.InteropServices.RuntimeEnvironment]
    $rtd = $lib::GetRuntimeDirectory()
    Join-Path $rtd msbuild.exe
}
[Updated 9/12/2012 based on Matt's comment]

This gets the directory for the latest installed .NET runtime This gets the directory for the runtime environment under which Powershell is running and joins it with msbuild.exe.

Usage

& (Get-MSBuild)            

Microsoft (R) Build Engine Version 4.0.30319.1
[Microsoft .NET Framework, Version 4.0.30319.1]
Copyright (C) Microsoft Corporation 2007. All rights reserved.

MSBUILD : error MSB1003: Specify a project or solution file.
The current working directory does not contain a project or solution file.

{ 4 comments… read them below or add one }

Joel "Jaykul" Bennett 12.02.10 at 9:49 pm

I just put it in my PATH — I ought to just hardcode it in the environment variable, but instead I put this in my profile:

$Env:Path = [System.Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory() + “;$Env:Path”

Doug Finke 12.03.10 at 8:09 pm

Good point.
I am also delivering this function in some scripts for others to use. The group consists of VS 2008/2010 users.

Matt 09.11.12 at 10:54 am

This gets the directory for the latest installed .NET runtime – this is false. This gets the directory for the runtime environment under which Powershell is running. Powershell runs under the .NET 2.0 runtime, which means this method will not work if you need later versions of MSBuild (unless you force Powershell to load under a later version – see http://stackoverflow.com/questions/2094694/how-can-i-run-powershell-with-the-net-4-runtime)

Doug Finke 09.12.12 at 6:03 pm

Thanks for point that out Matt. I’ve updated the post.

Doug

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Contrat Creative Commons

© 2007-2013, Doug Finke