Changing system environment variables from the Windows command line

Published: | by Julian Knight Reading time ~3 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 (WSH) script. Here is an example script to do this. Save this file as something like set-env.vbs somewhere convenient.

‘Set/Change/Delete An Environment Variable
‘  In this simple script, we mainly assume that the variable space is USER.
‘  The variable is not only changed for the calling script, it also forces a system-wide
‘    refresh of the lists so the created/updated/deleted variable is immediately available
‘    to all applications.
‘  With 3 arguments
‘    Creates or changes the named variable in the in the given environment variable list
‘    set-env.vbs <variable type> <variable name> <assigned value>
‘  With 2 arguments - Assumes the variable type is USER
‘    Creates or changes the named variable in the current USER’s environment variable list
‘    set-env.vbs <variable name> <assigned value>
‘  With 1 argument - Assumes the variable type is USER
‘    Deletes the named variable from the current USER’s environment variable list
‘    set-env.vbs <variable name>
‘  With no arguments
‘    No action taken
‘  <variable type> is one of
‘    “System” (HKLM),
‘    “User”   (HKCU),
‘    “Volatile” or “Process”
‘  Julian Knight,
‘  Permission is given to reuse this code in any way desired
‘  No support is given or implied for this code
Set WSHShell = WScript.CreateObject(“WScript.Shell”)
‘ Get arguments
Set oArgs=WScript.arguments
Select Case oArgs.Count
        case 3
                ‘ If we have 3 arguments, assume <type> <name> <value> and create/change
                WSHShell.Environment(oArgs.item(0)).item(oArgs.item(1)) = oArgs.item(2)
                WScript.Echo “Created or Changed Environment Variable: “ & oArgs.item(0) & “, ” & oArgs.item(1) & “, ” & oArgs.item(2)
        case 2
                ‘ If we have 2 arguments, assume <name> <value> and create/change in USER space
                WSHShell.Environment(“USER”).item(oArgs.item(0)) = oArgs.item(1)
                WScript.echo “Created or Changed Environment Variable: USER, “ & oArgs.item(0) & “, ” & oArgs.item(1)
        case 1
                ‘ If we have 1 arg, assume <name> and delete in USER space
                WScript.echo “Deleted Environment Variable: USER, “ & oArgs.item(0)
        case Else
                ‘ Otherwise do nothing
                WScript.echo “No action”
End select
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:
@REM change http_proxy variable for current user (forces change & refreshes system so new setting is immediately available to everything)
@cscript.exe //nologo C:\set-env.vbs http_proxy http://localhost:3128
@choice /T 5 /D y /N /M “Switch complete, now using local proxy”

Technorati : , , , ,
Diigo Tag Search : , , , ,

comments powered by Disqus