Powershell search for any exe in environment path always returns msbuild.exe

205 views Asked by At

I am trying to use Cake as a build tool but am running into an issue in their powershell script.

The script is trying to find nuget.exe in the environment variable path. If it doesn't exist it downloads it.

The issue is that msbuild.exe is always returned and if nuget.exe does not exist the script fails as it tries to us msbuild.exe

$existingPaths = $Env:Path -Split ';' | Where-Object { (![string]::IsNullOrEmpty($_)) -and (Test-Path $_) }

$NUGET_EXE_IN_PATH = Get-ChildItem -Path $existingPaths -Filter "nuget.exe" | Select -First 1

No matter which exeI try to search for using this script even if it exists, msbuild.exe is always returned in the list.

1

There are 1 answers

4
Roman Kuzmin On BEST ANSWER

I would use a different and probably more effective check for nuget.exe availability

if (!(Get-Command nuget.exe -ErrorAction 0)) {
    # nuget.exe is not found, download ...
}

As Enrico Campidoglio suggested, you may add -CommandType Application. In theory, it should be even more efficient. In (my) practice, this is not always the case.