Windows 7 : Command-Line and Automation Tools - Setting Environment Variables

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
2/16/2013 8:32:49 PM

Using environment variables is one of the ways that Windows communicates information such as the location of system files and folders—as set up on your particular computer—to programs. Environment variables indicate where temporary files are stored, what folders contain Windows program files, and other settings that affect program operation and system performance. In addition, they can be used in batch files to temporarily hold information about the job at hand.

In Windows 7, the initial environment variables that are defined when every Command Prompt window is first opened are set up using the GUI shown in Figure 1.

Figure 1. Examining the environment variables for the current user (top) and for all users of the system (bottom). The per-user list adds to or overrides the systemwide list.

Notice that this dialog box has two sections, System Variables and User Variables. The lower, System Variables section defines the environment variables set up for every user account. The upper, User Variables section defines additional default environment variables just for the current user account. These add to or override the variables set up in the systemwide list.

To open this dialog box, if you are a computer administrator user, use either of these two methods:

  • If you have a Command Prompt window open, type the command start sysdm.cpl and press Enter. You might need to confirm a UAC prompt. Then, select the Advanced tab, and click the Environment Variables button.

  • Alternatively, click Start, right-click Computer, and select Properties. Select Advanced System Settings. You might need to confirm a UAC prompt. Then, click the Environment Variables button.

You can now edit both the upper User Variables (personal settings) and lower System Variables (systemwide settings) lists.

If you are not a computer administrator user, it’s a bit trickier. Use either of these two procedures:

  • To edit the systemwide settings, you can use either of the preceding methods, but you’ll have to supply an administrator password. Don’t change the upper (personal) part of the dialog box—you will be changing settings for the wrong account.

  • To edit your own personal environment variable list, you must use the following method: Click Start, Control Panel, User Accounts and Family Safety, User Accounts. In the task list at the left side, click Change My Environment Variables. You will be able to edit only the upper (personal) environment variable list.

After you have the dialog box open, you can create new variables, delete variables, or highlight and edit existing variables using the corresponding buttons.

If you need to alter a variable, you must understand what happens if there’s a conflict between environment variables defined in both the System Variables and User Variables lists. As a rule, Windows examines several locations for definitions, and the last definition seen wins. Windows sets variables from the following sources, in this order:

The systemwide variable list.

The personal variable list.
Set commands in autoexec.nt. (This applies only for MS-DOS or Windows 3.x applications.

Subsequent definitions issued by set commands typed in a Command Prompt window or encountered in a batch file. These changes apply only to that particular window and will disappear when the window is closed.

Setting the PATH Environment Variable

If you write batch files or scripts, it’s useful to put them into one folder, and to add this folder name to the PATH variable, so that you can run your batch files and scripts simply by typing their names.

Because mis-editing the PATH variable can prevent Windows from finding applications it needs to run, Windows gives the “personal” PATH definition special treatment:

  • For the PATH variable, the User Variables definition is added to the end of (appended to) the System Variables definition.

  • For all other environment variables, a User Variables definition overrides a System Variables definition.

In other words, you can enter your own personal folder(s) into the User Variables definition of PATH without worrying about copying or messing up the system definitions.

To create a folder for your own scripts and batch files, use one of these two procedures:

  • If you want to use the scripts and batch files only for your own use, create a folder, and put the full path to the folder into your “personal” PATH variable. For example, create a folder named c:\scripts.

    Then, add a PATH variable to the upper part of the Environment Variables dialog box (refer to Figure 1) with the value c:\scripts. If you need to add more than one folder to your personal PATH, put a semicolon (;) between each folder name.

  • If you want to create scripts and batch files that can be used by anyone who uses your computer, create a folder and be sure that its permissions are set so that all users can read it.

    For example, create a folder named c:\scripts. Right-click the folder, select Properties, and select the Security tab. If Users does not appear under Group or User Names, click Edit, then Add, and add “Users” to the list. Be sure that the Read & Execute permission setting is checked.

    Then, carefully edit the PATH variable in the lower part of the Environment Variables dialog box (shown in Figure 1). Add a semicolon (;) to the end of the existing text, and then add the folder name c:\scripts.

Your folder now will be part of the PATH when you open a new Command Prompt window.