Recipe2.11.Viewing and Setting Environment Variables


Recipe 2.11. Viewing and Setting Environment Variables

Problem

You want to view the current environment variables or create new ones.

Solution

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:"<ServerName>" 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 <ServerName> /u <UserName> /p <Password>

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 = "<SYSTEM>" objVar.Put_ WScript.Echo "Created environment variable " & strVarName

Discussion

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).

There are many different environment variables, which you can view by running the set command without any parameters from the command line. You can see the default environment variables provided by the OS in Appendix D.

See Also

Appendix DList of Default Environment Variables, MS KB 41246 (How to Use Environment Variable Substitution in Batch Files), MS KB 104011 (HOW TO: Propagate Environment Variables to the System), MS KB 185652 (Environment Variable Processing Order in Windows 2000), and MSDN: Win32_Environment



Windows Server Cookbook
Windows Server Cookbook for Windows Server 2003 and Windows 2000
ISBN: 0596006330
EAN: 2147483647
Year: 2006
Pages: 380
Authors: Robbie Allen

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net