Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

June 01, 1999 12:00 AM

Managing NT Environment Variables

Windows IT Pro
InstantDoc ID #5379
Rating: (3)
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.

Related Content:

ARTICLE TOOLS

Comments
  • Anonymous User
    7 years ago
    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

  • Anonymous User
    7 years ago
    Jan 30, 2005

    I deleted the value of the path environment variable.how can i restore it

  • Anonymous User
    8 years ago
    Nov 05, 2004

    To the point and relevant!

  • Anonymous User
    8 years ago
    Nov 04, 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=* If you need any more help with variables or BAT files in general, e-mail me at blind-wig-at-yahoo-dot-com, without all the dashes, of course.

  • wzvh3c
    8 years ago
    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?

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.