Heckroth Industries

Explaining the PATH environment variable in Windows

Recently I have seen a large number of posts on forums asking how they run a windows console application that they have downloaded. The problem most of them encounter is that they can only run the application from the command line if they are in the same directory as the application they have just installed.

What is more worrying to me is the replies that people give to these posts suggesting some very silly things (e.g. moving their application into the c:\windows\system32\ directory). What a lot of the replies show is that there are a lot of people who don’t understand the what the PATH environment variable is.

So what is an environment variable then? An envrionment variable is a method of storing variable information so that processes can look it up. A good way to get an understanding of this is to actually use an environment variable. Open up a command line and enter echo %windir%. Notice the output. It is where your windows directory is located.

Now I hear you saying, “I know where my windows directory is, so whats the point.”, but lets look at it from the point of view of an installer. The installer wants to write a file into your windows directory, if we hard code the installer to always write it to c:\windows then the installer will fail when someone has a windows directory called c:\windowsxp or if they have their windows directory on a different drive (e.g. d:\windows). But if the installer uses the windir environment variable instead of hard coding it then it will work on those machines as well.

Now that we have an understanding of what an environment variable is lets look at the PATH environment variable. In your command line enter echo %PATH% and look at the long result it returns. At first this looks confusing but after looking at it for a little while we can see that it really is just a list of directories seperated by semicolons. So what is this environment variable used for?

When you enter a command on the command line the system uses the PATH environment variable as an ordered list of directories that it has to search for the command. So if your path environment variable is c:\windows\system32;c:\windowd;c:\ and you enter the command edit it will first look in c:\windows\system32\ for an executable called edit, it finds edit.com and runs it.

Now lets enter notepad on the command line. The system again looks in the first entry in the path environment variable (c:\windows\system32\ in our example) but it doesn’t find a match. It then looks in c:\windows where it finds notepad.exe and runs it.

Finaly lets enter asdf on the command line. The system again looks in the first entry in the path environment variable (c:\windows\system32) but fails to find a match. It then looks in c:\windows where it also fails to find a match. Finally it looks in c:\ and failing to find a match displays an error to the user.

Adding extra directories into your PATH environment variable is easy. Simply goto your system properties and under the advance tab click on Environment Variables. In the Environment Variables window that appears it is split into two sections User Variables and System Variables. User Variables are only for the current user logged in while System Variables are for all users.

It is important to notice that the PATH environment variable appears in both User Variables and System Variables. Windows uses both of these to create the PATH environment variable. The user’s PATH entry is concatenated onto the end of the system entry to produce a single environment variable. This lets the administrator define a default set of directories to be searched for all users and then individual users can add their own directories to be searched afterwards. The general rule I use is to add to the system PATH entry only if I need multipe users of the machine to have it in thier PATH otherwise I add directories to the user PATH entry.

Jason — 2009-12-04