Environment Variables Provided by Windows Operating System

Microsoft assigns a type to each environment variable that is part of the environment block of a process. The possible types are System, User, Volatile and Process.

An environment variable that contains the same value for all processes running on the system, regardless of which user the process runs as. These are shared or global environment variables, and are defined in the system registry under the HKEY_LOCAL_MACHINE hive.
An environment variable that is specified for a particular user on the system. These are defined in the system registry as well, but are maintained under the HKEY_CURRENT_USER hive.
An environment variable that is not persistent, and has a value that was assigned by the operating system based on some property of the system.
An environment variable that is set only for a particular process. Often this would be set in the environment block by the parent process at the time the process is created. These are not persistent.

Additionally, the CMD.EXE command processor defines some dynamic environment variables that are assigned new values after each command is evaluated. These variables are volatile.

These tables were created by going through the on-line help for Windows 2000. I have listed all the volatile and dynamic environment variables I could find defined. There are probably more, and some of these may not be accurately documented.

Volatile Environment Variables
Name Description
ALLUSERSPROFILE Local – returns the location of the All Users Profile.
APPDATA Local – returns the location where applications store data by default.
COMPUTERNAME System – returns the name of the computer.
HOMESHARE System – returns the network path to the user’s shared home directory. This variable is set based on the value of the home directory. The user’s home directory is specified in Local Users and Groups.
LOGONSERVER Local – returns the name of the domain controller that validated the current logon session.
USERDOMAIN Local – returns the name of the domain that contains the user’s account.
USERNAME Local – returns the name of the user currently logged on.
USERPROFILE Local – returns the location of the profile for the current user.
NUMBER_OF_PROCESSORS Number of processors running on the machine.
PROCESSOR_ARCHITECTURE Processor type of the user’s workstation.
PROCESSOR_IDENTIFIER Processor ID of the user’s workstation.
PROCESSOR_LEVE Processor level of the user’s workstation.
PROCESSOR_REVISION Processor version of the user’s workstation.
OS Operating system on the user’s workstation.
COMSPEC Executable file for the command prompt (typically cmd.exe).
HOMEDRIVE Primary local drive (typically the C drive).
HOMEPATH Default directory for users (typically \users\default in Windows 2000).
PATH PATH environment variable.
PATHEXT Extensions for executable files (typically .com, .exe, .bat, or .cmd).
PROMPT Command prompt (typically $P$G).
SYSTEMDRIVE Local drive on which the system directory resides (typcially c:\).
SYSTEMROOT System directory (for example, c:\winnt). This is the same as WINDIR.
WINDIR System directory (for example, c:\winnt). This is the same as SYSTEMROOT.
TEMP Directory for storing temporary files (for example, c:\temp).
TMP Directory for storing temporary files (for example, c:\temp).
Dynamic Environment Variables
Name Description
CD Expands to the current directory.
DATE Expands to the same output as typing DATE.
TIME Expands to the same output as typing TIME.
RANDOM Generates a random integer in the range of 0 – 32767.
ERRORLEVEL Expands to the current ERRORLEVEL.

Expands to the current Command Processor Extensions version number.
CMDCMDLINE Expands to the original command line that invoked the Command Processor.

PATH Environment Variable Limitations in Windows

Environment variables are managed using the System control panel applet in Windows. This user interface leaves much to be desired, but it works. Environment variables are separated into “System” and “User” categories. All users that may login on a machine share the System environment variables. Each user has her own set of User environment variables. User environment variables take precedence over System environment variables. How this precedence rule effects variable definitions is illustrated in the following example:

If the variable TEMP Generally speaking, if an environment variable appears in both categories then the value associated with the User environment variable will be used.

The PATH environment variable controls how the operating system searches for executable (EXE) and dynamic link library (DLL) files when asked to run an application. Some applications are installed for use by all users of the system and others are installed for exclusive use of a particular user.

Microsoft publishes some great human factors guidelines for building Windows applications. I sure wish they would follow some of these guidelines for the Control Panel applets that are provided with Windows. If these applets had to go through the logo certification process I’m sure they would fail!

Take for example the environment variable editing dialog box in the System applet.


This is a prefect example of what not to do when building a user interface. I hate editing environment variables using a one line edit box where you can’t even see the whole definition of the value. You can’t even resize the dialog box!

To make matters worse, there are limits imposed by the operating system that are not checked in the user interface. The PATH environment variable cannot exceed 1024 characters in length, but the user interface happily allows you to define a PATH that is way longer than this.

You can try this for yourself by defining a System PATH that is about 512 bytes in length and define a User PATH that is about 600 bytes long. Because the combine System+User PATH is longer than 1024 bytes the operating system decides to only use the System PATH, leaving out all your User PATH assignments.

A good user environment variable editor would check for this sort of thing. Since Microsoft doesn’t need to get their applications “logo certified” they can freely ignore the UI guidelines they publish. What a shame!

I looked into what it would take to create my own Control Panel Applet to replace this environment variable editor and it looks straight forward. Keep tuned, and maybe I’ll post an improved editor here.

As I dig further into this environment variable issue it seems like there is a lot of inconsistency between versions of Windows.

Microsoft has some information on the NT command prompt for Windows XP that says the maximum environment variable size is 8192 bytes and that the entire environment cannot exceed 65,536KB (though I think that is supposed to be bytes).

Early versions of MS-DOS had a 128 character limitation on the PATH variable. According to this article, that limitation was removed in MS-DOS 6.0. In KB169171 Microsoft talks about how 16-bit applications can still hang when the path exceeds 200 bytes.

The only place I could find that actually states that there is a 1KB limit on the combine length of the System+User path is here. In the WORKAROUND section of this article it states that the path should “total 1 KB or less of characters”. The article is for Windows NT Server 4.0 Terminal Server Edition, but my experiments on a Windows 2000 Professional system indicate that it is still the case. I haven’t tried Windows XP or Windows Server 2003 yet to determine if the problem exists in those variants of Windows.