Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I SET an enviroment variable iin WSH / VBScript

1,162 views
Skip to first unread message

Jeff Stockamp

unread,
Jun 27, 2000, 3:00:00 AM6/27/00
to
You can use WScript.Shell and use the set command.

- Jeff

"Peter Grant" <peter...@unisys.com> wrote in message
news:8jbfo9$r1i$1...@mail.pl.unisys.com...
> Trying to set the variable %TMP% to the output of a file called siteid
> (which = 400)
> how do i do this?
>
>
>

Peter Grant

unread,
Jun 28, 2000, 3:00:00 AM6/28/00
to

GBorn

unread,
Jun 28, 2000, 3:00:00 AM6/28/00
to
here are a few statements demonstrating how to read and set environment
variables:

Set WshShell = CreateObject ("WScript.Shell")
' get the collection using the Environment property
Set objEnv = WshShell.Environment("Process")
' create a (temporary) environment variable
objEnv("Born") = "Hello World!"
' Read environment variables
Message = ""
For Each i In objEnv
Message = Message & i & vbCRLF
Next
WScript.Echo Message

But keep in mind that creating environment variables from a script
creates only temporary values (the values are getting lost after terminating
the script).

G. Born
_________________________________________
Check out the WSH Bazaar at www.borncity.com

"Peter Grant" <peter...@unisys.com> schrieb im Newsbeitrag
news:8jbfo9$r1i$1...@mail.pl.unisys.com...

Peter Grant

unread,
Jun 28, 2000, 3:00:00 AM6/28/00
to
Thanks for your response. I'm configuring an NT post installation of 360+
servers. I'm using ScriptIT to modify printer names from xxxLEX1 to
%var%LEX1 where %var% is the variable Im trying to set. In ScriptIT you can
specify enviroment variables however as you said using the way below it gets
lost when the script ends. I was thinking of using the Env variable TMP but
how can you do this without using the GUI (System in Control Panel)?

Any Ideas??

Rdgs

"GBorn" <GB...@borncity.de> wrote in message
news:eD502UM4$GA.76@cppssbbsa05...

David Hamel

unread,
Jun 28, 2000, 3:00:00 AM6/28/00
to
If all your systems are NT then you can make the environment variables
permanent.

Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshProcessEnv = WshShell.Environment("PROCESS")
Set WshUserEnv = WshShell.Environment("USER")
'SYSTEM not valid on Win2k for TMP
Set WshSysEnv = WshShell.Environment("SYSTEM")
'if you write %SystemDrive% with WSH you will get %SystemDrive% not c:\
'so all variables must be expanded before being written to the environment
'WshShell.ExpandEnvironmentStrings("%SystemDrive%\TEMP")

WshProcessEnv("Test") = "Hello"
WshUserEnv("Test") = "Hello"
WshSysEnv("Test") = "Hello"
MsgBox "Process: " & WshProcessEnv("Test") & _
vbCRLF & _
"User: " & WshUserEnv("Test") & _
vbCRLF & _
"System: " & WshSysEnv("Test")

'Cleanup
WshProcessEnv.Remove("Test")
WshUserEnv.Remove("Test")
WshSysEnv.Remove("Test")

'If you want to run a program from your script
'with the new environment you need to set
'Process environment and User (User to make it permanent)
'If you don't set the Process environment then you will run
'the program with whatever environment your script started in


David Hamel

Peter Grant <peter...@unisys.com> wrote in message

news:8jcjhe$7aa$1...@mail.pl.unisys.com...

Dov Ramon

unread,
Jul 27, 2000, 3:00:00 AM7/27/00
to
I'm not sure, but I think that all the variables are stored in the
registery, in:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session
Manager\Environment
If you'll add your variable there, it will be permanent, and not get lost
when the script ends. And it's not very difficult to add values to the
registery using WSH/VBS, isn't it?
0 new messages