144

Say I have a program X.EXE installed in folder c:\abcd\happy\ on the system. The folder is on the system path. Now suppose there is another program on the system that's also called X.EXE but is installed in folder c:\windows\.

Is it possible to quickly find out from the command line that if I type in X.EXE which of the two X.EXE's will get launched? (but without having to dir search or look at the process details in Task Manager).

Maybe some sort of in-built command, or some program out there that can do something like this? :

detect_program_path X.EXE
Zabba
  • 60,240
  • 44
  • 170
  • 200
  • 2
    Possible duplicate of [Is there an equivalent of 'which' on the Windows command line?](http://stackoverflow.com/questions/304319/is-there-an-equivalent-of-which-on-the-windows-command-line) – Michael Freidgeim Apr 06 '17 at 07:23

3 Answers3

257

Use the where command. The first result in the list is the one that will execute.

C:\> where notepad
C:\Windows\System32\notepad.exe
C:\Windows\notepad.exe

According to this blog post, where.exe is included with Windows Server 2003 and later, so this should just work with Vista, Win 7, et al.

On Linux, the equivalent is the which command, e.g. which ssh.

Callum Watkins
  • 2,444
  • 2
  • 27
  • 42
Chris Schmich
  • 27,532
  • 5
  • 71
  • 93
  • 2
    +1 ! I never knew this could have been a part of Windows and so didn't look in that direction! :) – Zabba Oct 23 '10 at 06:45
  • 1
    Any equivalent for the poor xp users? – shahar_m Apr 28 '11 at 12:23
  • @shahar_m: did you try the script below from Michael Burr? It's not built-in, but it might do what you need. – Chris Schmich Apr 28 '11 at 17:19
  • I have a program without executable, only dll (Excel Interop project). It is listed in Add/Remove program and _wmic product get name, version_ but not seen by _where_. How can I retrieve its path ? – Kenny Nov 18 '15 at 18:40
  • 1
    @Kenny: Assuming you know the name of the DLL, you can use the ListDLLs utility (https://technet.microsoft.com/en-us/sysinternals/bb896656). From the command-line, just run `listdlls -d foo.dll` to see all processes that have the module loaded and the full path to the loaded module. Alternatively, you can just do a Windows file search for the file name. – Chris Schmich Nov 18 '15 at 23:37
  • @Chris Schmich: Thanks for your reply. This mean we have to carry an extra dll in the package. Besides would it take a long time to do such an extensive search. The installed program is seen by Add/Remove program and known automatically when I apply patching (https://msdn.microsoft.com/en-us/library/aa370578(v=vs.85).aspx), so would there be another simpler way to retrieve its installed path ? – Kenny Nov 19 '15 at 10:11
  • 2
    @Kenny: What you are asking for is very different from the answer posted here. You should create a new Stack Overflow question with what research you've done and post a link to it in these comments. – Chris Schmich Nov 19 '15 at 16:12
  • In case you are executing it from Powershell, remember to use `where.exe` as `where` is alias for other Powershell command, thus will not execute what you want (output will be empty). – GrayCat Dec 02 '20 at 09:37
12

As the thread mentioned in the comment, get-command in powershell can also work it out. For example, you can type get-command npm and the output is as below:

enter image description here

Eugene
  • 8,507
  • 3
  • 35
  • 55
  • `(get-command npm).Source` will respond with just the path to npm (for the example) instead of the whole table of all the properties. – David Brown Apr 23 '19 at 20:48
10

Here's a little cmd script you can copy-n-paste into a file named something like where.cmd:

@echo off
rem - search for the given file in the directories specified by the path, and display the first match
rem
rem    The main ideas for this script were taken from Raymond Chen's blog:
rem
rem         http://blogs.msdn.com/b/oldnewthing/archive/2005/01/20/357225.asp
rem
rem
rem - it'll be nice to at some point extend this so it won't stop on the first match. That'll
rem     help diagnose situations with a conflict of some sort.
rem

setlocal

rem - search the current directory as well as those in the path
set PATHLIST=.;%PATH%
set EXTLIST=%PATHEXT%

if not "%EXTLIST%" == "" goto :extlist_ok
set EXTLIST=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
:extlist_ok

rem - first look for the file as given (not adding extensions)
for %%i in (%1) do if NOT "%%~$PATHLIST:i"=="" echo %%~$PATHLIST:i

rem - now look for the file adding extensions from the EXTLIST
for %%e in (%EXTLIST%) do @for %%i in (%1%%e) do if NOT "%%~$PATHLIST:i"=="" echo %%~$PATHLIST:i
Michael Burr
  • 311,791
  • 49
  • 497
  • 724