PATH

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.

By default, Windows does not add a semicolon to the end of the path, but some program installers will incorrectly do so. This can lead to double semicolons appearing in the path list, creating a NULL node entry.

Permanent Changes

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 2 values are combined to give the PATH for the currently logged in user. This is explained in full by MS Product Support Article Q100843

[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.

PathExt

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 is: .COM;.EXE;.BAT;.CMD
Many would argue that .CMD should have higher priority than the legacy .BAT.

Dpath

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

Terminology - filename, path, pathname

For a file stored as:
C:\Program Files\Windows Media Player\wmplayer.exe

A pathname is Drive + Path + Filename

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.

Paths for GUI Windows applications

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.com

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\App Paths
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths

The filename used in App Paths to launch the application does not have to match the actual destination file.

Errorlevels

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.

Examples

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:%

Add 'My Application' to the end of the path, including the current %PATH% in the new setting:

PATH=%PATH%;C:\Program Files\My Application

Add 'My Application' to the start of the path, including the current %PATH% in the new setting:

PATH=C:\Program Files\My Application;%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

Related commands

SET - Display, set, or remove environment variables.
ePath - Add and remove individual directories from the system or current user path. (Bill Stewart).
PATHMAN - Resource Kit utility - modify system and user paths. Resolve duplicates. See Pathman.wri in the resource kit.
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


 
Copyright © 1999-2024 SS64.com
Some rights reserved