Managing NT Environment Variables

Use environment variables to make your scripts more powerful

Questions about environment variables are frequent on newsgroups as Windows NT administrators attempt to access the information these variables store. You can use several techniques to set and read environment variables on NT machines. In this article, I explain how an experienced NT administrator can manage system and user variables with Microsoft Windows NT Server 4.0 Resource Kit utilities, batch files, and Visual Basic Script (VBScript) applications.

NT Environment Variables
Environment variables on an NT machine hold a wealth of useful information that administrators can access to make logon scripts and other scripts automate daily tasks. Environment variables let scripts make decisions based on the domain name, the active username, the machine name, the domain controller name that authenticated the user, and other relevant information.

NT has two kinds of environment variables: system and user. System variables are available any time the OS is running. You can break these variables down into dynamic and static system variables. The OS creates dynamic system variables as it starts. One example of a dynamic system variable is COMPUTERNAME. NT doesn't store dynamic variables in the Registry, as it does static variables. Examples of static system variables are ComSpec, OS, Os2LibPath, path, TEMP, TMP, and windir. The HKEY_LOCAL_MACHINE\SYSTEM\ CurrentControlSet\Control\ Session Manager\ Environment Registry key stores static system variables.

User variables are available only to a specific user. You can also break down user variables into dynamic and static variables. The OS creates dynamic user variables when the user logs on but before the logon script runs. All user variables are available for the logon script to process. Examples of dynamic user variables are LOGONSERVER, USERDOMAIN, USERNAME, and USERPROFILE. Examples of static user variables are path, TEMP, and TMP. The HKEY_CURRENT_USER\Environment Registry key stores static user variables. Although you can edit the Registry to manage environment variables, I discuss safer and more convenient methods.

Setting Environment Variables
When you assign a value to an environment variable with the Set command, the value is effective only for that session. After the command prompt or batch file that sets the variable terminates, the value is no longer available. To create a persistent environment variable, you must set the value in the Registry as a system or user variable using NT or the resource kit.

The simplest method of setting environment variables is through the System applet in Control Panel. Screen 1 shows the two lists under the Environment tab: System Variables and User Variables. To change the value of an existing variable, select the variable from the corresponding list and modify the value in the Value field. You use the following steps to create a new variable:

  1. Click the System Variables or User Variables list to select the type of variable you want to create. The variable list that you click matters, not the individual variable.
  2. Replace the Variable field contents with the new variable name that you want to create.
  3. Replace the Value field's contents with the value that you want to assign to the new variable.
  4. Click Set.

The variable list that you selected will list your new variable.

If you want to use a batch file to assign values to variables, use the Setx resource kit utility. Setx is a powerful tool with which you can manage the environment variables in NT. This utility has three modes of operation: command line, Registry, and file. The command-line and Registry modes are the most common.

In the command-line mode, you specify the environment variable and the value you want to assign. Specifying the -m option assigns the value to a system variable. Omitting the -m option assigns the value to a user variable. Suppose you want to set the LOCATION system variable to DALLAS, and you want all users logged on to the machine to have access to this variable. To set these preferences, use the command

setx LOCATION DALLAS -m

Next, set the INITIALS variable to CK for the active user. Omit the -m option to configure this as a user variable. You can't specify the username, so you must set the variable for the active user. To set these preferences use the command

setx INITIALS CK

The -k option specifies the Registry mode of Setx, which reads a value from the Registry and assigns the value to an environment variable. Suppose you need to know what time zone the machine is in to determine how a script proceeds. The Registry stores the time zone information. The following command retrieves this information and assigns it to the TimeZone system variable:

setx TimeZone -k HKEY_LOCAL_MACHINE\ SYSTEM\CurrentControl Set\Control\TimeZoneInformation\StandardName -m

Be sure to enter the entire command on one line. After you create the TimeZone system variable, a batch file or VBScript will use the %TimeZone% environment variable to determine the time zone.

The changes won't take effect in the session in which you run the Setx command. If you run Setx at a prompt, you'll need to open a new command prompt to see the changes. If you run Setx in a batch file, the changes Setx makes aren't available in the same batch file.

Reading Environment Variables
Batch files easily access variable names specified between two percent signs. The batch file replaces the variable name and the two percent signs with the variable value before the batch file carries out the command line. For example, you can enter the following command to make a batch file display the active username:

echo The active user is %USERNAME%

