Environment Variables in Windows 7

Environment Variables in Windows 7
I have discussed environment variables in some detail for Windows XP on another page. Many of the general considerations given there apply to Windows 7 as well and they will not be repeated here. However, there are some changes and they will be outlined.List of common preset environment variables
The table below lists the environment variables most usually encountered. I have omitted some of the more technical ones, such as those that give information about the CPU. Many of the variables in Windows 7 are similar to those in Windows XP but there are some that are new. Also the paths associated with many have changed. Changes and additions are indicated in red.

Table I. Default Values on Microsoft Windows 7
Variable Windows 7
 %ALLUSERSPROFILE% and %PROGRAMDATA% C:\ProgramData
 %APPDATA% C:\Users\{username}\AppData\Roaming
 %COMPUTERNAME% {computername}
 %COMMONPROGRAMFILES% C:\Program Files\Common Files
 %COMMONPROGRAMFILES(x86)% C:\Program Files (x86)\Common Files
 %COMSPEC% C:\Windows\System32\cmd.exe
 %HOMEDRIVE% C:
 %HOMEPATH% \Users\{username}
 %LOCALAPPDATA% C:\Users\{username}\AppData\Local
 %LOGONSERVER% \\{domain_logon_server}
 %PATH% C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;{plus program paths}
 %PATHEXT% .com;.exe;.bat;.cmd;.vbs;.vbe;.js;.jse;.wsf;.wsh;.msc
 %PROGRAMFILES%  %SystemDrive%\Program Files
 %PROGRAMFILES(X86)%  %SystemDrive%\Program Files (x86) (only in 64-bit version)
 %PROMPT% Code for current command prompt format. Code is usually $P$G
{Drive}:\$Recycle.Bin C:\Recycle.Bin
 %SystemDrive% C:
 %SystemRoot%  %SystemDrive%\Windows
 %TEMP% and %TMP%  %SystemDrive%\Users\{username}\AppData\Local\Temp
 %USERDOMAIN% {userdomain}
 %USERNAME% {username}
 %USERPROFILE%  %SystemDrive%\Users\{username}
 %WINDIR% C:\Windows
 %PUBLIC%  %SystemDrive%\Users\Public
 %PROGRAMDATA%  %SystemDrive%\ProgramData
 %PSModulePath%  %SystemRoot%\system32\WindowsPowerShell\v1.0\Modules\

In this list, there is no environment variable that refers to the location of the user’s My Documents folder, so there is no standard method for setting a program’s home directory to be the My Documents folder.

Windows 7 has changed the locations for many of the personal files and settings. For example, there is no longer a “Documents and Settings” folder. Most of the XP environment variables that referred to this folder are still in Windows 7 but involve the new “Users” folder. (As an aside: why does Microsoft continue to use some folders whose names have spaces like “Program Files”? It means that environment variables often have to be put in quotation marks.) There are also four new environment variables listed in Table I. These are %CommonProgramFiles%, %LocalAppdata%, %ProgramData%, and %Public%

Dynamic environment variables
The so-called predefined variables are generally unchanged during a login session but there are also some dynamic variables whose value may change. Some of these are listed in the next table. These are the same as in Windows XP but are listed again for convenience.

Table II. Some dynamic environment variables
Variable 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

 Editing and adding environment variables
Sometimes it is desirable to edit certain of the environment variables such as %PATH%. Or it may be useful to create a user-defined environment variable. Windows 7 contains a command-line method SETX that provides a variety of options. It can be used to either create or modify environment variables. Variables can be in the user or the system environment. The command has a number of switches that make it useful in scripts. (Needs administrator privileges.)Environment variables can also be edited directly in the Registry. They are stored in the same Registry keys as they are in Windows XP. System variables are in the key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment User-specific variables are in HKEY_CURRENT_USER\Environment