Accessing environment variables


The following code fragments in VBScript, you must copy the text box of  "Program" step.

WshEnvironment object is used to access environment variables in VBScript.

Consider an example where value of the PATH variable is outputted:

Dim objShell, objEnv

'Create a variable objShell

Set objShell = WScript.CreateObject("WScript.Shell")

'Create an environment variable objEnv

Set objEnv = objShell.Environment

MsgBox "System path: " + objEnv("PATH")

The following table shows the list of variables that are supported by the operating system Windows. Scripts can access environment variables, which can be used in other applications.

Parameter

Description

System
(2000/XP/
Vista/Win7)

User
(2000/XP/
Vista/Win7)

Process (NT/
(2000/XP/
Vista/Win7))

NUMBER_OF_PROCESSORS

Number of processors on the workstation

X

-

X

PROCESSOR_ARCHITECTURE

The type of processor on the workstation

X

-

X

PROCESSOR_IDENTIFIER

Workstation Processor ID

X

-

X

PROCESSOR_LEVEL

The model number of the processor installed on the workstation

X

-

X

PROCESSOR_REVISION

Version of the processor installed on the workstation

X

-

X

OS

Operation system

X

-

X

COMSPEC

Executable file for the command line (e.g. cmd.exe).

X

-

X

HOMEDRIVE

Local Disk of User directory (e.g. c:\)

-

-

X

HOMEPATH

The default User Directory (eg \users\default on Windows 2000).

-

-

X

PATHEXT

Extensions of executable files (eg. .Com, .Exe, .Bat, or .Cmd).

X

-

X

PROMPT

Command line (e.g. $P$G).

-

-

X

SYSTEMDRIVE

Local Disk of System directory (e.g. c:\)

-

-

X

SYSTEMROOT

System directory (for example, c:\winnt). This variable is known as WINDIR.

-

-

X

WINDIR

System directory (for example, c:\winnt). This variable is known as SYSTEMROOT

X

-

X

TEMP

Directory for storing temporary files (for example, c:\temp)

-

X

X

TMP

Directory for storing temporary files (for example, c:\temp)

-

X

X

The following example displays some system information:

'create object WshShell: 

Set WshShell = WScript.CreateObject("WScript.Shell")

'create object Environment with value SYSTEM: 

Set WshSysEnv = WshShell.Environment("SYSTEM")

'create object Environment with value PROCESS: 

Set WshProEnv = WshShell.Environment("PROCESS")

 

SysInfo = "System parameters of the computer:" + Chr(10)+ Chr(10) 

SysInfo = SysInfo + "Processors: " + _ 

WshSysEnv("NUMBER_OF_PROCESSORS") + Chr(10) 

SysInfo = SysInfo + "Architecture: " + _ 

WshSysEnv("PROCESSOR_ARCHITECTURE") + Chr(10) 

SysInfo = SysInfo + "Processor ID: " + _ 

WshSysEnv("PROCESSOR_IDENTIFIER") + Chr(10) 

SysInfo = SysInfo + "Generation: " + _ 

WshSysEnv("PROCESSOR_LEVEL") + Chr(10) 

SysInfo = SysInfo + "Operation system: " + WshSysEnv("OS") + Chr(10) 

SysInfo = SysInfo + "Command line file: " + _ 

WshProEnv("COMSPEC") + Chr(10) 

SysInfo = SysInfo + "Paths: " + WshProEnv("PATH") + Chr(10) 

SysInfo = SysInfo + "Executable files: " + _ 

WshSysEnv("PATHEXT") + Chr(10) 

SysInfo = SysInfo + "Windows directory: " + _ 

WshProEnv("WINDIR") + Chr(10) 

SysInfo = SysInfo + "Temporary directory: " + WshProEnv("TEMP") + Chr(10) 

MsgBox SysInfo

 

Adding new variables and removing variables are considered in the following example:

Set objWSH =  CreateObject("WScript.Shell") 

Set objUserVariables = objWSH.Environment("USER")

For Each Var In objUserVariables

    MsgBox Var

Next

MsgBox objUserVariables("TEMP")

MsgBox objUserVariables.Item("TEMP")

 

MsgBox objUserVariables.Length

 

objUserVariables("MYVAR1") = "oops"

objUserVariables.Item("MYVAR2") = 1

 

MsgBox objUserVariables.Count

 

objUserVariables.Remove("MYVAR1")

objUserVariables.Remove("MYVAR2")

 

MsgBox objUserVariables.Count