China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-12 02:30
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Set environment variables locally or system-wide without writing scripts using setx. View 1,865 Replies 7
Original Poster Posted 2006-12-07 08:18 ·  中国 河南 郑州 电信
中级用户
★★
Credits 439
Posts 170
Joined 2006-01-09 20:29
20-year member
UID 48707
Status Offline
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
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +3 2006-12-07 08:23
bat c c++
Floor 2 Posted 2006-12-07 08:22 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 3 Posted 2006-12-07 08:28 ·  中国 河南 郑州 电信
中级用户
★★
Credits 439
Posts 170
Joined 2006-01-09 20:29
20-year member
UID 48707
Status Offline
setx directly writes the variable to the registry, and the variable still exists after restarting the machine
bat c c++
Floor 4 Posted 2006-12-07 12:31 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Need additional installation, it's better to use VBS.
Floor 5 Posted 2007-01-27 10:10 ·  中国 天津 河西区 联通
新手上路
Credits 16
Posts 8
Joined 2006-11-04 08:09
19-year member
UID 69534
Gender Male
Status Offline
Nice stuff, I was looking for this today.
Floor 6 Posted 2007-01-27 10:20 ·  中国 甘肃 平凉 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Floor 7 Posted 2010-10-27 11:42 ·  中国 海南 海口 联通
新手上路
Credits 2
Posts 2
Joined 2010-04-11 11:10
16-year member
UID 164306
Gender Male
Status Offline
Good post about setx, it's best to provide the download of setx.
Floor 8 Posted 2010-10-27 13:15 ·  中国 吉林 长春 电信
初级用户
枫中残雪
Credits 65
Posts 43
Joined 2010-04-29 23:20
16-year member
UID 165810
Gender Male
From 吉林省长春市
Status Offline
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.
Forum Jump: