![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-12 04:17 |
47,811 topics / 349,897 posts / today 0 new / 48,256 members |
| DOS批处理 & 脚本技术(批处理室) » Set environment variables locally or system-wide without writing scripts using setx. |
| Printable Version 1,866 / 7 |
| Floor1 a9319751 | Posted 2006-12-07 08:18 |
| 中级用户 Posts 170 Credits 439 | |
|
Setx
Set environment variables in the local or system environment without writing programs or making scripts. Setx also retrieves the value of a registry key and writes it to a text file. Syntax setx User ]]] VariableValue setx User ]]] /k Path setx User ]]] /f FileName { {/a X,y | /r X,y "String"} | /x} Parameters /s Computer Specifies the remote computer name or IP address (no backslashes allowed). The default is the local computer. This parameter applies to all files and folders specified in the command. /u User Runs the script with the permissions of the specified user account. The default is system permissions. /p Specifies the password for the user account specified in the /u parameter. Variable Specifies the name of the environment variable to set. Value Specifies the value to set the environment variable to. /k Path Specifies to set the variable based on information from a registry key. Specify Path in the format \HIVE\KEY\...\Value (for example, HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TimeZoneInformation\StandardName). /f FileName Specifies the file to use. /a X,y Specifies absolute coordinates and offset as search parameters. /r X,y "String" Specifies relative coordinates and offset from String as search parameters. /m Specifies to set Variable in the system environment. The default is the local environment. /x Displays the file coordinates. Ignores the /a, /r, and /d command-line options. /d Delimiters Specifies additional delimiters, such as "," or "\". The four built-in delimiters are space, tab, carriage return, and line feed. Any ASCII character can be used as an additional delimiter. The maximum number of delimiters is 15, including the four built-in ones. /? Displays help at the command prompt. Remarks • Use the /s, /u, and /p command-line options. The /u and /p command-line options are only available when using /s. You must use both /p and /u to provide the user password. • Setx provides a way to directly and permanently set system environment values for command-line or programming purposes. System environment variables can be manually configured through "Control Panel" or through the Registry Editor (Regedit.exe). The Set command is an internal command of the command interpreter (i.e., Cmd.exe) and only sets user environment variables for the current console window. • You can use setx to set values for user and system environment variables from one of three sources: command-line mode, registry mode, or file mode. • Setx is similar to the UNIX utility SETENV. • Setx writes variables to the main environment in the registry. Variables set with setx are only available in future command windows, not in the current command window. • Only the HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE hives are supported. REG_DWORD, REG_EXPAND_SZ, REG_SZ, and REG_MULTI_SZ are valid RegKey data types. • When accessing a REG_MULTI_SZ value in the registry, only the first item is expanded and used. • Values added to the local or system environment cannot be deleted using setx. You can use set with a variable name but no value to delete the corresponding value in the local environment. • REG_DWORD registry values are expanded and used in hexadecimal mode. • File mode only supports parsing CR-LF text files. Examples To set the MACHINE environment variable to Brand1 in the local environment, type: setx MACHINE brand1 To set the MACHINE environment variable to Brand1 Computer in the system environment, type: setx MACHINE "brand1 computer" /m To set the MYPATH environment variable in the local environment to use the search path defined in the PATH environment variable, type: setx MYPATH %PATH% To set the MYPATH environment variable in the local environment to use the search path defined in the PATH environment variable after replacing ~ with %, type: setx MYPATH ~PATH~ To set the MACHINE environment variable in the local environment to BRAND1 on a computer named Computer1, type: setx /s computer1 /u maindom\hiropln /p p@ssW23 MACHINE BRAND1 To set the MYPATH environment variable in the local environment to use the search path defined in the PATH environment variable on a computer named Computer1, type: setx /s computer1 /u maindom\hiropln /p p@ssW23 MYPATH %PATH% To set the TZONE environment variable in the local environment to the value found in the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TimeZoneInformation\StandardName registry key, type: setx TZONE /k HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TimeZoneInformation\StandardName To set the BUILD environment variable in the system environment to the value found in the HKEY_LOCAL_MACHINE\Software\Microsoft\WindowsNT\CurrentVersion\CurrentBuildNumber registry key, type: setx BUILD /k "HKEY_LOCAL_MACHINE\Software\Microsoft\WindowsNT\CurrentVersion\CurrentBuildNumber" /m To set the TZONE environment variable in the local environment on a computer named Computer1 to the value found in the HKEY_LOCAL_MACHINE\ System\CurrentControlSet\Control\TimeZoneInformation\StandardName registry key, type: setx /s computer1 /u maindom\hiropln /p p@ssW23 TZONE /k HKEY_LOCAL_MACHINE\ System\CurrentControlSet\Control\TimeZoneInformation\StandardName To set the BUILD environment variable in the system environment on a computer named Computer1 to the value found in the HKEY_LOCAL_MACHINE\Software\Microsoft\WindowsNT\CurrentVersion\CurrentBuildNumber registry key, type: setx /s computer1 /u maindom\hiropln /p p@ssW23 BUILD /k "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\CurrentBuildNumber" /m To display the contents of the file ipconfig.out and its corresponding coordinates, type: setx /f ipconfig.out /x To set the IPADDR environment variable in the local environment to the value found at coordinates 5,11 in the file ipconfig.out, type: setx IPADDR /f ipconfig.out /a 5,11 To set the OCTET1 environment variable in the local environment to the value found at coordinates 5,3 in the file ipconfig.out with delimiters "#$*.", type: setx OCTET1 /f ipconfig.out /a 5,3 /d "#$*." To set the IPGATEWAY environment variable in the local environment to the value found at coordinates 0,7 relative to "Gateway" in the file ipconfig.out, type: setx IPGATEWAY /f ipconfig.out /r 0,7 Gateway To display the contents of the file ipconfig.out and its corresponding coordinates on a computer named Computer1, type: setx /s computer1 /u maindom\hiropln /p p@ssW23 /f ipconfig.out /x |
|
| Floor2 redtek | Posted 2006-12-07 08:22 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
| Floor3 a9319751 | Posted 2006-12-07 08:28 |
| 中级用户 Posts 170 Credits 439 | |
|
setx directly writes the variable to the registry, and the variable still exists after restarting the machine
|
|
| Floor4 3742668 | Posted 2006-12-07 12:31 |
| 荣誉版主 Posts 718 Credits 2,013 | |
|
Need additional installation, it's better to use VBS.
|
|
| Floor5 kaidi | Posted 2007-01-27 10:10 |
| 新手上路 Posts 8 Credits 16 | |
|
Nice stuff, I was looking for this today.
|
|
| Floor6 vkill | Posted 2007-01-27 10:20 |
| 金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽 | |
| Floor7 luckyjady | Posted 2010-10-27 11:42 |
| 新手上路 Posts 2 Credits 2 | |
|
Good post about setx, it's best to provide the download of setx.
|
|
| Floor8 726842270 | Posted 2010-10-27 13:15 |
| 初级用户 Posts 43 Credits 65 From 吉林省长春市 | |
|
LZ has a very unique understanding of SET. I thought only REG could be used to modify the registry. It seems I need to study hard.
|
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |