Jonathan
Jonathan

Reputation: 7541

How to find whether a certain file is available in the system path?

My Powershell script must be run from a VS command prompt, and I want to verity that by checking that msbuild.exe is in path. I managed to do it using where.exe:

where.exe msbuild.exe > $null
if ($LASTEXITCODE -ne 0)
{
    throw "BuildAll must be run from ""Developer Command Prompt for VS2012"" shortcut."
}

However, this doesn't feel like "the Powershell way" - is there a Powershell-native way to do this?

Upvotes: 2

Views: 98

Answers (1)

CB.
CB.

Reputation: 60910

Try this if the msbuild.exe must be in the same folder as the script is

   if ( TEST-PATH (JOIN-PATH (Split-Path -parent $MyInvocation.MyCommand.Definition) "msbuild.exe " ))
{
  ...
}
else
{
  ...
}

If you want check if build.exe is available as command (any available path from $env:path) you can do:

if ([bool](Get-Command "msbuild.exe" -ea silentlycontinue)) 
{ 
  ...
}

Upvotes: 1

Related Questions