Modify the PATH Environment Variable in Windows

Thursday, 22 January 2009

You know what it's like when you need to be able to execute a particular command, perhaps one you've written yourself, from any Windows XP command window without having to specify the full path to the executable. The directory could be added to the PATH environment variable, but getting to it is such a pain in the arse:

Start > Run > sysdm.cpl [ENTER] > Advanced Tab > Environment Variables button > choose variable to edit > Edit button > Edit the path > OK > OK > OK

It's possible to add directories to the Windows XP PATH environment variable from the command line:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\> my_some_app_command -V
'my_some_app_command' is not recognized as an internal or external command, operable program or batch file

C:\> path=%PATH%;C:\Program Files\some_app

C:\> my_some_app_command -V
Version 1.0

C:\>

however this is a temporary change to the PATH environment variable and exists only in that command window and disappears once the window is closed.  This is because the path command only appends to a copy of the PATH environment variable (which was created when the command window was opened) and not to the original PATH.  When the window is closed the copy is destroyed along with any changes you made.

There is a nifty tool in the Windows XP Service Pack 2 Support Tools named setx (available even if you have service pack 3) which can quickly make permanent changes to environment variables from the command line.  The setx command modifies the actual PATH and not the copy in use by the currently open command window - so here I'll show you how to modify both in a couple of quick steps so that you can begin working with your new PATH in the current window whilst also permanently updating your PATH.

Once you've installed the support tools (accepting all the default options such as the install location and "Typical Install") they will be found in C:\Program Files\Support Tools.  Open a command window from any directory:

C:\> setx
'setx' is not recognized as an internal or external command, operable program or batch file.

The Support Tools directory is not in our path so lets add it temporarily so that we can use setx from here and at the same time, we'll add our some_app directory so that we can begin work:

C:\> path = %PATH%;C:\Program Files\Support Tools;C:\Program Files\some_app

These directories are now in the copy of the PATH environment variable in use by this command window.  Now let's permanently update the PATH:

C:\> setx path "%PATH%"

This has set PATH to the same value as the copy of PATH in use by this window and we're good to go in this or any future command windows.

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\> echo %PATH%
C:\WINDOWS;C:\WINDOWS\system32;C:\Program Files\Support Tools;C:\Program Files\some_app

C:\>