Introduction
Environment variables are a set of dynamic values consisting of system information that can control the behavior of various programs. They consist of specially named aliases or placeholders for certain basic system properties that are present for convenience in programming and system administration. Automate encompasses a number of actions designed to automate the creation, manipulation and management of environment variables in a Windows-based operating system. These actions reside in the Task Builder’s Available Actions pane located in the Environment Variables folder.

This article provides important details regarding environment variables and describe some Automate sample tasks utilized to manipulate and manage Microsoft Windows environment variables. It will provide information about existing environment variable types and list various common predefined Microsoft Windows environment variables along with their values.  

Environment Variables – Overview
Windows Environment variables are strings that contain information about various system features and/or components as well as certain basic system data (i.e. drive, path and filename). They control the behavior of various programs, information about the environment for the system, and the currently logged on user. Some software programs use environment variables to determine where to place files (such as temporary files). 

Environment variables are very convenient in scripts where certain standard directories and parameters need to be referenced but where the actual locations or names can vary from computer to computer. In Windows, environment variables are enclosed by percent signs when used in scripts or the command line. For instance, to display the program search path on a DOS or Windows system, the user could type in the command echo %PATH%. The term “environment” generally refers to several features of the computer system, such as a drive, path, or filename. For example, the TEMP environment variable specifies the location in which programs place temporary files. 

Environment variables are enclosed by percent signs when used in scripts or the command line. For instance, to display the program search path on a DOS or Windows system, the user could type in this command echo %PATH%. Additionally, environment variables are not case-sensitive, although they are referenced in the tables below in upper case.

Values for some environment variables are established during a user’s login procedure (usually called pre-defined variables). They include such parameters as the path and the name of the current user, default printer, browser, or text editor to be used. Because these are preset as values specific to the identified user, they save time that would be used selecting them at each login. 

Some common predefined Microsoft Windows environment variables and their values are listed in the table below.

EnvironmentVariable Name Typical Value (May vary depending on system)
%ALLUSERSPROFILE% C:\Documents and Settings\All Users
%APPDATA% C:\Documents and Settings\{username}\Application Data
%COMPUTERNAME% {computer name}
%COMSPEC% C:\Windows\System32\cmd.exe
%HOMEDRIVE% C:
%HOMEPATH% \Documents and Settings\{username}
%PATH% C:\Windows\System32\;C:\Windows\;C:\Windows\System32\Wbem
%PATHEXT% .COM; .EXE; .BAT; .CMD; .VBS; .VBE; .JS ; .WSF; .WSH
%PROGRAMFILES% Directory containing program files, usually C:\Program Files
%PROMPT% Code for current command prompt format. Code is usually $P$G
%SYSTEMDRIVE% The drive containing the Windows XP root directory, usually C:
%SYSTEMROOT% The Windows XP root directory, usually C:\Windows
%TEMP% and %TMP% C:\DOCUME~1\{username}\LOCALS~1\Temp
%USERNAME% {username}
%USERPROFILE% C:\Documents and Settings\{username}
%WINDIR% C:\Windows

 

 

 

 

 

 

 

 

 

 

 

 

Predefined variables are generally unchanged during a login session but there are also some dynamic variables whose value may change. Some are listed in the table below.

Environment Variable Name Value
%DATE% Current date in the format determined by the Date command
%TIME% Current time in the format determined by the Time command
%CD% Current directory with its full path
%ERRORLEVEL% Number defining exit status of a previous command or program
%RANDOM% Random number between 0 and 32767


 

 

 

 

NOTE: The environment variables listed above are based on a Windows XP operating system. Many of the variables in Windows Vista are similar to those in Windows XP, however, there are some new variables in Windows Vista that are not included in Windows XP. Also the paths associated with some of the environment variables in Windows Vista have changed. 

Environment Variable Types
In Windows operating systems, there are generally three types of environment variables. User environment variables are set for each user and system (or machine) environment variables set for everyone. The system maintains an environment block for each user and one for the computer. 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. Every process has an environment block that contains a set of environment variables and their values. More details regarding each environment variable type is presented below:

