Changing system environment variables from the Windows command line

Published: | by Julian Knight Reading time ~2 min.
📖 Posts | 📎 Windows | 🔖 Batch, Configuration, Scripting, VBS

There are several ways to change global or user environment variables manually in Windows. Most are well known so I wont repeat them here (e.g. in Vista or Windows 7, Control Panel/User Accounts, Change my environment variables). However, sometimes you want to do this from a command (aka script or batch) file. This is not as straightforwards as it might seem. That’s because if you simply set the variable – e.g. set FRED=JimBob – it is only set while you are in that command file. Once the script has finished, the variable will no longer be set. There are a number of examples of setting system or user environment variables available if you do a Google search but most of them are incomplete – they do not immediately make the new value available to all applications (and particularly to new command shells). To make sure that the new value is available system-wide, you have to tell Windows to refresh the environment variable list and the easiest way to ensure this happens is to change the variable from a [Windows Scripting Host][1] ([WSH][2]) script. Here is an example script to do this. Save this file as something like set-env.vbs somewhere convenient.

Note that in Vista or above, you will need to run the command file with elevated privileges for this to work. The normal command prompt gets this automatically but if you want to run the file from Windows Explorer, you will need to create a shortcut and change the settings. You can now use this in your batch command scripts, for example:

Technorati : Batch, Configuration, Scripting, VBS, Windows Diigo Tag Search : Batch, Configuration, Scripting, VBS, Windows


comments powered by Disqus