Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

January 29, 2002 12:00 AM

What environment variables are available in Windows?

Windows IT Pro
InstantDoc ID #23873
Rating: (18)

A. You can use environment variables to gain information about a system. For a complete list of environment variables available in Windows, see the following table:

Table 1: Environment Variables

ALLUSERSPROFILE Local returns the location of the All Users Profile.
APPDATA Local returns the location where applications store data by default.
CD Local returns the current directory string.
CMDCMDLINE Local returns the exact command line used to start the current cmd.exe.
CMDEXTVERSION System returns the version number of the current Command Processor Extensions.
COMPUTERNAME System returns the name of the computer.
COMSPEC System returns the exact path to the command shell executable.
DATE System returns the current date. This variable uses the same format as the date /t command. Cmd.exe generates this variable. For more information about the date command, see the Date command.
ERRORLEVEL System returns the error code of the most recently used command. A non-0 value usually indicates an error.
HOMEDRIVE System returns which local workstation drive letter is connected to the user's 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.
HOMEPATH System returns the full path of the user's 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.
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.
LOGONSEVER Local returns the name of the domain controller that validated the current logon session.
NUMBER_OF_PROCESSORS System specifies the number of processors installed on the computer.
OS System returns the OS name. Windows XP and Windows 2000 display the OS as Windows_NT.
PATH System specifies the search path for executable files.
PATHEXT System returns a list of the file extensions that the OS considers to be executable.
PROCESSOR_ARCHITECTURE System returns the processor's chip architecture. Values: x86, IA64.
PROCESSOR_IDENTFIER System returns a description of the processor.
PROCESSOR_LEVEL System returns the model number of the computer's processor.
PROCESSOR_REVISION System returns the revision number of the processor.
PROMPT Local returns the command-prompt settings for the current interpreter. Cmd.exe generates this variable.
RANDOM System returns a random decimal number between 0 and 32767. Cmd.exe generates this variable.
SYSTEMDRIVE System returns the drive containing the Windows root directory (i.e., the system root).
SYSTEMROOT System returns the location of the Windows root directory.
TEMP or TMP System and User return the default temporary directories for applications that are available to users who are currently logged on. Some applications require TEMP and others require TMP.
TIME System returns the current time. This variable uses the same format as the time /t command. Cmd.exe generates this variable. For more information about the time command, see the Time command.
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.
WINDIR System returns the location of the OS directory

To access these environment variables, you must place a percentage symbol (%) before and after the variable. For example,

echo %PROCESSOR_ARCHITECTURE%
x86 
will display the processor type (but still won't display Intel's ia64 platform). Some environment variables are available only in later OSs (e.g., %RANDOM% is not available in Windows NT 4.0).

Related Content:

ARTICLE TOOLS

Comments
  • James
    5 years ago
    Aug 20, 2007

    Rendall Said:
    > Well, in xp, set USERPROFILE=D:\\Somefile works
    > only during the current session. Logging out and
    > in resets the environment variable to default. I
    > don't know how to fix that.

    If you have XP Pro, you can set that under "Manage Users". If you have XP Home will have to manually edit your registry. Open the following key:

    HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList

    You need to find your profile discription, the easyest way to do this is:

    Click Edit -> Find
    then type: \\YOUR_USER_NAME_HERE
    (replace YOUR_USER_NAME_HERE with your username, put NO space between the \\ and your username)
    check "Match whole string only"
    Click "Find Next"

    This should find your profile information, stopping on the entry "ProfileImagePath".

    Right Click "ProfileImagePath" -> Modify

    You will now be able to modify your profile path, something currently like "%SystemDrive%\\Documents and Settings\\xxx"

    So, in short, you want to edit this entry:
    HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\S-???-???-???\\ProfileImagePath

    Disclaimer: Messing with your registry can turn your PC into a nice paperweight, so backup anything you are about to change. And then STILL act with care. The registry is not for the faint of heart, nor the stupid.

    =====

    Dom Asked:
    > [how can I] find the path that a bat file was started from.
    > i want to copy a file from the same dir as the .bat file TIA
    > Dom

    %~dp0

    To use it as requested:

    copy "%~dp0\\somefile.ext" "E:\\where\\ever\\U\\like"

    FYI some neat ~ options are
    d - drive
    p - path
    n - name (w/o extention)
    x - extention only
    s - short filenames (8.3 format)
    a - the file attributes
    t - time and date
    z - file size

    (These are not the only modifiers, but they are the ones I use most.)

    All or none can be used, and in any order, so %~dpnxsatz0 will return something like:

    --a------ 08/19/2007 01:46 AM 127 C:\\PROGRA~1\\SUPPOR~1\\x.bat

  • Anonymous User
    7 years ago
    Aug 23, 2005

    when java(ver.1.5) is installed in windows it put the java.exe in windows system folder and also in its on directory, and it the path variable if we add the path of java, ie: path=%path%;C:\\JDK1.5\\bin; which java.exe will be invocked by windows ?? java.exe in he system folder or java.exe in the jdk folder ?? and also is there any way to understand the full path name of the program that is beeing executed ???
    ie: there are two java.exe in the path, one in the windows system folder and one in the jdk folder, and path of both directoy is mapped into the path variable, how to know which java.exe is working ????

  • Anonymous User
    7 years ago
    Apr 19, 2005

    Terrific article!! I was looking for something like this for a long time.

  • Anonymous User
    7 years ago
    Mar 29, 2005

    to the person looking to copy a file from the same directory that the .BAT or .CMD file was run from, simply do this

    copy %0\\..\\filename.ext destination

    It works!

  • Anonymous User
    7 years ago
    Mar 18, 2005

    http://www.microsoft.com/windowsxp/home/using/productdoc/en/ntcmds_shelloverview.asp

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.