Machine (or System) Environment Variables - Machine/System environment variables are defined by Windows and apply to all computer users. When a user creates a system environment variable in a set operation, the operating system stores the environment variable in the system registry, but not in the current process. If any user on the local machine starts a new process, the operating system copies the environment variable from the registry to that process. When the process terminates, the operating system destroys the environment variable in that process. However, the environment variable in the registry persists until a user removes it using a program or by means of an operating system tool. 

You must be an administrator to modify a system environment variable. Changes to the system environment are written to the registry and usually require a restart to become effective. System environment variables are stored or retrieved from the following registry location:

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment

User Environment Variables - User environment variables are tied to a user profile. A user's environment block represents the environment variables the system maintains for that particular user, including the set of system environment variables. The most common reason for adding user environment variables is to provide data that is required for variables to be used in scripts. Any user can add, modify, or remove a user environment variable. The changes are written to the registry, and are usually effective immediately. However, after a change to user environment variables is made, any open software programs should be restarted to force them to read the new registry values. 

When a user creates a user environment variable in a set operation, the operating system stores the environment variable in the system registry, but not in the current process. If the user starts a new process, the operating system copies the environment variable from the registry to that process. When the process terminates, the operating system destroys the environment variable in that process. However, the environment variable in the registry persists until the user removes it using a program or by means of an operating system tool. User environment variable are stored or retrieved from the following registry location:

HKEY_CURRENT_USER\Environment 

Process Enviroment Variables - Process environment variables are restricted to a single process. This environment variable is stored or retrieved from the environment block associated with the current process. The user creates the environment variable in a set operation. When the process terminates, the operating system destroys the environment variable in that process.    


Sample Task 1 - List Environment Variables
The ‘List Environment Variables’ action creates and populates a dataset that contains all the current environment variables within a specific scope along with their values. During runtime, a ‘Loop Dataset’ action is used to loop through the list of environment variables populated into the dataset. A 'Message Box' action is contained within the loop/end loop steps which display the current environment variable and its corresponding value during each loop process. 

In the General tab properties of the ‘List Environment Variables’ action, the option labeled Environment variable scope specifies the variable type to specify. As mentioned above, there are three environment variable scopes to select from:

  • User
  • Machine
  • Process

In this sample task, the environment variable scope selected is Machine. 

In the Create and populate dataset field, the dataset name entered is EnvironmentVariables. The following dataset fields (columns) are created by the ‘List Environment Variables’ action:

  • EnvironmentVariables.VarName - Specifies the name of the environment variable.
  • EnvironmentVariables.VarValue - Specifies the value of the environment variable.

List Environment Variables Action General Properties

Step 2 of the sample task uses a ‘Loop Dataset’ action which loops through the list of process type environmental variables. A ‘Message Box’ action resides inside the loop/end loop steps and is used to display the current environment variable’s name along with its value during each reiteration. The value entered in the Message to display parameter of the ‘Message Box’ action’s properties is as follows: 

%EnvironmentVariables.VarName% = %EnvironmentVariables.VarValue%

During runtime the value above results to message dialogues appearing (one at a time during each iteration).

Sample Task 2 - Create, Get, Append & Delete Environment Variable
The next sample task demonstrates a combination of other environment variable related procedures. It will contain the following actions the following actions: 

  • Set Environment Variable - Sets the value of a system or user-level environment variable, and optionally creates the variable if it does not exist.
  • Get Environment Variable - Retrieves the value of a system or user-level environment variable and places it into an Automate variable.
  • Append Environment Variable - Appends text to a system or user-level environment variable. Optionally, the action can populate a variable with the resulting value.
  • Delete Environment Variable - Removes a system or user-level environment variable.

Step 1 of the task uses a ‘Set Environment Variable’ action to create a new environment variable. In the General tab properties of this action, the parameter labeled Environment Variable Name specifies the name of the environment variable to create. The name entered in this sample is BPATemp.

The Environment variable scope selected is User. 

