Subscribe to Windows IT Pro
June 01, 1999 12:00 AM

Managing NT Environment Variables

Windows IT Pro
InstantDoc ID #5379
Rating: (3)

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.

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

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