Export (0) Print
Expand All

Environment Variables

Microsoft® Windows® 2000 Scripting Guide

Environment variables are a set of string values associated with a process. The Windows Shell process has a number of environment variables associated with it that contain useful information that you can use within your scripts, including:

  • Directories searched by the shell to locate programs (the path).

  • Number of processors, processor manufacturer, and processor architecture of the computer.

  • User profile location.

  • Temporary directory locations.

When a user logs on to Windows, the shell process starts and obtains its initial environment variables by loading both the computer-specific (system) and user-specific (user) environment variables from the registry.

In addition to the computer-specific and user-specific environment variables loaded from the registry, additional process environment variables are generated dynamically during each logon.

Table 3.12 lists a description of each type of environment variable and its location in the registry.

Table 3.12 Types of Environment Variables and Their Storage Locations

Type

Description

Registry Location

User

Applies to the user currently logged on to the computer and is saved between logoffs and restarts

HKCU\Environment

System

Applies to all users of the computer and is saved between logoffs and restarts

HKLM\System\CurrentControlSet\Control\Session Manager\Environment

Volatile

Applies to current logon session and is not saved between logoffs and restarts

HKCU\VolatileEnvironment

Process

Applies to current process and might be passed to child processes

Not stored in the registry

The Environment property of the WshShell object returns a WshEnvironment collection object that gives your scripts the ability to retrieve, create, and modify environment variables. The WshEnvironment collection provides access to all four types of environment variables: system, user, process, and volatile.

Retrieving Environment Variables

To retrieve a collection of environment variables of a specific type, your script must access the WshShell Environment property and provide a string parameter that represents the desired type of environment variable: system, user, process, or volatile. Your script can then use the resulting WshEnvironment collection to access the values of those environment variables by name.

The environment variables that can be retrieved using WSH are shown in Table 3.13.

Table 3.13 WSH Environment Variables

Name

System

User

Process

Process (Windows 98/Me only)

NUMBER_OF_PROCESSORS

 

Table Bullet

 

 

Table Bullet

 

 

PROCESSOR_ARCHITECTURE

 

Table Bullet

 

 

Table Bullet

 

 

PROCESSOR_IDENTIFIER

 

Table Bullet

 

 

Table Bullet

 

 

PROCESSOR_LEVEL

 

Table Bullet

 

 

Table Bullet

 

 

PROCESSOR_REVISION

 

Table Bullet

 

 

Table Bullet

 

 

OS

 

Table Bullet

 

 

Table Bullet

 

 

COMSPEC

 

Table Bullet

 

 

Table Bullet

 

Table Bullet

 

HOMEDRIVE

 

 

 

Table Bullet

 

 

HOMEPATH

 

 

 

Table Bullet

 

 

PATH

 

Table Bullet

 

Table Bullet

 

Table Bullet

 

Table Bullet

 

PATHEXT

 

Table Bullet

 

 

Table Bullet

 

 

PROMPT

 

 

 

Table Bullet

 

Table Bullet

 

SYSTEMDRIVE

 

 

 

Table Bullet

 

 

SYSTEMROOT

 

 

 

Table Bullet

 

 

WINDIR

 

Table Bullet

 

 

Table Bullet

 

Table Bullet

 

TEMP

 

 

Table Bullet

 

Table Bullet

 

Table Bullet

 

TMP

 

 

Table Bullet

 

Table Bullet

 

Table Bullet

 

The environment variables shown in the preceding table are present on all Windows computers. However, you might also have additional user-specific or computer-specific environment variables, which can also be accessed through a script. If you do not know the names of these variables, you can obtain a complete list of the variables (and their values) by typing set from the command prompt.

The script in Listing 3.22 retrieves both the user-specific and computer-specific PATH environment variables.

Listing 3.22 Displaying User-specific and Computer-specific PATH Environment Variables

  
1
2
3
4
5
6
7


Set objShell = WScript.CreateObject("WScript.Shell")
Set colSystemEnvVars = objShell.Environment("System")
Set colUserEnvVars = objShell.Environment("User")
Wscript.Echo "Computer-specific PATH Environment Variable"
Wscript.Echo colSystemEnvVars("PATH")
Wscript.Echo "User-specific PATH Environment Variable"
Wscript.Echo colUserEnvVars("PATH")

When the preceding script runs under CScript, output similar to the following appears in the command window:



