Windows System environment variable, changes not getting reflected in CMD prompt (VBScript)

Send Us a Sign! (Contact Us!)
Word PDF XPS XML
TXT

With Restart

You could add or modify an environment variable creating or modifing a registry key in Windows, using the following simple script, for example, we will add the environmet variable named CATALINA_HOME with value C:\Tomcat5 to the system:

set WshShell = CreateObject("WScript.Shell") 

WshShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\CATALINA_HOME", "C:\Tomcat5" , "REG_EXPAND_SZ"

The drawback will be that this variable will not be available to %COMSPEC% (or simply CMD) through the SET command. To let this change active you will have to restart your computer.

[tweet]

Without Restart

Actually, adding/modifying environment variable using registry is a bit raw way. We might be creating some inconsistencies by doing so.

So, the ideal way of doing is by using the collection which is dedicated to the environment variables, WshEnvironment.

Now write the following [gs script] to add system environment variable:

Set wshshell = CreateObject("WScript.Shell") 

Dim WshSySEnv
Set WshSysEnv = wshshell.Environment("SYSTEM")
WshSysEnv("1VINOD") = "1Vinod"
WshSysEnv("2VINOD") = "2Vinod"
Set WshSySEnv = Nothing

Save this code in vbs file, execute the vbs [gs file]. Now, you will get the 2 environment variables in cmd prompt without restarting the system.

Similar script for removing the [gs variable]s:

Set wshshell = CreateObject("WScript.Shell") 

Dim WshSySEnv
Set WshSysEnv = wshshell.Environment("SYSTEM")
WshSysEnv.Remove("1VINOD")
WshSysEnv.Remove("2VINOD")
Set WshSySEnv = Nothing

This also does not require any restarts/[gs logon]-[gs logoff]s.

Enjoy!

SOURCE

LINK

LANGUAGE
ENGLISH

1 thought on “Windows System environment variable, changes not getting reflected in CMD prompt (VBScript)”

Comments are closed.