Display or set a search path for executable files at the command line.
Syntax PATH path [;path] [;path] [;path]...[;%PATH%]] PATH PATH ; Key path A directory path to be added to %PATH% %PATH% Optionally include %PATH% at the beginning or end to avoid overwriting the current PATH definition. ; The command 'PATH ;' will clear the path
PATH without parameters will display the current path.
The %PATH% environment variable contains a list of folders. When a command is issued at the CMD prompt, the Operating System will first look for an executable file in the current folder, if not found it will scan %PATH% to find it.
Use the PATH command to display or change the list of folders stored in the %PATH% environment variable
The PATH environment variable uses semicolons: ; to separate directories. It will ignore spaces or commas.
You do not need to surround each part of the path with double quotes, PATH will automatically treat spaces as part of the filename.
A trailing backslash is accepted but is not required, each part of the PATH is always treated as a directory not a file.
PowerShell in particular will ignore any path node delimited by double quotes.
Early versions of Windows did not include a semicolon at the end of the path, but recent versions do.
Changes made using the PATH command are NOT permanent, they apply to the current CMD prompt only and remain only until the CMD window is closed.
To permanently change the PATH use Control Panel ➞ System ➞ Advanced System settings ➞ Environment Variables.
Alternatively, type environment variables into the Windows Search box.
The %PATH% variable is set as both a system and user variable, the two values are combined to give the PATH for the currently logged in user. Like several of the default environment variables it is constructed by combining Core+System+User environment variables.
[HKLM\System\CurrentControlSet\Control\Session Manager\Environment]
[HKCU\Environment]Be wary of using commands like SETX to modify the PATH, the User path can be edited, but the System path remains read-only for most users. If you try to delete an old value and add a new one it is very common for the 'delete' to fail and the 'add' to succeed, resulting in duplicate values being added to the path.
To programatically modify the PATH there are a few traps to consider:
Stray quotation marks or semi-colon delimiters in the current path, Duplicate entries in the user and system paths, also duplicates in either one (Windows does not check or warn about this), the maximum length of the system path (roughly 2 KB or just 7 paths of the maximum 260 characters), and lastly checking for any dependencies in other applications (if the path you are adding or removing could be used by them).To modify the path to add settings for a single application, one method is to use a second variable:
e.g.SetX MYAPP "C:\Program Files\My App" -m
Now include the new variable in the path like so ...C:\Windows\system32;%MYAPP%
You can now easily change that one variable %MYAPP% at any time in the future and the PATH will reflect the new value.
- Changing a variable in the Control Panel will not affect any CMD prompt that is already open, only new CMD prompts will get the new setting.
- To change a system variable you must have administrator rights
- If your system has an AUTOEXEC.BAT file then any PATH setting in AUTOEXEC.BAT will also be appended to the %PATH% environment variable. This is to provide compatibility with old installation routines which need to set the PATH. All other commands in AUTOEXEC.BAT are ignored.
If you start/run an application without a file extension (for example WinWord instead of WinWord.exe)then the PATHEXT environment variable will be read to determine which file extensions to search for and in what order.
The default value for the PATHEXT variable has changed over the years:
.COM;.EXE;.BAT;.CMD
to
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;,WSH;.MSC
This can be changed with SETX and many will argue that the legacy IE scripting options should be dropped as they are almost exclusively used for drive-by malware.
DPATH is an undocumented internal utility that allows the TYPE command to read data files in specified directories as if they were in in the current directory. On some OS’s this is also implemented as the now deprecated APPEND command. The list of directories is held in the %DPATH% environment variable which works just like the %PATH% variable, delimited with semicolons (not quotes). Syntax: DPATH pathname [;pathname]...
To type any file on the path:C:\batch\> type win.ini
The system cannot find the file specified.
C:\batch\> dpath %path%
C:\batch\> type win.ini
For a file stored as:
C:\Program Files\Windows Media Player\wmplayer.exe
So Drive + Path + Filename = Pathname.
If a file reference uses only the filename rather than a full pathname, then that will work to execute the file, only if the file is in the current directory or is listed in the PATH.
The App Paths registry subkey is available to provide a location for specific executable filenames.
This can be used to populate the 'Open With Dialog Box' and also avoids the need to modify the system PATH environment variable.The keys can be found in the following two registry locations, for the Current User (strongly recommended) or all users.
Full documentation of this is on docs.microsoft.comHKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\App Paths
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App PathsThe filename used in App Paths to launch the application does not have to match the actual destination file.
When CMD Command Extensions are enabled (the default):
If the PATH was successfully changed %ERRORLEVEL% = unchanged, typically this will be 0 but if a previous command set an errorlevel, that will be preserved (this is a bug).
If PATH could not be changed %ERRORLEVEL% = 1
PATH is an internal command.
To view each item in the path on a single line:
For %G in ("%path:;=" "%") do @echo:%~G
Or in a batch file:
For %%G in ("%path:;=" "%") do @echo:%%~G
Alternatively replacing the semicolons with ECHO: to generate a newline:
ECHO:%PATH:;=& ECHO:%
The above will fail in one case, if any of the paths include a semicolon [ ; ] which is normally only used to delimit items on the path.
A script to display each item in the path on a single line even if they include semicolons can be found here on stackoverflow.
Add 'My Application' to the end of the path, including the current %PATH% in the new setting:
PATH=%PATH%;C:\Program Files\My Application
Retrieve the path to the executable 7zip (if installed) from the registry and add it to the PATH for the current session:
For /F "tokens=1,2*" %%A in ('reg query HKLM\Software\7-Zip /v Path') do ( Set "PATH=%%C;%PATH%" )
“If you do not love your job, change it. Instead of pushing paper, push ideas. Instead of sitting down, stand up and be heard. Instead of complaining, contribute. don’t get stuck in a job description” ~ Microsoft job advert
SET - Display, set, or remove environment variables.
ePath - Edit the PATH environment System/User. (Bill Stewart).
Pathed - Edit the PATH environment System/User, part of the freeware gtools (Gerson Kurz).
PATHMAN - Edit the PATH environment System/User (old Resource Kit utility).
How to check if directory exists in %PATH%? - Stackoverflow.com
Dynamic-Link Library Search Order - docs.microsoft.com
Equivalent PowerShell: DIR Env: or "$Env:path" To add an item to the path for the current session: $env:path += ";C:\demo"
Equivalent bash command (Linux): env - Display, set, or remove environment variables - PATH/CDPATH/MAILPATH