Computer-specific PATH Environment Variable
%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\system32\WBEM;C:\Program Files\M
icrosoft.NET\FrameworkSDK\Bin\;C:\Program Files\Microsoft Visual Studio.NET\Vc7\
bin\;C:\Program Files\Microsoft Visual Studio.NET\Common7\IDE\;C:\WINNT\Microsof
t.NET\Framework\v1.0.2914\;C:\Program Files\Microsoft Visual Studio.NET\Vc7\bin\
;C:\Program Files\Microsoft Visual Studio.NET\Common7\IDE\; C:\MSSQL7\BINN;C:\Program Files\Support Tools\;C:\Program Files\
Resource Kit\C:\PROGRA~1\CA\Common\SCANEN~1;C:\PROGRA~1\CA\eTrust\ANTIVI~1
User-specific PATH Environment Variable
C:\Perl\bin;C:\Perl\bin;C:\Perl\bin\

Creating Environment Variables

To create a new environment variable, your script must start with the same first step required for retrieving the values of environment variables: It must obtain a reference to a collection of environment variables of one of the four types (user, system, process, or volatile).

After your script has a reference to the collection corresponding to the type of environment variable being created, it can then store a new string value in the corresponding collection location.

Storing the string value creates the new environment variable. The new index becomes the name of the new environment variable, and the corresponding string becomes its initial value. For example, this line of code creates an environment variable named MyVariable, with the initial value 0:



colUsrEnvVars("MyVariable") = 0

You can write scripts that create process or volatile environment variables, but neither of these types is saved between logons and reboots. You can also create a volatile type, which is stored in the registry and lasts for a logon session; this type of environment variable can be used as a mechanism for communicating information between two scripts that both run during a single logon session. However, you will likely find that creating system or user types is more useful because these environment variables are saved between logon sessions and computer reboots.

The script in Listing 3.23 creates a user environment variable named APP_VARIABLE and sets its initial value to "Installed." The script then retrieves the value of the new APP_VARIABLE environment variable to confirm that it was created.

Listing 3.23 Creating a User-specific Environment Variable

  
1
2
3
4


Set objShell = WScript.CreateObject("WScript.Shell")
Set colUsrEnvVars = objShell.Environment("USER")
colUsrEnvVars("APP_VARIABLE") = "Installed"
Wscript.Echo colUsrEnvVars("APP_VARIABLE")

Modifying Environment Variables

To modify an environment variable, your script must use steps similar to those used to create a new environment variable. It must obtain a reference to a collection of environment variables of one of the four types (user, system, process, or volatile) and store that reference in a variable.

After your script has a reference to the collection corresponding to the type of environment variable being modified, it can then reference the name of the environment variable to modify and store a string value in the corresponding collection location, overwriting the string previously located in that location.

The script in Listing 3.24 modifies the value of a user-specific environment variable named APP_VARIABLE by changing the value to Upgraded.

Listing 3.24 Modifying a User-specific Environment Variable

  
1
2
3
4
5


Set objShell = WScript.CreateObject("WScript.Shell")
Set colUsrEnvVars = objShell.Environment("USER")
strCurrentValue = colUsrEnvVars("APP_VARIABLE")
colUsrEnvVars("APP_VARIABLE") = "Upgraded"
Wscript.Echo colUsrEnvVars("APP_VARIABLE")

Expanding Environment Variables

When constructing environment variables or configuration strings to be used in the registry or elsewhere, you might want to incorporate the current value of an existing environment variable within those variables or strings. For example, if a script needs access to the temporary folder, you need to somehow indicate that, for this user on this computer, the temporary folder can be found in C:\Temp.

However, you would not want to hard-code the value of that environment variable in your configuration string, as it might change in the future and your script would no longer be valid. Although the temporary folder might be C:\Temp today, there is no reason why that cannot be changed to something else (for example, C:\Temporary Folder) tomorrow. Because of that, it is better to use an environment variable to dynamically retrieve the location of the temporary folder rather than hard-coding in the value and hoping that it never changes.

To refer to the value of environment variables within configuration strings, you must use the WshShell ExpandEnvironmentStrings method.

This method accepts, as a parameter, a string with an embedded environment variable name enclosed in percentage symbols (%) and returns a string in which the environment variable name and percentage symbols (%) have been replaced with the value of the corresponding environment variable.

The script in Listing 3.25 shows the difference between echoing the value of an environment variable and echoing the expanded value of an environment variable.

Listing 3.25 Creating and Displaying an Environment Variable That Incorporates Existing Environment Variables

  
1
2
3
4
5
6


Set objShell = WScript.CreateObject("WScript.Shell")
Set colEnvVars = objShell.Environment("User")
Wscript.Echo "Temporary folder (Unexpanded):"
Wscript.Echo colEnvVars("TEMP") & vbCrLf
Wscript.Echo "Temporary folder (Expanded)"
Wscript.Echo objShell.ExpandEnvironmentStrings("%TEMP%")

When run under Cscript, output similar to the following appears in the command window:



Temporary folder (Unexpanded):
%USERPROFILE%\Local Settings\Temp
Temporary folder (Expanded)
C:\DOCUME~1\kmyer\LOCALS~1\Temp

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft