How to change PERMANENTLY environment variables from batch script?

C

Cindy Parker

Ok, I know. In a batch file I can change environment variables temporarily (=for the
current, remaining session) be entering e.g.

set CLASSPATH=D:\newpath;%CLASSPATH%

However these changes are not visible outside of the current session and furthermore they
are lost after a reboot.

Is there a(nother) way to change them PERMANENTLY from batch script?

I can imagine that there is a way by a regedit or VisualBasic script or 3rd party cmdline tool.

Again: I don't want to edit them manually e.g. in the "System" dialog in control panel.
The change should take place on cmdline from a script

Cindy
 
J

John John - MVP

Cindy said:
Ok, I know. In a batch file I can change environment variables temporarily (=for the
current, remaining session) be entering e.g.

set CLASSPATH=D:\newpath;%CLASSPATH%

However these changes are not visible outside of the current session and furthermore they
are lost after a reboot.

Is there a(nother) way to change them PERMANENTLY from batch script?

I can imagine that there is a way by a regedit or VisualBasic script or 3rd party cmdline tool.

Again: I don't want to edit them manually e.g. in the "System" dialog in control panel.
The change should take place on cmdline from a script

Use SetX (a Resource Kit tool).

John
 
J

John John - MVP

Cindy said:
Ok, I know. In a batch file I can change environment variables temporarily (=for the
current, remaining session) be entering e.g.

set CLASSPATH=D:\newpath;%CLASSPATH%

However these changes are not visible outside of the current session and furthermore they
are lost after a reboot.

Is there a(nother) way to change them PERMANENTLY from batch script?

I can imagine that there is a way by a regedit or VisualBasic script or 3rd party cmdline tool.

Again: I don't want to edit them manually e.g. in the "System" dialog in control panel.
The change should take place on cmdline from a script

You can use the Resource Kit's SetX for this.

John
 
M

Marcello

REG ADD "HKLM\SYSTEM\ControlSet001\Control\Session Manager\Environment" /v
MyVar /t REG_SZ /d "Good Morning"

(Restart required.)

Lg
Marcello
 
J

John John - MVP

Should be put in the CurrentControlSet, for all we know the
ControlSet001 could be a failed control set... Placing it in the
CurrentControlSet will ensure that it is written to the proper
corresponding nnn Control Set.

John
 
P

Pegasus [MVP]

Cindy Parker said:
Ok, I know. In a batch file I can change environment variables temporarily
(=for the
current, remaining session) be entering e.g.

set CLASSPATH=D:\newpath;%CLASSPATH%

However these changes are not visible outside of the current session and
furthermore they
are lost after a reboot.

Is there a(nother) way to change them PERMANENTLY from batch script?

I can imagine that there is a way by a regedit or VisualBasic script or
3rd party cmdline tool.

Again: I don't want to edit them manually e.g. in the "System" dialog in
control panel.
The change should take place on cmdline from a script

Cindy

Here are a couple of options:
- setx.exe (Win2000 Resource Kit)
- setenv.exe (ftp://barnyard.syr.edu/pub/vefatica/setenv.exe)

Note that with either tool the change will only affect processes launched
*after* you issue this command. It has no effect on pre-existing processes.
 
T

Twayne

In
Cindy Parker said:
Ok, I know. In a batch file I can change environment
variables temporarily (=for the
current, remaining session) be entering e.g.

set CLASSPATH=D:\newpath;%CLASSPATH%

However these changes are not visible outside of the
current session and furthermore they
are lost after a reboot.

Is there a(nother) way to change them PERMANENTLY from
batch script?

I can imagine that there is a way by a regedit or
VisualBasic script or 3rd party cmdline tool.

Again: I don't want to edit them manually e.g. in the
"System" dialog in control panel.
The change should take place on cmdline from a script

Cindy

Try Google or your favorite Seach Engine, whatever it might be. Maybe:
batch "permanent environment" +change
or similar.
 
B

+Bob+

Ok, I know. In a batch file I can change environment variables temporarily (=for the
current, remaining session) be entering e.g.

set CLASSPATH=D:\newpath;%CLASSPATH%

However these changes are not visible outside of the current session and furthermore they
are lost after a reboot.

Is there a(nother) way to change them PERMANENTLY from batch script?

I can imagine that there is a way by a regedit or VisualBasic script or 3rd party cmdline tool.

Again: I don't want to edit them manually e.g. in the "System" dialog in control panel.
The change should take place on cmdline from a script

Cindy

You can run a command line update to the registry from a script to
change the path that is stored there. You run it form the command line
or a batch file with a command like this:

C:\Windows\regedit.exe /s c:\myfile.reg

myfile has to contain a reg key in text format, something like this:

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session
Manager\Environment]
"CLASSPATH"=".;C:\\Program Files\\Java\\jre6\\lib\\ext\\QTJava.zip"

Unfortunately that's not the key and value you want to set. It's
"PATH" and it's stored as a hex value, not text. So, creating the .reg
file to add would be a chore. If you want to set a fixed path for the
system, you could just set it manually, export the key to get the .reg
file you need, then use it in the future. But, if you need to add your
directory to the end of whatever is on the system currently, that
won't work.

I'd suggest you take a look towards the bottom of this page. There are
some other alternatives.

http://vlaurie.com/computers2/Articles/environment.htm#editing
 

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