The parameter labeled New value specifies the value in which to populate the newly created environment variable with. If the environment variable already exists, this parameter then specifies the new value to populate the existing variable with. For this sample task, the value entered is BPA Training.

If the checkbox labeled Create Environment Variable only if it does not exist is checked, a new environment variable will be properly created only if an environment variable with the same name does not already exist. If it already exists, Automate will generate an error message stating Environment Variable already exists. If this parameter is unchecked and an environment variable with the same name already exists, the current value will be overwritten with the new value specified by the user. 

Set Environment Variable Action (Step 1) General Tab

A ‘Create Variable’ action is placed in Step 2 and creates an Automate variable named EnvironmentVarValue.

A ‘Get Environment Variable’ action resides in Step 3 and used to retrieve the value of an environment variable. In the Generaltab properties of this action, the parameter labeled Environment variable name denotes the name of the environment variable to retrieve. For this sample task, the environment variable name entered is BPATemp.

The parameter labeled Populate Variable with Value specifies the Automate variable to populate with the value of the environment variable selected. The variable created in Step 2 named EnvironmentVarValue is entered for this sample task.
 
Get Environment Variable Action (Step 3) General Tab

A ‘Message Box’ action is located in Step 4 which will display the value of the environment variable previously created. This action’s General tab parameter labeled Message to display is entered with the text below:

The value of environment variable BPATemp is:

%EnvironmentVarValue%

Step 5 contains a ‘Create Variable’ action and creates another Automate variable named EnvironmentVarAppended

An ‘Append Environment Variable’ action resides in Step 6. In the General properties of this action, the Environment variable name parameter specifies the name of the environment variable to which the specified text will be appended. For this sample, the Environment variable name parameter is populated with BPATemp. 

The Text to append parameter signifies the text to be added to the current value of the environment variable. For this sample task, the Text to Append parameter is populated with at Network Automation. 

The Populate variable with the value option specifies a variable to populate with the resulting value. For this sample, the variable created in Step 5 named EnvironmentVarAppended is entered.
 
Append to Environment Variable Action (Step 6) General Tab

Another ‘Message Box’ action is used which is located in Step 7. The Message to display parameter in the General of this step contains the following text:

The new value after appending the text 'at Network Automation' is:

%EnvironmentVarAppended%

The final step of this task consists of a ‘Delete Environment Variable’ action which will be used to delete the environment variable initially created in the beginning of the task. In the General tab properties of this action, the Environment Variable Name parameter specifies the name of the environment variable to remove. In this case, BPATemp is the environment variable name entered in this field. 

During runtime, the following will transpire during each step of this task: 

  1. A ‘Set Environment Variable’ action creates a new environment variable of type User along with a value for that variable. The value being BPATemp.
  2. A 'Create Variable' action creates an Automate variable named EnvironmentVarValue.
  3. A ‘Get Environment Variable’ action populates the Automate variable with the value of the newly created environment variable.
  4. A 'Message Box' action displays the value of the created environment variable.
  5. Another 'Create Variable' action creates a variable named EnvironmentVarAppended.
  6. An ‘Append Environment Variable’ action appends text to the value of the created environment variable. The text to append is at Network Automation. The Automate variable named EnvironmentVarAppended is populated with the text value.  
  7. Another 'Message Box' action displays the new value of the created environment variable after new text has been appended.
  8. Finally, a ‘Delete Environment Variable’ action deletes the environment variable initially created in the first step.

Message Box (Step 4) displayed during runtime

Message Box (Step 7) displayed during runtime

Conclusion
Environment variable manipulation may be necessary in order to define some aspects of a user's or a program's environment and control the way that a Windows operating system functions. Managing environment variables can be tedious and somewhat time consuming if performed manually. Automate’s environment variable actions is an ideal solution allowing such procedures to be performed in an automated fashion. In short, Automate greatly accelerates the process of managing environment variables, at the same time, eliminating redundancy and doing away with common human errors.

Applies to: Automate 10

Still have questions? We can help. Submit a case to technical support

Last Modified On:
You don't have the appropriate permissions.
No, open a new Support Case