• ARTICLES
SEARCH

How-To Geek

How to Use Global System Environment Variables in Windows


Any system administrator who spends a good bit of time in the command prompt or batch scripts is probably well aware of built in environment variables Windows offers (i.e. Path, WinDir, ProgramFiles, UserProfile, etc.). If you find yourself using a particular value over and over, wouldn’t it be great if you had your own variable which you can use the same way as the built in values?

With a few clicks, you can create and maintain you own environment variables which are both global on the system and survive reboots.

Creating a Custom System Environment Variable

Creating a new global system variable is quite simple and is one of those features hiding in plain sight. Please note the screenshots are for Windows Server 2008, however the process for most versions of Windows is almost identical with only a few of the screens different.

In the Control Panel, open the System option (alternately, you can right-click on My Computer and select Properties). Select the “Advanced system settings” link.

image

In the System Properties dialog, click “Environment Variables”.

image

In the Environment Variables dialog, click the New button underneath the “System variables” section.

image

Enter the name of your new variable as well the value and click OK.

image

You should now see your new variable listed under the “System variables” section. Click OK to apply the changes.

image

You can now access your new system environment variable like you would any other. You can use it from the command line or batch scripts without having to define it.

image

Using the Custom Environment Variable

As stated above, your custom environment variable is no different than any other system variable as you can reference it from the command line and inside of scripts. For a quick example, consider this batch script:

@ECHO OFF
TITLE Global Environment Variable Test
ECHO.
ECHO System NotifyEmail value
ECHO NotifyEmail = %NotifyEmail%
ECHO.
SETLOCAL
ECHO Overriding global variable in this script…
SET NotifyEmail=jfaulkner@otheremail.com
ECHO NotifyEmail = %NotifyEmail%
ECHO.
ECHO Exiting override script…
ENDLOCAL
ECHO.
ECHO System NotifyEmail value
ECHO NotifyEmail = %NotifyEmail%
ECHO.
ECHO.
ECHO.
PAUSE

When executed, the output is exactly what you would expect:

image

Usage Ideas

The real power of custom environment variables¬† enters when you use them in your scripts. In our example, we set a variable called “NotifyEmail” which we could reference in any number of scripts without having to hard code the value. So in the event we need to change the email address, we simply update the system variable and the impacted scripts will use this new value without us having to update each script individually.

This is not only a time saver, but also protects against the situation where you forget to update a particular script and a “dead” value is being used. Additionally, in the event you need to override a system variable in a particular script, you can see in our example above this is fully supported.

Here are some ideas where you could apply system variables in place of local scope variables:

  • Email addresses (like in our example)
  • Backup folder locations
  • URL and FTP sites
  • Metric and threshold values

Another great feature about using system variables is you have a single place where you can edit or view your variable values. Simply put, you could potentially apply updates to multiple scripts by editing the environment variables in a single location.

Jason Faulkner is a developer and IT professional who never has a hot cup of coffee far away. Interact with him on Google+

  • Published 11/16/10

Comments (2)

  1. trm96

    I used to use %sys32% for C:windowssystem32

  2. Two Replies

    SETX can be used to set those global values from the prompt (useful for persisting values from one script instance to another).

    But one thing that I’ve always wrestled with is that once a command window is opened, it seems that the enviromental variables are loaded from their global state at THAT point.
    Meaning if any global values change in other instances, those values won’t be reflected in any open/existing command windows/scripts.
    Linux has a command that will refresh the current prompt’s globals (it escapes me at the moment), but I don’t know if something similar exists for windows.

    re: trm96
    Type “SET” (with no arguments) into the command prompt to see all the set variables. There are LOTS more you can use.

Enter Your Email Here to Get Access for Free:

Go check your email!