0

I am trying to determine the path to the executable of git. Since there might be multiple executables found via PATH directories, I want to restrict the result to one path.

When using the following construct, PowerShell sets $LastExitCode to -1, although an executable was found.

$git_path = "$(where.exe git | select-object -first 1)"

I am wondering why this exit code is set?

Paolo
  • 21,270
  • 6
  • 38
  • 69
René Nyffenegger
  • 39,402
  • 33
  • 158
  • 293

1 Answers1

4

Select -First <int> works by collecting the requisite amount of input (in your case just 1 string) - after which point it throws a special exception that forces the runtime to interrupt/stop all upstream commands - in the case of an external application like where.exe, that means forcefully closing it.

So -1 is not a real exit code, it's simply the "unknown exit code" value PowerShell uses to indicate that "we'll never know, because I killed'em".

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206