181

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
1

3 Answers 3

315

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.

8
  • 2
    +1 ! I never knew this could have been a part of Windows and so didn't look in that direction! :)
    – Zabba
    Commented Oct 23, 2010 at 6:45
  • 1
    Any equivalent for the poor xp users?
    – Dawn
    Commented Apr 28, 2011 at 12:23
  • 1
    @Kenny: Assuming you know the name of the DLL, you can use the ListDLLs utility (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. Commented Nov 18, 2015 at 23:37
  • 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. Commented Nov 19, 2015 at 16:12
  • 1
    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
    Commented Dec 2, 2020 at 9:37
26

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

2
  • (get-command npm).Source will respond with just the path to npm (for the example) instead of the whole table of all the properties. Commented Apr 23, 2019 at 20:48
  • I was looking for info on dir and Get-ChildItem and where is not helpful for those. This works though!
    – Akaisteph7
    Commented Aug 7, 2023 at 15:10
11

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.