Environment variables are useful when working with the CMD shell or when automating tasks. To use an environment variable, enclose the variable name with percent signs (%). For example, this command prints the SystemRoot environment variable:
You can generally use environment variables with most commands. This makes them very useful in batch scripts. This command changes the working directory to the system32 directory:
For more on environment variables, see Recipe 2.11.
The following are the default environment variables available with Windows 2000 and Windows Server 2003:
- ALLUSERSPROFILE
-
The path to the All Users Profile.
Example: C:\Documents and Settings\All Users\Desktop
- APPDATA
-
User-specific path where applications store data by default.
Example: C:\Documents and Settings\Administrator\Application Data
- CD
-
Current working directory.
Example: C:\
- CMDCMDLINE
-
The path to the CMD executable.
Example: C:\WINDOWS\system32\cmd.exe
- CMDEXTVERSION
-
The version number of the Command Processor Extensions.
Example: 2
- COMPUTERNAME
-
The name of the computer.
Example: SRV01
- COMSPEC
-
The path to the CMD executable.
Example: C:\WINDOWS\system32\cmd.exe
- DATE
-
The current date.
Example: Fri 01/16/2004
- ERRORLEVEL
-
The error code returned from the most recently used command. A value other than 0 indicates an error condition.
Example: 0
- HOMEDRIVE
-
The drive where the currently logged on user's home directory is located.
Example: C:
- HOMEPATH
-
The path to the currently logged on user's home directory.
Example: \Documents and Settings\Administrator
- HOMESHARE
-
The network path to the currently logged on user's home directory (if on a network share).
Example: \\fs01\rallen
- LOGONSEVER
-
In workgroup mode, this will be local server. When the computer is part of a domain, this will be the local server for cached logons or a domain controller for authenticated logons.
Example: AD01
- NUMBER_OF_PROCESSORS
-
The number of processors installed on the system.
Example: 1
- OS
-
The operating system name.
Example: Windows_NT
- PATH
-
The search path for programs.
Example: C:\Perl\bin\;C:\Program Files\Res Kits\Tools\;C:\Program Files
- PATHEXT
-
The list of extensions the operating system uses to find unqualified file names.
Example: .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
- PROCESSOR_ARCHITECTURE
-
The processors chip architecture (e.g., x86 or IA64)
Example: x86
- PROCESSOR_IDENTFIER
-
A description of the processor.
Example: x86 Family 15 Model 0 Stepping 7, GenuineIntel
- PROCESSOR_LEVEL
-
The processor level.
Example: 15
- PROCESSOR_REVISION
-
The revision number of the processor.
Example: 0007
- PROMPT
-
The variables used to generate the command prompt.
Example: $P$G
- RANDOM
-
A random number between 0 and 32767 generated by the operating system.
Example: 19803
- SYSTEMDRIVE
-
The drive on which the operating system was installed.
Example: C:
- SYSTEMROOT
-
The path to the root directory containing the operating system files.
Example: C:\WINDOWS
- TEMP and TMP
-
The user-specific path to the directory used to house temporary files.
Example: C:\DOCUME~1\ADMINI~3\LOCALS~1\Temp\1
- TIME
-
The current time.
Example: 0:07:22.67
- USERDOMAIN
-
The domain of the currently logged on user.
Example: RALLENCORP
- USERNAME
-
The username of the currently logged on user.
Example: administrator
- USERPROFILE
-
The user-specific path to user profile data.
Example: C:\Documents and Settings\Administrator
- WINDIR
-
The path to the root directory containing the operating system files.
Example: C:\WINDOWS