If Bsmith is the logged-on user, the command will produce the output The active user is Bsmith.

Listing 1 shows how to make the batch file base decisions on the domain name. In Listing 1, the If statements compare the USERDOMAIN environment variable value with HR and ACCT. If the value of USERDOMAIN matches either string, the batch file carries out the respective Goto statement. The Goto statement carries out the batch file from the appropriate heading.

In addition, you can write scripts with VBScript to read NT environment variables. Suppose you write a script that needs to read the machine name to present the name to the user. In Listing 2, the VBScript example shows how to retrieve the machine name from the COMPUTERNAME environment variable. In Listing 2, the Dim statements initialize the Shell and CompName variables. The third line defines the Shell variable as a WScript application object. The fourth line assigns the COMPUTERNAME environment variable to CompName, using the Shell object's ExpandEnvironmentStrings method. Finally, line five uses the MsgBox statement to present the result. In the same way, you can use the ExpandEnvironmentStrings method to read any environment variable your script needs.

No matter which scripting language you prefer, you can easily access environment variables to make all your scripts more powerful. The more powerful your scripts are, the less you have to do manually.

Discuss this Article 18

wzvh3c (not verified)
on Sep 22, 2004
Is there a way to set an environment variable by reading an input file? Example: rem Save path to temporary file echo %PATH% > tempdata rem Perform some commands that change PATH . . . rem Now I would like to restore the environment variable PATH from tempdata. rem How do I do it?
Anonymous User (not verified)
on Jan 30, 2005
I deleted the value of the path environment variable.how can i restore it
Anonymous User (not verified)
on Nov 4, 2004
* Gerry - set the variable(s) in a login script that all members of that domain will run. * Leslie - As Maor suggested, use the SET command to get a list of environment variables. You can SET > ENV.TXT to save them to a file called ENV.TXT. * wzvh3c - to set a variable equal to the first line in a file, do SET /P VAR_NAME=
Miguel (not verified)
on Jun 26, 2002
Michael, You can use the command SET without parameters to see all the environment variables.
Denis Marsh (not verified)
on Mar 27, 2002
How can I set an environment variable within a VB application that will persist while it is running?
Larry (not verified)
on Mar 18, 2004
how would i set a variable domain wide. Meanning can I set it on the server ?
Anonymous User (not verified)
on Jan 31, 2005
What are the effects of setting a UNC path in the PATH variable name? Does W2k attempt to verify the each value in the PATH on start up? If it does what are the performance implications of having 5-10 different servers in the PATH? Thanks J
Paul Fitzmaurice (not verified)
on May 15, 2003
Please don’t beat me up for asking but my boss would like us to research and recommend a testing approach on any impact of setting the user and system environment variables TEMP and TMP to the same location on NT4, W2K, and WXP workstations. I understand that you can change the structure of The NT environmental variables through an applet and everything is supposed to work fine. Why would someone go through all this trouble? Is there a way to trick Active Directory by doing this so that it runs in both modes? Any feedback would be appreciated.
michael schwartz (not verified)
on Mar 27, 2002
This is a useful article. However, it doesn't mention whether or not there is a command similar to "env" in the unix world so that one can view all the current env var settings from the command line. Is there such a command in nt?
Anonymous User (not verified)
on Nov 5, 2004
To the point and relevant!
leslie (not verified)
on Apr 21, 2004
the article is interesting but i was wondering if you could put the actual script on how to extract the system environment,

Please or Register to post comments.

IT/Dev Connections

Las Vegas
September 30th - October 4th

Paul ThurottYou'll have the opportunity to experience:
• The Microsoft
Technology Roadmap
• Office 365 Implementation
• Hyper-V Optimizing
• Windows 8 Deployment
and much more!

Come See Paul Thurrott & Rod Trent in Person!

Early Registration Now Open

Upcoming Training

Mastering System Center 2012

During over 6 hours of training you can join John Savill from your computer as he will walk you through the key components and capabilities of System Center 2012, what’s involved in using the components, and the benefit they can bring to your environment.

Register Now

Current Issue

May 2013 - The NameTranslate object is useful when you need to translate Active Directory object names between different formats, but it's awkward to use from PowerShell. Here's a PowerShell script that eliminates the awkwardness.

CURRENT ISSUE / ARCHIVE / SUBSCRIBE

Windows Forums

Get answers to questions, share tips, and engage with the Windows Community in our Forums.