What happens when you set the Path environment variable – Part 1
This article was originally published for www.prowareness.com and could be located at http://www.prowareness.com/blog/what-happens-when-you-set-the-path-environment-variable-part-1/.
When you want to update the Path environment variable, there are a handful ways to do it: via the System Properties GUI; via the registry; via the command prompt; via the Windows APIs. However you do, whenever you set or update the Path variable, there’s a couple of checks that happen and a couple of background messages that are required to be broadcasted to all processes running on the computer, indicating the change. Let’s review our options one by one.
1. The recommended way is to use the System Properties GUI
You could do a Ctrl + Break, or you type sysdm.cpl (Control Panel Applet) on the Run dialog box, or you type Advanced System Settings on the Start Menu’s search box. Finally you open the System Properties dialog box. Then, Advanced –> Environment Variables –> System Variables –> Path
Now, when you try to edit it, the regular way, you wouldn’t have any problems. All the directory paths you put in separated by a semi colon (;) would work just fine.
And whenever you had to execute a program without typing the full path, the operating system or the command shell would search the list of directories specified in the Path environment variable and would invoke that program for you. But let’s say you are someone that has lot of development tools installed on your computer, and you wanted them all to be accessible via the Run dialog box or the command prompt (and do not want to see the message “……” is not recognized as an internal or external command, operable program or batch file) , then you might find yourself running in to a limit of 2047 chars, on the Edit System Variable dialog. Although there is a theoretical limit of 32767 chars, you can’t practically hit that limit with the Path variable because, there are other system environment variables that share this 32767 space – which we call a Environment block. Read more here.
There are some workarounds that you could use, that I will discuss later in Part 2 of the article.
Beside the 2047 chars check, the Edit System Variable dialog box does not really do any other fancy checks. It pretty much allows you to type anything from your keyboard and updates the registry key under the HKLM hive. And you are allowed to update the Path variable, just like that.
After updating the Path variable, at this point of time, if you already had a command prompt open (or any other process for that matter), the new Path variable you have just set via the Edit System Variable dialog will not be reflected till you restart the command prompt (or the processes). This is the expected behaviour and it is by design because, when a process is spawned it obtains a copy of all the environment variables from its parent (and hence the Path variables) and accesses them on every Read and Write request.
Sometimes, in order to ensure system integrity i.e. i.e. to have all running processes have the updated Path variable; whenever you change the path variable it is required to log off the user session and log in back again.
2. Updating Path via the registry key
This is a String Value named Path under the registry location
Open the registry location and edit it accordingly. The limit for this String Value is limited only by the maximum value that the registry editor allows you to enter for a String Value.
Updating the registry directly, requires a log off and log on for that particular user session. Unlike the Edit System Variable dialog, which did not enforce a restart of the user session (log off and log on), updating the registry key directly, does. So when you update the Path variable via the registry directly, and restart all your programs, and command prompt windows, and batch files, you would not see the updated Path value unless you restart the user session by a log off and log on or a machine reboot. The screenshot below shows the Edit System Variable dialog and the registry string showing the same value, however a new instance of command prompt still referring to an old value.
As it is evident from the registry path, there is no separate Path variable (or any other environment variable) for the 64 bit processes and the 32 bit processes (Wow6432Node) on a x64 based Windows.
3. Updating Path via the Command Prompt
Setx command makes permanent changes to any environment variables. The below command updates the Path variable just like the Edit System Variable dialog, but this could be very useful when writing batch files.
In case, you do not want to make permanent changes to the Path variable, however for a particular instance of the command prompt or for a single batch file you want some values added to the Path variable (only during the command prompt’s lifetime), then you could use the regular SET command as below:
This SET only updates the Path variable for the current session, i.e. the current command window. And when you shut down that command prompt window, the changes you made to the Path variable will be lost, as desired.
The follow up article Part 2 will focus on updating the Path variable via the APIs, broadcasting windows settings change message to the operating system, and will discuss a few workarounds to overcome the size limit of 2047.
Disclaimer: All these facts are explored based on tests ran on a Windows 7 x64 Enterprise. If you find any incorrect content, please let me know.