Permanent changes to env vars from a batch file?

A

Angel Tsankov

How do I set an environment variable in a batch file and make the new value permanent across system restarts?
 
M

Mark V

In said:
How do I set an environment variable in a batch file and make
the new value permanent across system restarts?

SETX.EXE from the Resource Kit is one way.
One might also edit the registry from the commandline (.REG file
merge or REG.EXE). One thing you need to know is whether this is a
System or User environment change.
 
A

Angel Tsankov

Mark V said:
SETX.EXE from the Resource Kit is one way.
One might also edit the registry from the commandline (.REG file
merge or REG.EXE). One thing you need to know is whether this is a
System or User environment change.

OK, if I choose to modify the registry via a reg file how do I apply the changes (from a batch file again) without restarting the
system.
 
P

Paul R. Sadowski [MVP]

Hello, Angel:
On Sun, 7 Aug 2005 09:55:12 +0300: you wrote...

??>> In microsoft.public.win2000.cmdprompt.admin Angel Tsankov wrote:
??>>
??>>> How do I set an environment variable in a batch file and make
??>>> the new value permanent across system restarts?
??>>
??>> SETX.EXE from the Resource Kit is one way.
??>> One might also edit the registry from the commandline (.REG file
??>> merge or REG.EXE). One thing you need to know is whether this is a
??>> System or User environment change.
AT>
AT> OK, if I choose to modify the registry via a reg file how do I apply
AT> the changes (from a batch file again) without restarting the system.

Here's how you can set env vars using a WSH script. But neither will be
available to the current instance of the command interpreter.
'SetSomeVars.vbs
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshSysEnv = WshShell.Environment("SYSTEM")
WshSysEnv("MySysVar") = "123"
Set WshProcessEnv = WshShell.Environment("USER")
WshProcessEnv("MyVar") = "456"

Set WshSysEnv = Nothing
Set WshProcessEnv = Nothing
Set WshShell = Nothing

Regards, Paul R. Sadowski [MVP].
 
S

Stefan Kanthak

Angel Tsankov said:
OK, if I choose to modify the registry via a reg file how do I apply the changes (from a batch file again) without restarting the
system.

Please break your lines at about 70 chars.

%SystemRoot%\System32\RUNDLL32.EXE %SystemRoot%\System32\USER32.DLL,UpdatePerUserSystemParameters

should do the job. But I won't rely on it. And it's clumsy to have
a *.REG and some command lateron to "activate" the changes.
Better write an *.INF instead of the *.REG, and "install" it with

%SystemRoot%\SYSTEM32\RUNDLL32.EXE %SystemRoot%\SYSTEM32\ADVPACK.DLL,LaunchINFSection %1,<your section>

Stefan
 
M

Mark V

In said:
Please break your lines at about 70 chars.

%SystemRoot%\System32\RUNDLL32.EXE
%SystemRoot%\System32\USER32.DLL,UpdatePerUserSystemParameters
[ ]

I use (W2K) ... ,UpdatePerUserSystemParameters ,1 ,True
Although I do not know the differences between
,UpdatePerUserSystemParameters
and
,UpdatePerUserSystemParameters ,1 ,True

Anyone explain?
 
M

Mark V

In said:
OK, if I choose to modify the registry via a reg file how do I
apply the changes (from a batch file again) without restarting
the system.

It seems Paul and Stephan have covered it for the most part. Still
have questions?

I seem to recall a small compiled utility that dos much the same
"update" function, but do not recall either the name or source.

You still have not said whether a User of System variable. If a
System variable and if the process that uses it is already running
or it is used during system startup, you may *have* to restart the
system for effect.

And BTW I'd push for using reg.exe (if SETX is not available) and
not messing with .reg files. (Reg.exe is in the W2K Support Tools
and ships by default in XP (Pro anyway). The registry location may
require Administrators access.

Should this be PATH related, other tools are available.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top