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 }
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”
Good point.
I am also delivering this function in some scripts for others to use. The group consists of VS 2008/2010 users.
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)
Thanks for point that out Matt. I’ve updated the post.
Doug