Tip of the day: Learn to use environment variables

Environment variables contain information about the environment for the system and for the user who’s currently logged on. Knowing how to use these variables can save you time and keystrokes in batch files and shortcuts and at the command line. In addition, some programs use environment variables to determine where to store user data and temporary files. To use an environment variable in a command or a dialog box, you surround the variable name with percent signs; this tells Windows to look up the current value for that variable in the registry and use that value in the command or path where you entered it.

Two environment variables that I use regularly are %appdata% and %userprofile%. By typing either of these strings in the Run dialog box and pressing Enter, I can jump straight to the Application Data folder or the root of the User Profile folder for the currently logged-on user. I don’t need to know what drive Windows is installed on, and I don’t need to know the name of the current user. Similarly, typing cd %allusersprofile% at a command prompt switches to the All Users profile folder, where shared documents and settings are stored. In a default installation, the full path name is nearly twice as long: C:\Documents and Settings\All Users. You can also use environment variables to create shared shortcuts that work differently for each user. A shortcut that points to the %UserProfile%\My Documents folder, for instance, will always open the My Documents folder of the user who’s currently logged on.

Windows XP creates a group of default system variables, including several that define the location of Windows system files. Only an administrator can change a system variable. However, any user can add, edit, or remove user variables, which are exclusive to the user who created them and can be used in scripts and batch files.

To view and edit all variables, open the System option in Control Panel, and click the Environment Variables button on the Advanced tab. Click the New button to add a variable name and value, click the Edit button to change an existing variable, or click the Delete button to remove a variable.

Env_variables

You can also see a list of all currently defined environment variables by opening a Command Prompt window and entering the set command.

14 thoughts on “Tip of the day: Learn to use environment variables

  1. As usual Ed you provide usable info that I find essential. How about providing some more examples of how you’ve seen the environmental variables used? I’m in an enterprise environment and do quite a bit of VB Scripting. There must be a few tricks up your sleeve?

  2. Phil, at the risk of sounding like I’m plugging my book… Well, OK, I’m plugging the book.

    Windows XP Inside Out Deluxe Second Edition has probably 40 pages (maybe more) on using environment variables in scripts and batch files, with lots of really good examples. Most of the work was done by my co-author Carl Siechert. If you haven’t got that book, there’s your starting point!

  3. I’m wanting to find the location of the “My Documents” folder (Which may or may not have been relocated from it’s ‘usual’ location).

    Is there a system variable that I can use to locate this?

  4. But what if you’re installing something, and it happens that the user utilizes an OS of a different language (say, Spanish or French)? And even if it is in English, what happens if he redirects his My Documents folder to a different one?

  5. Is it possible to add comments in system variables, this could be very handy to comment some settings out temporarily.
    Thank you, Greetings, Gnap.

  6. Hi Ed,
    Can I include Environment Variables in the key values of the Registry?
    For example, I have the following value in the RUN key, but the %windir% never got expanded to c:\windows\

    xcopy /y /v “\server\Office Share\IT\COMPUTERS\Tools\zchecknet.cmd” %windir%\system32\zchecknet.cmd

    Any ideas (please email me if possible)? Thnx!
    Calvin

  7. Calvin, you should indeed be able to use environment variables in the way you suggest. However, the reason your command isn’t working is because you can’t use UNC syntax (\server\share) at a command prompt, and the xcopy command runs in a command shell. Try mapping the server based locations to a drive letter and see if that works.

  8. Thank you Ed for replying so promptly.

    I have read your reply. However, I was able to do the xcopy on logon with the UNC syntax. It is just not copying to the %windir%\system32 directory (instead, it created %windir%\system32\zchecknet.cmd in my user profile under “Documents and Settings”.

    Here’s the registry key that I exported:

    Windows Registry Editor Version 5.00

    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\Explorer\Run]

    “1”=”xcopy /y /v \”\\server\Office Share\IT\COMPUTERS\Tools\zchecknet.cmd\” %windir%\system32\”

    If I replace %windir% in the registry key by c:\windows, then the xcopy works fine (What I am trying to do here is to have the zchecknet.cmd file copied to user computers every time they logon to our domain; I’m controlling this via W2K Group Policy, hence you see the key lies under policies\explorer\run ).

    Any other suggestions? Thnx again,
    Calvin

  9. Calvin,

    Aha. A little Googling turns up the news that environment variables stored in the Registry are not expanded by default. Apparently you need to use the Expandable String data type to make it work. There’s an interesting article in VBScripting Solutions that might point you in the right direction:

    Link.

    Hope that helps!

    Ed

  10. Ed,
    You never answered the question on how to find “My documents” for use from scripts. I searched registry, environment variables, and google. Somehow the system must know besides looking at where the shortcut on the desktop points (I think).

  11. Keith, I’m not aware that the My Documents folder is associated with an environment variable. You should be able to use %userprofile%\My Documents, which will find the default location for the logged-on user. But this will fail if you’ve moved the shell folder using Tweak UI.

  12. Is there a way to use %random% or %time% within the shortcut command line? %username% substitutes but %time% doesn’t.
    They do if you put them in a batch file, but then we get a dos window while our application runs.

    Iain

  13. I know it probably is possible and if so, What are the commands I can use to create a batch file that will copy a set of files from a networked server and write them to a Rewritable?

    Scripting Newbe

Comments are closed.