Link to home
Start Free TrialLog in
Avatar of bjshallenberger
bjshallenbergerFlag for United States of America

asked on

How to set User Environment variable in vbscript and use it with Command Shell 'set' command

I have the following code in a vbscript:

Set WshShell = WScript.CreateObject("Wscript.Shell")
Set WshUserEnv = WshShell.Environment ("USER")

WshUserEnv("Test") = "123"

When I login the user, the variable 'Test' appears in the user's list of variables in the 'MyComputer'->'Properties'->'Advanced'->'Environment Variables' window.  However, if I type 'set' from the user's command prompt, the variable does not show up in the list (echo %Test% results in %Test% being echoed back).  

Is there something more I should do?

Thanks,
Brian
Avatar of bjshallenberger
bjshallenberger
Flag of United States of America image

ASKER

Well,  the variable appeared (some 1 hour later) without changes to the system or code. It was viewable from the 'set' command from a command prompt.  Essentially, it could be used immediately if we wanted to use it read it from a vbscript (once set) as it is defined in Windows Env Variable.  However, how can I have it appear more immediately from the command shell?  I don't understand the delay in the user variable being defined and not readily available to the user on the next login.  Is there some sort of command to affect the change immediately...much like a gpupdate command to force an refresh of GPO changes?
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial