8

The 'which' utility, when run with a parameter naming an executable, will tell you the first executable with that name it finds in your path, if found at all. This gives a good idea which version of the executable will be run. (Forgive me if this description is incomplete, but it conveys the general idea)

I'm looking for either a port of the 'which' utility, a Powershell command, or some other utility I'm not aware of that does the same thing.

I have looked at the following SO question (and will try the for loop logic in the selected answer). I'd prefer to have a single command that implements this functionality and want to see if that exists. If something like that doesn't exist, that logic would be fairly easily put into a script:

unix "which java" equivalent command on windows?


The "winwhich" utility on CodeProject exists. It hasn't been updated in 6 years or so and, when built on my Win 7 machine with VS 2010, crashed upon running. I plan to do my due diligence to find out why it crashed, but don't have time until tonight.


Has anybody used another utility or command on Windows to emulate this functionality?

Taylor Price
  • 622
  • 1
  • 8
  • 21
  • Did you try searching? http://stackoverflow.com/questions/63805/equivalent-of-nix-which-command-in-powershell – manojlds Dec 19 '11 at 20:40
  • 1
    @manojlds Yes I did and I didn't come across that answer, nor did I see it in the suggested questions when I wrote mine. I found that question once Novakov answered and I searched with his answer. – Taylor Price Dec 19 '11 at 20:43

3 Answers3

13

You can use Get-Command <command>, or shorten it to gcm.

Joey
  • 344,408
  • 85
  • 689
  • 683
Novakov
  • 3,055
  • 1
  • 16
  • 32
  • 1
    If you run: `(Get-Command ).Path` you can get the directory containing the command. i like this best: `Get-Command | Format-Table Path, Name` so i can get the containing directory as well. – jrsconfitto Nov 27 '12 at 15:15
7

where does the same on recent versions of Windows. If you're after a PowerShell command, Novakov's answer is correct.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • Thank you. I'd accept both your answer and Novakov's if I could since I did leave the question open to both answers. Upvoted. – Taylor Price Dec 19 '11 at 20:10
1

On Windows from Command Prompt (cmd)

cmd> where <command>

In Powershell (PS)

ps> get-command <command>
ps> where.exe <command>

You can also add alias to the 'which' command in PS

ps> New-Alias which get-command

and now you can use 'which' command like usual

ps> which <command>
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
  • The `where` utility seems to only search the system path. As I have a batch file add a folder to the path using `set`, `where` never searches that folder. – Leonardo Apr 21 '20 at 22:09