Click here to Skip to main content
13,288,520 members (55,843 online)
Click here to Skip to main content
Add your own
alternative version


94 bookmarked
Posted 3 Nov 2005


, 11 Feb 2008
Rate this:
Please Sign up or sign in to vote.
A free tool for setting/updating/deleting System Environment Variables.
Screenshot - SetEnv.gif


I recently used the excellent Inno Setup utility to create some install scripts for my own applications. I discovered that there was no way to set a system-wide environment variable except by using an external application. SetEnv supports User and System environment variables.

I began by searching for the Microsoft setex tool, but couldn't find it. I didn't really try that hard though. I thought I'd write my own, hence this article.
You can find a proper setup kit for SetEnv on my website.

System Environment Variables

The method used to create a system-wide environment is dependent upon the operating system in use. SetEnv will automatically detect this and will use one of the following techniques to create/modify or delete environment variables.

Windows 95/98/ME

Under Windows 9x, creating an environment variable requires modifying the user's autoexec.bat file and then executing it or rebooting before the variable is recognized by the operating system. SetEnv will automatically locate the autoexec.bat file itself -- as long as the file is on the C:\ or D:\ drive -- and then add or modify the selected system variable. The one thing SetEnv will not do is reboot the PC, as it should be left up to the user or the Setup Kit -- from Inno Setup, for example -- to choose when to perform the reboot.

Windows XP/2000/2K3/Vista

Under more modern (i.e. proper) operating systems such as Windows 2000, XP, and Windows 2003 Server, environment variables are stored in the registry under the following key:

    Control\Session Manager\Environment

Variables are added by creating a new value under this key or by modifying a value if it already exists. To delete a variable, we simply delete its registry value. That is, unless we are removing part of an expanded value such as PATH, in which case we only remove the part we want.

At this point, Windows will not be aware of our changes unless we log off or reboot. To get around this, SetEnv will broadcast a WM_SETTINGCHANGE to all of the Windows in the system. This allows other running applications such as Explorer.exe to be notified of our change. If you run SetEnv from a command prompt, then this will not update the environment variable for the current DOS window. This is mainly due to the fact that a process (SetEnv) cannot change the environment of its parent, the command prompt. However, any new DOS/command prompt that you open will show the new variable/value.

Broadcasting this message results in a slight delay of around 2-3 seconds, whilst the open Windows process it. So it may appear that SetEnv has hung, but this is not the case.

Using SetEnv

SetEnv is very easy to use and has only a few command line arguments. This section will describe how to use them. Typing SetEnv at a command prompt and then pressing the Return key will make SetEnv display its usage information. This can be seen in the screenshot at the top of this page.

User Environment Variables

As of version 1.04, SetEnv also supports User environment variables. If you want SetEnv to either add or delete a User variable, then add the -u option to the command line.

Creating a Variable

SetEnv can create two types of variables, a simple one that has only a single value such as:

InstallPath = C:\Program Files\Xanya\_Bin

... or more complex variables with multiple values. A good example of this is the PATH variable:

PATH = C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Xanya\_Bin

To create a simple variable, just enter in the following command line. Obviously, substitute your own variable name and value:

SetEnv -a InstallPath "C:\Program Files\Xanya\_Bin"

Similarly, to add a variable with multiple values, you need to type the following command line for each value. Ensure that you prefix the value with the % character:

SetEnv -a name %value


SetEnv -a PATH %"C:\Program Files\Xanya\_Bin
SetEnv -a PATH %"C:\Bin

To add a User variable instead of a System one, add the -u option as in the following example:

SetEnv -ua PATH %"C:\Program Files\Xanya\_Bin

Modifying an Existing Variable

You modify variables in exactly the same way in which you would create them. If you are modifying a multi-value variable and you forget the % prefix to the value, then SetEnv will automatically detect that the destination has multiple values and will modify it correctly.

Dynamic Variable Expansion

This allows you to set a variable to always equal the value of an existing environment variable, even if that variable changes. Let me show you an example of when this would be very useful. This example was inspired by Synetech; if you mind me stealing it just let me know.

Say that you have an environment variable called MEDIADIR (set to C:\Media) and you want to create two additional variables called MUSICDIR and PICDIR (set to %MEDIADIR%\Music and %MEDIADIR%\Pics, respectively). These would be expanded by the operating system to the following:


