Viewing and Setting Environment Variables

Environment variables are very similar in concept to the variables you’d find in a programming language: there is a variable identifier and an associated value. This is extremely handy because it means that you don’t always have to know the exact path or name of certain settings on the system.

For example, let’s say you wanted to view the NetLogon log file on a remote system. After you log on to the system, you have to locate the file. On your computer it is located in C:\WINNT\Debug, but that directory might not exist on the remote computer. You then perform a search for the file and find it is located in D:\Windows\Debug. If you used environment variables, finding the file would have been a lot easier. On your machine, the Debug directory lives in the C:\WINNT directory, which is the default system root path. On the remote server it was D:\Windows. The default system root directory on Windows Server 2003 is \Windows, but since it is on the D: drive in this case, that means whoever installed the OS chose a nondefault drive (which is fine). Regardless of where the system root is, the SystemRoot environment variable defines this path for us. By simply enclosing SystemRoot in percent signs (%), we could have found our file much quicker (%systemroot%\debug\netlogon.log).

Using a graphical user interface

  1. From the Control Panel, open the System applet.
  2. Select the Advanced tab.
  3. Click the Environment Variables button.
  4. Click the New button under the User variables or System variables box depending on whether you want to create an environment variable that is visible only to the currently logged-on user or system-wide.
  5. Enter the variable name and value and click OK until all windows are closed.

The new variable(s) will not be available in any CMD windows that are currently open. You’ll need to close and reopen any CMD sessions in which you want to use the new variable(s).

Using a command-line interface

To view environment variables, run the set command. You can also view a subset of environment variables by running set and specifying the first letters of the variable(s). This command displays all environment variables that begin with USER:

> set user

You can use the wmic utility to print environment variables on a remote system:

> wmic /node:"" environment list full

You can print the value of an environment variable using echo:

> echo %systemroot%

To set an environment variable for use in the current CMD session, use the set command. The following command sets the FOOBAR environment variable:

> set FOOBAR=test

FOOBAR will be valid only for the life of the CMD session you set it in. If you need to create a permanent environment variable, use setx.exe:

> setx FOOBAR test

Just as with set, you will not be able to use the new variable in any CMD sessions you had open before creating it (other than the one in which it was created).

With the Windows Server 2003 version of setx (which comes with the OS), you can even set new environment variables on a remote server:

> setx FOOBAR test /s  /u  /p
 

Using VBScript

' This code prints the environment variables.
 ' ------ SCRIPT CONFIGURATION ------
 strComputer = "."
 ' ------ END CONFIGURATION ---------
 set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 set colVars = objWMI.InstancesOf("Win32_Environment")
 for each objVar in colVars
    WScript.Echo objVar.Name & ": " & objVar.variableValue & _
                 " (" & objVar.Username & ")"
 next
' This code shows how to expand an environment variable.
 set objShell = CreateObject("WScript.Shell")
 WScript.Echo objShell.ExpandEnvironmentStrings("%systemroot%\notepad.exe")
' This code creates a new system environment variable called FOOBAR.
 ' ------ SCRIPT CONFIGURATION ------
 strVarName = "FOOBAR"
 strVarValue = "Foobar Value"
 strComputer = "."
 ' ------ END CONFIGURATION ---------
 set objVarClass = GetObject("winmgmts:\\" & strComputer & _
                             "\root\cimv2:Win32_Environment")
 set objVar = objVarClass.SpawnInstance_
 objVar.Name = strVarName
 objVar.VariableValue = strVarValue
 objVar.UserName = ""
 objVar.Put_
 WScript.Echo "Created environment variable " & strVarName