6 April 2012

Windows Environment Variables



Environment Variables in Windows are a collection of dynamic named values that can control behavior of running processes in Windows operating system. They are not only specified for Windows but also you can find Environment Variables in MS-DOS and all UNIX systems. They are simply strings that hold information like drive or file name and path. An environment variable starts and ends with % sign. The best example for an environment variable is %TEMP% which determines the location for storing temporary files in Windows. You can use environment variables in Windows Command line or RUN line to access a particular Windows properties or location. These variables are also useful for making a script and mostly used by advanced users.
Tow most important characteristics of environment variables are they are local to process (change the value of one environment variable will not affect the others) and they inherit all the variables and their values from the parent process when they are created. When a function of a program calls another program or application, it makes a child process that inherits the environment and it substitutes itself with the called application. The environment variables are available to all programs that are running in Windows and even you can create your own user defined variables but the system environment variables can be only modified by system administrator. Some of the system environment variables need to restore every time the system reboots. The

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment




Windows registry key is in charge of creating these variables each time the PC starts. There are two types of variables in Windows, one is System environment variables and the other one is the Local environment variables. The system variables control the global Windows environment and local variables control the current order of Windows command prompt. Those variables that created by users (user defined) are placed in the registry key:

HKEY_CURRENT_USER\Environment 

How to create or modify an Environment Variable
Follow these steps to create or modify an Environment variable in Windows:

1. Right-click on My Computer icon and from the drop down menu select Properties.




2. In the System window, from the left panel select Advanced system settings.




3. When the System Properties opened, go to Advanced tab and hit the Environment Variables button.




4. Under the User variables and System variables box, you have three options, New, Edit and Delete. You can use these options to create a new variable or modify an existing variable. Use User variable to create a user defined Environment variable. Click on the New button to make a new one. In the New User Variable window, enter your variable name and its value and OK the windows.




The command line used in Windows Command Prompt for environment variables is the SET command. To check all available variables in your system, in command prompt window type SET and press the Enter key. This command will be displayed the current environment variable settings. You can use this command to create or set a new variable also. To know more about Windows command line you can read this article. there are some more commands that are available for environment variables, tow more commands that you can use is Setlocal and Setx. You can reference to these three commands in Windows command line reference:






Note: You should know the user environment variables are different for each user who uses the system. System variables can be found in the HKEY_LOCAL_MACHINE registry key and Local variables in the HKEY_CURRENT_USER registry key.

Windows Environment Variables Reference





Environment Variables Reference (397Kb)