Now, say you decided to move the MEDIADIR folder to drive D (D:\Media) using dynamic variable expansion. SetEnv will ensure that your MUSICDIR and PICDIR variables are correctly relocated to the following automatically.


To enable dynamic variable expansion, simply create your variables as in the following example. Use the ~ symbol to identify the existing variable name.

setenv -a MEDIADIR C:\Media
// Create the original variable

// Create our dynamic variable, when MEDIADIR changes, so will MUSICDIR
setenv -a MUSICDIR ~MEDIADIR~/Music
// Our second dynamic variable, PICDIR will change if MEDIADIR changes
setenv -a PICDIR ~MEDIADIR~/Pics

Deleting a Variable

To delete a variable, specify the -d option instead of the -a option, as in the following example:

SetEnv -d InstallPath

To delete a value from a multi-value variable, you simply enter the following, specifying the value to remove:

SetEnv -d PATH %"C:\Program Files\Xanya\_Bin

As with modifying a multi-value variable, if you forget to specify the % prefix, then SetEnv will automatically work this out and delete the specified value only. To delete a User environment variable, simply add the -u option as follows:

SetEnv -ud PATH %"C:\Program Files\Xanya\_Bin

Running SetEnv from a Batch File

SetEnv can be run successfully from a batch file. However, a problem was found by David Langford, which occurs when you attempt to specify the expanded variable prefix % to a value which contains a drive letter, as in the following example. Note that this is only an issue when running SetEnv from a batch file:

SetEnv -d PATH %C:\Test

The Windows batch file interpreter will interpret the % character as a prefix to a variable. It will replace the character with what it believes is the variable %C: with the contents of that variable, which of course is nothing. This results in SetEnv being passed a modified value, as shown here:

SetEnv -d PATH \Test

This is obviously wrong, and SetEnv will not be able to match the value to be removed in the expanded variable. It will thus fail to delete the value from the variable. The workaround is to escape the % character with another % character. The following example will correctly remove the C:\Test value from the expanded variable, PATH.

SetEnv -d PATH %%C:\Test


There are no known bugs, but if you do find any, then please let me know.


  • 1.09 (Feb 9, 2008) - Fixed a problem on Windows 98 where it sometimes failed to open the Autoexec.bat file (Found by mulinux).
  • 1.08 (May 31, 2007) - Updated SetEnv's usage information on how to delete a user environment variable using SetEnv. The code has not changed.
  • 1.07 (Jan 25, 2007) - Fixed a bug found by depaolim
  • 1.06 (Jan 14, 2007) - Added dynamic expansion support (same as using ~ with setx), originally requested by Andre Amaral, additional request by Synetech.
  • 1.05 (Sep 06, 2006) - Added support to prepend (rather than append) a value to an expanded string, requested by Masuia.
  • 1.04 (May 30, 2006) - Added support for User environment variables.
  • 1.03 (May 17, 2006) - Updated the article to explain how to use SetEnv from a batch file, after a problem was found by David Langford.
  • 1.03 (Apr 20, 2006) - Bug fix in ProcessWinXP() discovered by attiasr.
  • 1.01 (Nov 15, 2005) - Bug fix in IsWinME() discovered by frankd.
  • 1.00 (Oct 29, 2005) - Initial public release


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jonathan [Darka]
Architect Elekta Limited
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralRe: Delete a User Variable Pin
Jonathan [Darka]31-May-07 10:31
memberJonathan [Darka]31-May-07 10:31 
QuestionProblem with empty value Pin
depaolim25-Jan-07 4:12
memberdepaolim25-Jan-07 4:12 
AnswerRe: Problem with empty value Pin
Jonathan [Darka]25-Jan-07 4:41
memberJonathan [Darka]25-Jan-07 4:41 
GeneralRe: Problem with empty value Pin
depaolim25-Jan-07 5:11
memberdepaolim25-Jan-07 5:11 
AnswerRe: Problem with empty value Pin
Jonathan [Darka]25-Jan-07 5:04
memberJonathan [Darka]25-Jan-07 5:04 
GeneralRe: Problem with empty value Pin
depaolim25-Jan-07 5:20
memberdepaolim25-Jan-07 5:20 
GeneralVersion 1.06 Pin
Jonathan [Darka]17-Jan-07 5:58
memberJonathan [Darka]17-Jan-07 5:58 
News?/! Adding Environment Variables to Variables Pin
Synetech17-Dec-06 19:16
memberSynetech17-Dec-06 19:16 

