Recipe 2.11. Viewing and Setting Environment VariablesProblemYou want to view the current environment variables or create new ones. SolutionUsing a graphical user interface
Using a command-line interfaceTo 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 DiscussionEnvironment 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 AlsoAppendix 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 |