### Using Scripts to Set Global Environment Variables
Recently, I've noticed that many people are discussing how to use scripts to set system global environment variables that are long - term and effective. Coincidentally, I've also used this kind of content recently, so I've collected and sorted out everyone's discussion content, and thus this article is formed.
1. Internal tools: regedit /s, echo >> & regedit /s, reg add
2. External tools: setx, setntenvar, winset
3. Other scripts: vbs
4. Automatic batch processing: autoexec.bat, autoexec.nt
===============================================
1. Internal tools: regedit /s, echo >> & regedit /s, reg add
1.1 regedit /s - Windows built - in tool
Advantages: Can be used under all Windows systems, and can operate other registry items simultaneously
Disadvantages: Cannot dynamically define variables, needs to be restarted to take effect, and needs to read additional registry files
:: dsc.reg already exists and is available
REGEDIT /S DSC.REG
1.2 echo >> & regedit /s - Windows built - in commands and tools
Advantages: Can dynamically define variables, can be used under all Windows systems, and can operate other registry items simultaneously
Disadvantages: Needs to be restarted to take effect, and needs to read and write additional registry files
:: dsc.reg already exists and is available, modify the environment variable Driver to the converted value of the cd variable
ECHO "Driver"="%cd:\=\\%\\sqora32.dll">>DSC.REG
REGEDIT /S DSC.REG
1.3 reg add - Windows 2K/XP/03 built - in tool
Advantages: Can dynamically define variables
Disadvantages: Needs to be restarted to take effect, and cannot be used under 9X by default
SET ENV_POOL=HKLM\SYSTEM\ControlSet001\Control\Session Manager\Environment
REG ADD "%ENV_POOL%" /V PATH /T REG_SZ /D "%cd%;%Path%" /F >nul
2. External tools: setx, setntenvar, winset
2.1 setx - Support tool package extracted from 2K/XP/03 installation disc
Version 1.0a (5/31/96)
Gary Milne - Microsoft MCS
Advantages: Can dynamically define variables, takes effect immediately without restart, can dynamically reference variables, and can reference text content to set variables
Disadvantages: Can only be used under NTs, cannot delete variables but can only set them to empty values
:: Set current user global variable
SETX MACHINE COMPAQ
:: Set system global variable
SETX MACHINE "COMPAQ COMPUTER" -m
:: Static reference to other environment variables (only reference the current session environment variables once)
SETX MYPATH %PATH%
:: Dynamic reference to other environment variables (always reference the global environment variables)
SETX MYPATH ~PATH~
:: Use registry value to set environment variable
SETX BUILD -k "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\CurrentBuildNumber"
:: Use the content specified in the file to set environment variable (will not take effect immediately)
SETX IPGATEWAY -f ipconfig.out -r 0,7 "Gateway"
2.2 setntenvar - From Wuyou Boot Forum (topic = 4197) Lx1638 (Lao Jiu)
SetNTEnVar V06.04
Advantages: Can dynamically define variables, can define the CD - ROM drive letter as an environment variable, takes effect immediately without restart
Disadvantages: Can only be used under NTs
:: Modify variable
SETNTENVAR PATH=%PATH_BAK%
:: Delete variable
SETNTENVAR BAK_PATH=
:: Define CD - ROM drive letter as environment variable CDROM\CDROM0\CDROM1, etc.
SETNTENVAR /FindCDROM
2.3 winset - From 9x installation disc support tool package
Advantages: Can dynamically define variables, takes effect immediately without restart
Disadvantages: Can only be used under 9X, and runs in error under NTs
:: Modify variable
WINSET PATH=%PATH_BAK%
:: Delete variable
WINSET BAK_PATH=
3. Other scripts: vbs
3.1 vbs - From China DOS Union Forum (tid = 27952) electronixtar
set sysenv=CreateObject("WScript.Shell").Environment("system") 'Array object of system environment variables
sysenv.Remove("ztest2") 'Delete variable
sysenv("ztest3")="test value" 'Add variable
Advantages: Can dynamically define variables, takes effect immediately without restart
Disadvantages: Needs to modify the system configuration file, and is only valid for the command line created by command
4. Automatic batch processing: autoexec.bat, autoexec.nt
4.1 autoexec.bat - From the root directory of the system disk
Advantages: Can dynamically define variables, can be used under all DOS/Windows systems
Disadvantages: Needs to be restarted to take effect, and needs to modify the system file
echo set path=d:\batch;%path%>> c:\autoexec.bat
In 2K/XP/03, whether the variables in autoexec.bat are parsed is related to the following registry item
User Key:
Value Name: ParseAutoexec
Data Type: REG_SZ (String Value)
Value Data: (0 = disabled, 1 = enabled)
4.2 autoexec.nt - From %SystemRoot%\system32 of 2K/XP/03 system
Advantages: Can dynamically define variables, takes effect immediately without restart
Disadvantages: Needs to modify the system file, and is only valid for the command line that uses the autoexec.nt file in 2K/XP/03
echo set path=d:\batch;%path%>> %SystemRoot%\System32\autoexec.nt
===============================================
Generally speaking, there are two places for system environment variables under the NT series registry:
1. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
Registry item representing the system environment variable space
2. HKEY_CURRENT_USER\Environment
Registry item representing the current user's environment variable space
The registry values under these two keys respectively represent the corresponding environment variables. Directly modifying the variables in the system space requires restarting to take effect globally, and for the variables modified in the user space, it is only necessary to log off.
In addition, it should be noted that the system variables set by the above methods cannot take effect in the current session (window) where the batch processing or command line is run. So if you query immediately after setting the variables, there will be no change.
The simplest way to solve this problem is to set the session - level variable to the same content as the system - level variable while setting the system - level variable. In addition, you can also use reg query or regedit /e or setx -k to query the corresponding registry value.
===============================================
About the issue of the autoexec.nt file
The effectiveness of the configuration file has nothing to do with the file name. It only takes effect when it can be correctly referenced by the command line program.
The method to reference the configuration file is to create a shortcut, fill in command in the item, click OK to generate the program information file (.pif) "MS - DOS mode", and define the initialization file in "Properties -> Program -> Advanced"
But because command.com always uses %SystemRoot%\_default.pif as the program information file, and _default.pif defines auotexec.nt and config.nt as the initialization configuration files, so when using command.com to open the command line, it can always successfully reference the environment variables set in autoexec.nt.
============================
Related links
1. setx.exe extracted from the support tool package of WinXP SP2
http://www.cn-dos.net/forum/viewthread.php?tid=28698
2. setx.vbs system environment variables that still take effect after restart
http://www.cn-dos.net/forum/viewthread.php?tid=27952
3. How to write a script to automatically add system environment variables
http://www.cn-dos.net/forum/viewthread.php?tid=24114
4. How to use batch processing to modify the system path environment variable
http://www.cn-dos.net/forum/viewthread.php?tid=25057
5. How to use batch processing to set the environment variables of the XP system
http://www.cn-dos.net/forum/viewthread.php?tid=23409
6. Mutual influence of various factors during the startup process of MSDOS7.10
http://www.cn-dos.net/forum/viewthread.php?tid=17107
16:56 2007 - 3 - 24
14:42 2007 - 4 - 1
[ Last edited by willsort on 2007 - 4 - 2 at 06:04 AM ]
Recently, I've noticed that many people are discussing how to use scripts to set system global environment variables that are long - term and effective. Coincidentally, I've also used this kind of content recently, so I've collected and sorted out everyone's discussion content, and thus this article is formed.
1. Internal tools: regedit /s, echo >> & regedit /s, reg add
2. External tools: setx, setntenvar, winset
3. Other scripts: vbs
4. Automatic batch processing: autoexec.bat, autoexec.nt
===============================================
1. Internal tools: regedit /s, echo >> & regedit /s, reg add
1.1 regedit /s - Windows built - in tool
Advantages: Can be used under all Windows systems, and can operate other registry items simultaneously
Disadvantages: Cannot dynamically define variables, needs to be restarted to take effect, and needs to read additional registry files
:: dsc.reg already exists and is available
REGEDIT /S DSC.REG
1.2 echo >> & regedit /s - Windows built - in commands and tools
Advantages: Can dynamically define variables, can be used under all Windows systems, and can operate other registry items simultaneously
Disadvantages: Needs to be restarted to take effect, and needs to read and write additional registry files
:: dsc.reg already exists and is available, modify the environment variable Driver to the converted value of the cd variable
ECHO "Driver"="%cd:\=\\%\\sqora32.dll">>DSC.REG
REGEDIT /S DSC.REG
1.3 reg add - Windows 2K/XP/03 built - in tool
Advantages: Can dynamically define variables
Disadvantages: Needs to be restarted to take effect, and cannot be used under 9X by default
SET ENV_POOL=HKLM\SYSTEM\ControlSet001\Control\Session Manager\Environment
REG ADD "%ENV_POOL%" /V PATH /T REG_SZ /D "%cd%;%Path%" /F >nul
2. External tools: setx, setntenvar, winset
2.1 setx - Support tool package extracted from 2K/XP/03 installation disc
Version 1.0a (5/31/96)
Gary Milne - Microsoft MCS
Advantages: Can dynamically define variables, takes effect immediately without restart, can dynamically reference variables, and can reference text content to set variables
Disadvantages: Can only be used under NTs, cannot delete variables but can only set them to empty values
:: Set current user global variable
SETX MACHINE COMPAQ
:: Set system global variable
SETX MACHINE "COMPAQ COMPUTER" -m
:: Static reference to other environment variables (only reference the current session environment variables once)
SETX MYPATH %PATH%
:: Dynamic reference to other environment variables (always reference the global environment variables)
SETX MYPATH ~PATH~
:: Use registry value to set environment variable
SETX BUILD -k "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\CurrentBuildNumber"
:: Use the content specified in the file to set environment variable (will not take effect immediately)
SETX IPGATEWAY -f ipconfig.out -r 0,7 "Gateway"
2.2 setntenvar - From Wuyou Boot Forum (topic = 4197) Lx1638 (Lao Jiu)
SetNTEnVar V06.04
Advantages: Can dynamically define variables, can define the CD - ROM drive letter as an environment variable, takes effect immediately without restart
Disadvantages: Can only be used under NTs
:: Modify variable
SETNTENVAR PATH=%PATH_BAK%
:: Delete variable
SETNTENVAR BAK_PATH=
:: Define CD - ROM drive letter as environment variable CDROM\CDROM0\CDROM1, etc.
SETNTENVAR /FindCDROM
2.3 winset - From 9x installation disc support tool package
Advantages: Can dynamically define variables, takes effect immediately without restart
Disadvantages: Can only be used under 9X, and runs in error under NTs
:: Modify variable
WINSET PATH=%PATH_BAK%
:: Delete variable
WINSET BAK_PATH=
3. Other scripts: vbs
3.1 vbs - From China DOS Union Forum (tid = 27952) electronixtar
set sysenv=CreateObject("WScript.Shell").Environment("system") 'Array object of system environment variables
sysenv.Remove("ztest2") 'Delete variable
sysenv("ztest3")="test value" 'Add variable
Advantages: Can dynamically define variables, takes effect immediately without restart
Disadvantages: Needs to modify the system configuration file, and is only valid for the command line created by command
4. Automatic batch processing: autoexec.bat, autoexec.nt
4.1 autoexec.bat - From the root directory of the system disk
Advantages: Can dynamically define variables, can be used under all DOS/Windows systems
Disadvantages: Needs to be restarted to take effect, and needs to modify the system file
echo set path=d:\batch;%path%>> c:\autoexec.bat
In 2K/XP/03, whether the variables in autoexec.bat are parsed is related to the following registry item
User Key:
Value Name: ParseAutoexec
Data Type: REG_SZ (String Value)
Value Data: (0 = disabled, 1 = enabled)
4.2 autoexec.nt - From %SystemRoot%\system32 of 2K/XP/03 system
Advantages: Can dynamically define variables, takes effect immediately without restart
Disadvantages: Needs to modify the system file, and is only valid for the command line that uses the autoexec.nt file in 2K/XP/03
echo set path=d:\batch;%path%>> %SystemRoot%\System32\autoexec.nt
===============================================
Generally speaking, there are two places for system environment variables under the NT series registry:
1. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
Registry item representing the system environment variable space
2. HKEY_CURRENT_USER\Environment
Registry item representing the current user's environment variable space
The registry values under these two keys respectively represent the corresponding environment variables. Directly modifying the variables in the system space requires restarting to take effect globally, and for the variables modified in the user space, it is only necessary to log off.
In addition, it should be noted that the system variables set by the above methods cannot take effect in the current session (window) where the batch processing or command line is run. So if you query immediately after setting the variables, there will be no change.
The simplest way to solve this problem is to set the session - level variable to the same content as the system - level variable while setting the system - level variable. In addition, you can also use reg query or regedit /e or setx -k to query the corresponding registry value.
===============================================
About the issue of the autoexec.nt file
The effectiveness of the configuration file has nothing to do with the file name. It only takes effect when it can be correctly referenced by the command line program.
The method to reference the configuration file is to create a shortcut, fill in command in the item, click OK to generate the program information file (.pif) "MS - DOS mode", and define the initialization file in "Properties -> Program -> Advanced"
But because command.com always uses %SystemRoot%\_default.pif as the program information file, and _default.pif defines auotexec.nt and config.nt as the initialization configuration files, so when using command.com to open the command line, it can always successfully reference the environment variables set in autoexec.nt.
============================
Related links
1. setx.exe extracted from the support tool package of WinXP SP2
http://www.cn-dos.net/forum/viewthread.php?tid=28698
2. setx.vbs system environment variables that still take effect after restart
http://www.cn-dos.net/forum/viewthread.php?tid=27952
3. How to write a script to automatically add system environment variables
http://www.cn-dos.net/forum/viewthread.php?tid=24114
4. How to use batch processing to modify the system path environment variable
http://www.cn-dos.net/forum/viewthread.php?tid=25057
5. How to use batch processing to set the environment variables of the XP system
http://www.cn-dos.net/forum/viewthread.php?tid=23409
6. Mutual influence of various factors during the startup process of MSDOS7.10
http://www.cn-dos.net/forum/viewthread.php?tid=17107
16:56 2007 - 3 - 24
14:42 2007 - 4 - 1
[ Last edited by willsort on 2007 - 4 - 2 at 06:04 AM ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
