Does the Windows Command Prompt search somewhere other than those locations specified by the PATH variable when launching application programs?
At first, I thought that cmd only looks for executables in the directories contained in the PATH variable, so I randomly picked an application - winword.exe (Microsoft Word) and tried to launch it from the command line:
The reason winword.exe
worked is that a registry key exists which defined the path to Microsoft Word (Winword.exe). A similar key exists for Firefox.exe and Chrome.exe if those applications are installed.
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
What I want to know is where exactly does the command prompt look for executables?
System PATH Variable, User PATH Variable, and the various keys within ..\App Paths
. I was able to confirm that Audacity does not create a key for itself when it's installed.
When the ShellExecuteEx function is called with the name of an executable file in its lpFile parameter, there are several places where the function looks for the file. We recommend registering your application in the App Paths registry subkey. Doing so avoids the need for applications to modify the system PATH environment variable.
- The current working directory.
- The Windows directory only (no subdirectories are searched).
- The Windows\System32 directory.
- Directories listed in the PATH environment variable.
- Recommended: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
Source: Application Registration
From the command prompt, if you just enter WinWord
it fails to run.
If you enter START WinWord
it runs.
The Start
command is key here.
When you try to execute a file through the start command, Command Prompt does not perform any searching. Instead, it passes the file name (and arguments) over to Windows itself (via the ShellExecuteEx API call), which must then search for the file's location. There are several places it searches in the following order:
The current working directory.
The
Windows
directory only (no subdirectories are searched).The
Windows\System32
directory.Directories listed in the
PATH
environment variable.Recommended:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
WinWord
is in that registry key. The key is there to keep PATH
from getting too long.
The program (when you specify its module name without drive/path in command prompt) in Windows command processor (CMD.EXE) can be started when found:
by PATH environment variable (both executable and its hardlink/softlink/shortcut with the same name)
by DOSKEY alias
by application path from
HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths
orHKCU\Software\Microsoft\Windows\CurrentVersion\App Paths
(when usingstart
command)
Using this knowledge (especially the last one) you can create your own aliases convenient for you. For example you can create HKCU\Software\Microsoft\Windows\CurrentVersion\App Paths\au.exe
with default value of C:\Program Files (x86)\Audacity\Audacity.exe
and start this application simply by typing start au
in command prompt.