Google Answers Logo
View Question
 
Q: Windows XP: Setting environment variables via windows scripting ( No Answer,   1 Comment )
Question  
Subject: Windows XP: Setting environment variables via windows scripting
Category: Computers > Programming
Asked by: marvingreenberg-ga
List Price: $12.00
Posted: 24 Jul 2003 08:59 PDT
Expires: 31 Jul 2003 07:04 PDT
Question ID: 234631
I need to know how, using windows scripting (preferably jscript) to
set environment variables such that the new values are immediately
available for new commands that are run.

I have a windows script (javascript) that sets environment variables
in the registry.  Here is a relevant bit:

var shell = WScript.CreateObject("wscript.shell");
// Set some string variables OMhome and append to the path
shell.RegWrite ("HKCU\\Environment\\TENA_OM_HOME",  OMhome, "REG_SZ"
);
shell.RegWrite ("HKCU\\Environment\\PATH",  path, "REG_SZ" );

Although this sets the values in the registry, the values are not
added to the environment until a reboot or until you view the current
environment using MyComputer/Properties/Advanced/Environment and click
OK.  This is verified by opening a CMD.EXE shell and typing set PATH.

Is there a scripting command to explicitly force Windows (Explorer?)
to reread a particular environment variable from the registry?
Answer  
There is no answer at this time.

Comments  
Subject: Re: Windows XP: Setting environment variables via windows scripting
From: cleverclever-ga on 24 Jul 2003 10:39 PDT
 
Set An Environment Variable Via WMI
From a Google post.

Note: this works great for modifying a variable for all Terminal
Server sessions as well.  The example below creates a variable named
%RouterStatus% with the value of UP in the SYSTEM  environment on a
server named Mort.  - AKA

SetWmiEnvVar "MORT", "<SYSTEM>", "RouterStatus", "UP"

Sub SetWmiEnvVar( Host, sContext, sVarName, sValue)
 ' Sets an environment variable on an arbitrary host via WMI
 ' NOTE: This will of course not affect currently running
 ' processes.
 ' For a systemwide value, use <SYSTEM> as context
 ' for default user, use <DEFAULT>
 ' otherwis,e use the specific user's name
 ' Get the class object itself
 Dim EnvClass, EnvVarInst
 Set EnvClass = GetObject("WinMgmts://" & Host _
  & "/root/cimv2:Win32_Environment")
 ' Make a new instance of that class
 Set EnvVarInst = EnvClass.SpawnInstance_
 ' File in the key props and props of interest on that instance
 EnvVarInst.UserName = sContext
 EnvVarInst.Name = sVarName
 EnvVarInst.VariableValue = sValue
 ' Write the new instance in to WMI
 EnvVarInst.Put_
End Sub

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy