PATH

You learned that you can run any executable in Command Prompt by: 1. changing your working directory, and 2. typing it’s name. But how are programs running when you type their name without changing the working directory? A good example of this feature is running Microsoft’s Notepad. You can run notepad without navigating to C:\Windows\System32 where notepad.exe really is.

Understanding the answer to this question is vital to saving you huge headaches in the future. The feature is called “PATH”, which you’ll now learn about.

Environment Variables

In Windows, every program running has an “environment”. A program’s environment comes with pieces of text called environment variables. You can see examples of these by running the SET command with no parameters. Run this command now and you’ll see variables for your username, your computer’s name, etc. Programs can access these values if the variable is inside their environment.

There are two types of environment variables:

  1. User environment variables (set for the current logged in user)
  2. System environment variables (set for every user)

By default, a program will inherit the variables of whatever program started it. The programs started when you first turn on your computer and log in will inherit both the system environment variables and then the user environment variables.

These variables are stored in something called “the registry” but the easy way to access them is using SET.

SET

The SET command allows you to make or change environment variables for the current command prompt window.

As stated above, you can see the environment variables that the current command prompt window has inherited by typing SET with no arguments. You can also see specific environment variables by specifying their name as an argument. For example, set userprofile will show you the path stored for other programs to know where your Documents, Music, etc are.

To make a new variable or change an old one, use SET [variable=[string]]. For example SET test=my test environment variable will add a new variable “test” to the list of variables you can see with SET.

Points to note:

  • This new environment variable is only known to that Command Prompt window. If you close Command Prompt and open it again, you will not see “test”.
  • While it is common practice to not have spaces in your environment variable names, you can specify spaces if you’d like.
  • Variable names are case insensitive. “TEST” is the same as “test”.
  • Using SET may be useful to you writing scripts in the future.

PATH

Now that you know about environment variables, we can talk about the environment variable PATH. I will write “PATH” in caps to mean the environment variable named “Path”, and not the generic word “path” for file/folder locations.

When you type SET or SET path you’ll see the PATH inherited value. It should be a long list of directories separated by semicolons ;. When you type the name of a program in Command Prompt, Windows will search all directories in your PATH variable for the program you’re trying to run. If it is found, then the program will run without you changing your working directory!

Notice C:\Windows\System32 is in your PATH by default. This is why you can run Microsoft’s Notepad from Command Prompt without changing your working directory.

Without PATH, you would either have to always change your working directory before running any program, or Windows would need to search every single folder in your file system (which could take a very long time). PATH is the compromise between the two extremes, limiting the search to a small number of directories.

Changing PATH Temporarily

To add a path to PATH without retyping everything already in PATH, you can use the Command Prompt variable syntax: %PATH%. Placing percent signs around an environment variable name will cause Command Prompt to look up the value and use the value instead.

For example, a quick way to change your working directory to your Documents folder is:

cd %userprofile%\documents

For me, the above is the same as typing cd C:\Users\Kristofer\Documents.

Therefore we can add directories to PATH with set path=%path%;DirectoryName. Some examples:

set path=%path%;C:\MyDownloadedPrograms
set path=%path%;C:\InstallDrivers1;C:\InstallDrivers2
set path=%path%;%userprofile%\documents

Try adding a directory to your PATH variable now. If you mess up, just close and reopen Command Prompt to get the default inherited environment back.

Changing PATH Forever

To change PATH permanently for every Command Prompt window you open in the future, follow these steps:

  1. Navigate to your System information page. You’ll see information on your version of Windows, processor, RAM, computer name, etc. There are several ways to do this:
    • Press the keyboard shortcut: Windows Logo Key + Pause/Break Key
    • Start -> right click “My Computer” or “This PC” -> choose “Properties”
    • Start -> Control Panel -> System and Security -> System
    • Enter the command “control system” in Command Prompt
  2. Click the “Change settings” link to open System Properties and click the Advanced tab.
  3. Click the “Environment Variables…” button near the bottom.

Now you can choose to modify either your user environment variables or system-wide variables. When any user on your computer opens Command Prompt (or any other program), first the system variables will be loaded, and then the user specific variables for that user. If you have (or create) a PATH variable for both the system environment and the user environment, then the user paths will be appended to the end of the system paths.

Think about whether you want other users running programs from the directory you want to add. If you add it to the system environment, then everyone will see the path in their Command Prompt window PATH variable. If you don’t want to affect any other user, then add/modify your user variable instead.


Prev ————————– Next

Leave a Reply

Your email address will not be published.