First off, let me say that I am glad and impressed to see that you went beyond the basics and made the effort to support "extras" like expandable strings. I have seen far too many tools written by narrow-minded/short-sighted developers who ignore things like this, especially when it comes to the registry, which limits their usefulness. Congrats. (You're probably thinking "So what, who doesn't?", and that's my point. It may be obvious to you and me and a precious few others, but a lot of developers aren't even aware of capabilities such as this, and most of the ones who are, don't bother to use them.)

Having said that, there does not seem to be a native (built-in) way to have the tool put an unexpanded variable into another variable. For example, say that I've got a variable called MEDIADIR set to c:\media. I now want to create two new variables MUSICDIR and PICDIR set to %mediadir%\music and %mediadir%\pics respectively, such that if I change MEDIADIR, the other variables reflect that. Using the following command:
setenv -a MUSICDIR %mediadir
Will as expected create an expandable string environment variable called MUSICDIR but it contains mediadir.

There is a way to hack it on the command line:
setenv -a MUSICDIR %^%MEDIADIR^%\music
setenv -a PICDIR %^%MEDIADIR^%\pic
or in a batch file:
setenv -a MUSICDIR %%%%MEDIADIR%%\music
setenv -a PICDIR %%%%MEDIADIR%%\pic
However if you use the tool more than once, the value is appended to the variable which is fine for the PATH, but that's pretty much it.

Microsoft's SETX tool allows you to accomplish the above task like this:
setx MUSICDIR ~MEDIADIR~\music -m
setx PICDIR ~MEDIADIR~\pics -m
Of course, then you can't use the tilde in your variables. I'm sure there's a better way but both work (albeit differently) so I guess it's up to the user for now.


GeneralRe: ?/! Adding Environment Variables to Variables Pin
Synetech17-Dec-06 19:37
memberSynetech17-Dec-06 19:37 
GeneralRe: ?/! Adding Environment Variables to Variables Pin
Jonathan [Darka]1-Jan-07 3:09
memberJonathan [Darka]1-Jan-07 3:09 
GeneralRe: ?/! Adding Environment Variables to Variables Pin
Synetech1-Jan-07 7:05
memberSynetech1-Jan-07 7:05 
GeneralRe: ?/! Adding Environment Variables to Variables Pin
Jonathan [Darka]17-Jan-07 5:49
memberJonathan [Darka]17-Jan-07 5:49 
GeneralRe: ?/! Adding Environment Variables to Variables Pin
Synetech18-Jan-07 6:15
memberSynetech18-Jan-07 6:15 
GeneralRe: ?/! Adding Environment Variables to Variables Pin
Jonathan [Darka]18-Jan-07 7:56
memberJonathan [Darka]18-Jan-07 7:56 
QuestionHow about auto remove invalid path entries? Pin
Sorin Sbarnea3-Nov-06 1:29
memberSorin Sbarnea3-Nov-06 1:29 
AnswerRe: How about auto remove invalid path entries? Pin
Jonathan [Darka]7-Nov-06 21:59
memberJonathan [Darka]7-Nov-06 21:59 
GeneralREGKEY C++ source code usage Pin
gegematic17-Oct-06 2:56
membergegematic17-Oct-06 2:56 
GeneralRe: REGKEY C++ source code usage Pin
Darka17-Oct-06 3:25
memberDarka17-Oct-06 3:25 
GeneralRe: REGKEY C++ source code usage Pin
gegematic17-Oct-06 5:50
membergegematic17-Oct-06 5:50 
GeneralRe: REGKEY C++ source code usage Pin
Darka17-Oct-06 5:58
memberDarka17-Oct-06 5:58 
GeneralRe: REGKEY C++ source code usage Pin
gegematic17-Oct-06 6:40
membergegematic17-Oct-06 6:40 
GeneralRe: REGKEY C++ source code usage Pin
Darka17-Oct-06 7:54
memberDarka17-Oct-06 7:54 
Generalut Pin
gegematic17-Oct-06 23:52
membergegematic17-Oct-06 23:52 
GeneralRe: ut Pin
Darka18-Oct-06 0:00
memberDarka18-Oct-06 0:00 
QuestionPrepend in front of existing multi-value variable Pin
Masuia5-Sep-06 3:31
memberMasuia5-Sep-06 3:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 11 Feb 2008
Article Copyright 2005 by Jonathan [Darka]
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid