Getting and setting environment variables
The variables can be used both in scripts and on the command line. They are usually referenced by putting special symbols in front of or around the variable name. For instance, to display the user home directory, in most scripting environments, the user has to type:
echo $HOME
On DOS, OS/2 or Windows systems, the user has to type this:
echo %HOME%
In Windows PowerShell, the user has to type this:
Write-Output $HOME
DOS, OS/2 and Windows (Command Prompt)
In DOS, OS/2 and Windows, the set
command without any arguments displays all environment variables along with their values.
To set a variable to a particular value, use:
set VARIABLE=value
However, this is temporary. Permanent change to the environment variable can be achieved through editing the registry (not recommended for novices) and using the Windows Resource Kit application setx.exe
. With the introduction of Windows Vista, the setx
command became part of Windows.
Users of the Windows GUI can manipulate variables via <Control Panel:System:Advanced:Environment Variables>; through the Windows Registry this is done changing the values under HKCU\Environment (for user specific variables) and HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment (for System variables).
To set a variable whose name is in another variable:
set %VARNAME%=value
This feature allows certain interesting applications. For example, a uni-dimensional array of elements (vector) may be generated this way:
set VECTOR[%I%]=value of element subscript %I% :MkVec set VECNAME=%1 set i=0 :loop shift if "%1" == "" goto exitloop set /a i+=1 set %VECNAME%[%i%]=%1 goto loop :exitloop exit /B %i% call :MkVec DOWNAME=Monday Tuesday Wednesday Thursday Friday Saturday Sunday
To see the current value of a particular variable, use:
echo %VARIABLE%
or
set VARIABLE
Note: Please take note that doing so will print out all variables beginning with ‘VARIABLE’. Another example is:
C:\> set p Path=c:\.. .. PATHEXT=.COM;.EXE;.BAT; PROCESSOR_ARCHITECTURE=.. .. PROCESSOR_IDENTIFIER=x8.. PROCESSOR_LEVEL=6.. PROCESSOR_REVISION=1706.. ProgramFiles=C:\Program.. . PROMPT=$P$G
To see the value of an array element a double expansion is required: one for the subscript value and an additional expansion for the array element. This may be achieved via Delayed !VARIABLE! Expansion this way:
set DOW=value of Day of Week (1..7) echo !DOWNAME[%DOW%]!
To delete a variable, the following command is used:
set VARIABLE=
Windows PowerShell
To set a system variable:
Set-Content -Path Env:VARIABLE -value value $Env:VARIABLE = value # alternate form
Examples of DOS environment variables
This variable contains the full path to the command processor, command.com.
- %PATH%
This variable contains a semicolon-delimited list of directories in which the command interpreter will search for executable files. Equivalent to the Unix $PATH variable (although note that PATH on Windows additionally performs the same task as LD_LIBRARY_PATH on Unix-like systems). Note that %PATH% can also be set like this PATH=c:\dos; where SET isn’t required.
- %TEMP% and %TMP%
These variables contain the path to the directory where temporary files should be stored.
Examples from Microsoft Windows
Discrete value variables
These variables generally expand to discrete values, such as the current working directory, the current date, or a random number. Some of these are true environment variables and will be expanded by all functions that handle environment variables. Others, like %CD% simply look like environment variables and will only be expanded by some functions and shells. They are not case sensitive.
- %CD%
This variable points to the current directory. Equivalent to the output of the command cd when called without arguments.
- %DATE%
This variable expands to the current date. The date is displayed according to the current user’s date format preferences.
The following is a way of reformatting the date and time for use in file copies. The example assumes UK format of day month year and the time is set for a 24 hour clock.
@echo off echo %DATE% %TIME% for /F "tokens=1-3 delims=/" %%a in ("%DATE%") do set MTH=%%a& set DAY=%%b& set YR=%%c for /F "tokens=1-3 delims=:." %%a in ("%TIME%") do set HR=%%a& set MIN=%%b& set SEC=%%c if "%HR:~0,1%"==" " set HR=0%HR:~1,1% set MYDATE=%YR%%MTH%%DAY%-%HR%%MIN%%SEC% echo %MYDATE%
- %ERRORLEVEL%
This variable points to the current error level. If there was an error in the previous command, it is checked against this.
- %RANDOM%
This variable returns a random number between 0 and 32767.
- %TIME%
This variable points to the current time. The time is displayed according to the current user’s time format preferences.
System path variables
These variables refer to locations of critical operating system resources, and as such generally are not user-dependent.
- %AppData%
Contains the full path to the Application Data directory of the logged-in user. Does not work on Windows NT 4.0 SP6 UK.
- %LOCALAPPDATA%
This variable is the temporary files of Applications. Its uses include storing of Desktop Themes, Windows Error Reporting, Caching and profiles of web browsers.
- %ComSpec%
This variable contains the full path to the command processor; on Windows NT based operating systems this is cmd.exe, while on Windows 9x and ME it is the DOS command processor, COMMAND.COM.
- %PATH%
This variable contains a semicolon-delimited (do not put spaces in between) list of directories in which the command interpreter will search for an executable file that matches the given command. Equivalent to the Unix $PATH variable.
- %ProgramFiles%
This variable points to Program Files directory, which stores all the installed program of Windows and others. The default on English-language systems is C:\Program Files. In 64-bit editions of Windows (XP, 2003, Vista), there are also %ProgramFiles(x86)% which defaults to C:\Program Files (x86) and %ProgramW6432% which defaults to C:\Program Files. The %ProgramFiles% itself depends on whether the process requesting the environment variable is itself 32-bit or 64-bit (this is caused by Windows-on-Windows 64-bit redirection).
- %CommonProgramFiles%
This variable points to Common Files directory. The default is C:\Program Files\Common Files.
- %SystemDrive%
The %SystemDrive% variable is a special system-wide environment variable found on Microsoft Windows NT and its derivatives. Its value is the drive upon which the system directory was placed. Also see next item.
The value of %SystemDrive% is in most cases C:.
- %SystemRoot%
The %SystemRoot% variable is a special system-wide environment variable found on Microsoft Windows NT and its derivatives. Its value is the location of the system directory, including the drive and path.
The drive is the same as %SystemDrive% and the default path on a clean installation depends upon the version of the operating system. By default, on a clean installation:
- Windows NT 5.1 (Windows XP) and newer versions use \WINDOWS
- Windows NT 5.0 (Windows 2000), Windows NT 4.0 and Windows NT 3.1 use \WINNT
- Windows NT 3.5x uses \WINNT35
- Windows NT 4.0 Terminal Server use \WTSRV
- %WinDir%
This variable points to the Windows directory (on Windows NT-based operating systems it is identical to the %SystemRoot% variable, above). If the System is on drive C: then the default values are:
- C:\WINDOWS on Windows 95, Windows 98, Windows Me, Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008 and Windows 7
- C:\WINNT for Windows NT 4, and Windows 2000
Note that Windows NT 4 Terminal Server Edition by default installs to C:\WTSRV.
User management variables
These variables store information related to resources and settings owned by various user profiles within the system. As a general rule, these variables do not refer to critical system resources or locations that are necessary for the OS to run.
- %AllUsersProfile% (%PROGRAMDATA% for Windows Vista, Windows 7)
The %AllUsersProfile%(%PROGRAMDATA%) variable expands to the full path to the All Users profile directory. This profile contains resources and settings that are used by all system accounts. Shortcut links copied to the All Users’ Start menu or Desktop directories will appear in every user’s Start menu or Desktop, respectively.
- %UserDomain%
The variable holds the name of the Workgroup or Windows Domain to which the current user belongs. The related variable, %LOGONSERVER%, holds the hostname of the server that authenticated the current user’s logon credentials (name and password). For Home PCs, and PCs in a Workgroup, the authenticating server is usually the PC itself. For PCs in a Windows Domain, the authenticating server is a domain controller (a primary domain controller, or PDC, in Windows NT 4-based domains).
- %UserProfile%
The %UserProfile% variable is a special system-wide environment variable found on Microsoft Windows NT and its derivatives. Its value is the location of the current user’s profile directory, in which is found that user’s HKCU registry hive (NTUSER).
Users can also use the %USERNAME% variable to determine the active users login identification.
Windows GUI forced variable expansion
In certain cases it is not possible to create file paths containing environment variables using the Windows GUI, and it is necessary to fight with the user interface to make things work as intended.
- In Windows 7, a shortcut may not contain the variable %USERNAME% in unexpanded form. Trying to create shortcut to \\server\share\accounts\%USERNAME% or C:\users\%USERNAME% will be silently changed to replace %USERNAME% with the account name of the currently logged-in user, when the OK button is pressed on the shortcut properties.
- This can only be overridden if the %USERNAME% variable is part of a parameter to some other program in the shortcut. For example, %SYSTEMROOT%\Explorer.exe C:\Users\%USERNAME% is not expanded when OK is clicked, but this shortcut is treated as unsafe and displays a warning when opened.
- In Group Policy Management on Server 2008 R2, a profile directory can not be redirected to a custom directory hierarchy. For example, the desktop can not be redirected to \\server\share\accounts\%USERNAME%\custom\path\desktop. Upon pressing OK, this is silently changed to “Create a directory for each user in the root path” with the path \\server\share\accounts\ pointing to “\username\desktop”.
- This behavior can only be overridden if the path contains a variable or drive letter that is not currently resolvable at the time of editing the GPO. For example if a mapping for drive Z: does not exist on the server, then the path Z:\directory\%username%\CustomTarget is not expanded when OK is selected.
- A domain user account may not contain a profile path or home directory path containing an unexpanded %USERNAME% variable. Upon clicking OK, this is silently replaced with the user’s account name.
- This causes problems for new user creation that is performed by copying an existing user account, if there are additional directories listed after the username in the path. For a pre-existing account with a profile path of \server\share\accounts\DomainUser\profile the Microsoft Management Console doesn’t know which part of the path contains the previous user’s name and doesn’t change the path during the copy, resulting in the new account pointing to the other account’s profile/home paths. The profile/home paths must be manually re-edited to point to the correct location.
Default Values on Microsoft Windows
Variable | Windows XP | Windows Vista/7 |
---|---|---|
%ALLUSERSPROFILE% | C:\Documents and Settings\All Users | C:\ProgramData |
%APPDATA% | C:\Documents and Settings\{username}\Application Data | C:\Users\{username}\AppData\Roaming |
%COMPUTERNAME% | {computername} | {computername} |
%COMMONPROGRAMFILES% | C:\Program Files\Common Files | C:\Program Files\Common Files |
%COMMONPROGRAMFILES(x86)% | C:\Program Files (x86)\Common Files (only in 64-bit version) | C:\Program Files (x86)\Common Files (only in 64-bit version) |
%COMSPEC% | C:\Windows\System32\cmd.exe | C:\Windows\System32\cmd.exe |
%HOMEDRIVE% | C: | C: |
%HOMEPATH% | \Documents and Settings\{username} | \Users\{username} |
%LOCALAPPDATA% | C:\Users\{username}\AppData\Local | |
%LOGONSERVER% | \\{domain_logon_server} | \\{domain_logon_server} |
%PATH% | C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;{plus program paths} | C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;{plus program paths} |
%PATHEXT% | .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.WSF;.WSH | .com;.exe;.bat;.cmd;.vbs;.vbe;.js;.jse;.wsf;.wsh;.msc |
%PROGRAMDATA% | %SystemDrive%\ProgramData | |
%PROGRAMFILES% | %SystemDrive%\Program Files | %SystemDrive%\Program Files |
%PROGRAMFILES(X86)% | %SystemDrive%\Program Files (x86) (only in 64-bit version) | %SystemDrive%\Program Files (x86) (only in 64-bit version) |
%PROMPT% | Code for current command prompt format. Code is usually $P$G | Code for current command prompt format. Code is usually $P$G |
%PSModulePath% | %SystemRoot%\system32\WindowsPowerShell\v1.0\Modules\ | |
%PUBLIC% | %SystemDrive%\Users\Public | |
{Drive}:\$Recycle.Bin | C:\Recycle.Bin | C:\$Recycle.Bin |
%SystemDrive% | C: | C: |
%SystemRoot% | The Windows directory, usually C:\Windows, formerly C:\WINNT | %SystemDrive%\Windows |
%TEMP% and %TMP% | %SystemDrive%\Documents and Settings\{username}\Local Settings\Temp | %SystemDrive%\Users\{username}\AppData\Local\Temp |
%USERDOMAIN% | {userdomain} | {userdomain} |
%USERNAME% | {username} | {username} |
%USERPROFILE% | %SystemDrive%\Documents and Settings\{username} | %SystemDrive%\Users\{username} |
%WINDIR% | %SystemDrive%\Windows | %SystemDrive%\Windows |
In this list, there is no environment variable that refers to the location of the user’s My Documents directory, so there is no standard method for setting a program’s home directory to be the My Documents directory.