Tutorial : Know about Environment Variables in Windows XP

Discussion in 'Tutorials' started by sridatta, Feb 16, 2007.

Thread Status:
Not open for further replies.
  1. sridatta

    sridatta An Esoteric Geek

    Mar 28, 2006
    Likes Received:
    Trophy Points:
    Hello guyz..
    when i am browsing through the system properties box, I just had a glance at Environment Variables.. and... I thought i could post a tutorial about this..

    What are Environment Variables?

    Environment variables are specially named aliases or placeholders for certain basic system properties that are present for convenience in programming and in system administration. Although they are mostly of interest to technical people, some can be useful to the average PC user and these are discussed here.

    Environment variables are strings that contain information such as drive, path, or file name. They control the behavior of various programs. For example, the TEMP environment variable specifies the location in which programs place temporary files.

    First i'll list all the Environment variables in windows XP and later have a glance at some of their applications.

    Note: I am referring C:\ as the drive where your windows XP is installed.

    %APPDATA% : C:\Documents and Settings\{username}\Application Data

    %COMPUTERNAME% : {computername}

    %COMSPEC% : C:\Windows\System32\cmd.exe

    %HOMEDRIVE% : C: (drive where windows xp is installed)

    %HOMEPATH% : \Documents and Settings\{username}

    %PATH% :C:\Windows\System32\;C:\Windows\;C:\Windows\System32\Wbem

    %PATHEXT% : .COM; .EXE; .BAT; .CMD; .VBS; .VBE; .JS ; .WSF: .WSH

    %PROGRAMFILES% : Directory containing program files, usually C:\Program Files

    %PROMPT% : Code for current command prompt format. Code is usually $P$G

    %TEMP% and %TMP% : C:\DOCUME~1\{username}\LOCALS~1\Temp

    %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.

    %ERRORLEVEL% : This variable points to the current error level. If there was an error in the previous command, this is what you need to check against to find out about that.

    %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.

    %AppData% : Contains the full path to the Application Data folder of the logged-in user. Does not work on Windows NT 4.0 SP6 UK.

    %ComSpec% : This variable contains the full path to the Windows NT command processor, cmd.exe.

    %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.

    %ProgramFiles% : This variable points to Program Files directory, which stores all the installed program of Windows and others. The default is C:\Program Files.

    %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 folder was placed.
    The value of %SystemDrive% is in most cases C:. The system drive cannot be altered once the operating system is running.

    %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 folder, 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 by default use \WINDOWS
    Windows NT 5.0 (Windows 2000), Windows NT 4.0 and Windows NT 3.1 by default use \WINNT
    Windows NT 3.5x by default uses \WINNT35

    %WinDir% : This variable points to the Windows directory. If the System is on drive C: then the default values are:
    C:\WINDOWS on Windows 95, Windows 98, Windows Me, Windows XP, and Windows Server 2003
    C:\WINNT for Windows NT 4 and Windows 2000

    %ALLUSERSPROFILE% : The %ALLUSERSPROFILE% 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 folders 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).

    %USERNAME% : Users can also use the %USERNAME% variable to determine the active users login identification.

    : Specifies the number of processors installed on the computer.

    %OS% : Returns the operating system name. Windows 2000 displays the operating system as Windows_NT.

    %PROCESSOR_ARCHITECTURE% : Returns the chip architecture of the processor. Values: x86, IA64.

    %PROCESSOR_IDENTIFIER% : Returns a description of the processor.

    %PROCESSOR_LEVEL% : Returns the model number of the processor installed on the computer.

    %PROCESSOR_REVISION% : Returns the revision number of the processor.

    %PROMPT% : Returns the command prompt settings for the current interpreter. Generated by Cmd.exe.

    How to View & Set Environment Variables :

    1. Graphical Method : ( Permanent Changes )

    Not all environment variables are listed here.. but you can find and edit most of them.. here you go...

    Right Click on MyComputer -> Properties -> Advanced tab -> Environment Variables (button).

    The first set of variables are Local/User variables and those are applicable to that particular user who logged on the system. The Second set, called System variables are global to whole system.

    You can view, edit or delete the keys in this GUI window...

    Remember : Do not modify any Environment Variables unless you know what to do with..

    2. Command Line method : (Temporary Changes)

    The changes applied to the environment variables lasts till you close your command window.. These are actually used in scripts... Not a useful method for now...

    Start -> Run and enter cmd

    echo %EnvironmentVariable% will display its value...

    Eg : echo %username% will displays the current user..

    We use SET command to add/edit the enivonment variables through command prompt.

    Use the set command to create, change, delete, or display environment variables. The set command alters variables in the current shell environment only.

    To view a variable, at a command prompt, type:

    set VariableName

    To add a variable, at a command prompt, type:

    set variablename=value

    To delete a variable, at a command prompt, type:

    set VariableName=

    You can use most characters as variable values, including white space. If you use the special characters <, >, |, &, or ^, you must precede them with the escape character (^) or quotation marks. If you use quotation marks, they are included as part of the value because everything following the equal sign is taken as the value. Consider the following examples:•
    To create the variable value new&name, type:

    set varname=new^&name

    To create the variable value "new&name", type:

    set varname="new&name"

    If you type set varname=new&name at the command prompt, an error message similar to the following appears:
    "'name' is not recognized as an internal or external command, operable program or batch file."

    Variable names are not case-sensitive. However, set displays the variable exactly as you typed it. You can combine uppercase and lowercase letters in your variable names to make your code more readable (for example, UserName).

    3. Through Registry (Un predictable Changes :D)

    The registry paths for local and system variables can be found at

    Local Variables :

    System Variables :
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

    Note that any environment variable that is in the form that needs to be expanded (for example, %PATH%) must be stored in the registry as a REG_EXPAND_SZ registry value. Editing the Registry is primarily for scripts used by systems administrators and is not recommended for the average PC user.


    Now, we shall see some of the applications of these Environment Variables

    Path Environment Variable :

    Here is where you set the paths of the directories where windows will search by default, the executable files. Confused?? Well.. let me explain.. suppose you type notepad in the Run window, Windows will automatically launch notepad although you donot specify the path of it. Because, the value of Path environment variable contains the entry %SYSTEMROOT%\System32. Hence, windows will automatically search by default, this folder for that particular executable if no path is mentioned along with it.

    Suppose that we use some applications very often that we feel confortable to run them through Run window. One thing we can do is.. to place their shortcuts in system32 folder. Other thing is that.. we can add a Local Environment Variable named Path specifying the path of the directory where you place those frequently used softwares...




    The PATHEXT Variable

    Let me take the above example to illustrate this also.. when i type "notepad" in Run command (without quotes ofcourse), Notepad will run.. although you didnot specify its extension as .exe explicitly. Here is where windows stores file extensions to indicate that the file is executable; that is, the file opens a program or does something. The %PATHEXT% environment variable contains executable file extensions that do not have to be specified for any file in a directory given in the %PATH% variable. The extension .EXE is the most common. Thus the filename sol.exe need only be entered as sol in a command line. If files have the same name but different extensions, the operating system searches in the following order of precedence: .EXE, .COM, .BAT, and .CMD.

    We cannot add extensions of non-executable types here...


    P.S : Please post your comments or queries...
  2. VexByte

    VexByte Norms Buster

    Feb 10, 2007
    Likes Received:
    Trophy Points:
    Very nice find....It's a tiny retrospection to the good old days of DOS, BAT files & command line !!!
  3. OP

    sridatta An Esoteric Geek

    Mar 28, 2006
    Likes Received:
    Trophy Points:
    Well.. This seems to be a OHT for many of u ppl.. You can atleast refer to Applications part directly.. its somehow useful for most of us..
Thread Status:
Not open for further replies.

Share This Page