setx

在用户或系统环境中创建或修改环境变量,而无需编程或脚本。 The Setx command also retrieves the values of registry keys and writes them to text files.

Note

此命令提供直接和永久设置系统环境值的唯一命令行或编程方式。 System environment variables are manually configurable through Control Panel or through a registry editor. The set command, which is internal to the command interpreter (Cmd.exe), sets user environment variables for the current console window only.

Syntax

setx [/s <computer> [/u [<domain>\]<user name> [/p [<password>]]]] <variable> <value> [/m]
setx [/s <computer> [/u [<domain>\]<user name> [/p [<password>]]]] <variable>] /k <path> [/m]
setx [/s <computer> [/u [<domain>\]<user name> [/p [<password>]]]] /f <filename> {[<variable>] {/a <X>,<Y> | /r <X>,<Y> <String>} [/m] | /x} [/d <delimiters>]

Parameters

Parameter Description
/秒 <computer> 指定远程计算机的名称或 IP 地址。 不要使用反斜杠。 默认值为本地计算机的名称。
/u [<domain>\]<user name> 使用指定用户帐户的凭据运行脚本。 默认值为系统权限。
/p [<password>] Specifies the password of the user account that is specified in the /u parameter.
<variable> 指定要设置的环境变量的名称。
<value> 指定要设置环境变量的值。
/k <path> 指定根据注册表项中的信息设置变量。 The path uses the following syntax: \\<HIVE>\<KEY>\...\<Value>. 例如,可以指定以下路径:HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TimeZoneInformation\StandardName
/f <filename> 指定要使用的文件。
/一个 <X>,<Y> 将绝对坐标和偏移指定为搜索参数。
/r <X>,<Y> <String> Specifies relative coordinates and offset from String as search parameters.
/m 指定在系统环境中设置变量。 默认设置是本地环境。
/x Displays file coordinates, ignoring the /a, /r, and /d command-line options.
/d <delimiters> Specifies delimiters such as , or \ to be used in addition to the four built-in delimiters — SPACE, TAB, ENTER, and LINEFEED. 有效的分隔符包括任何 ASCII 字符。 分隔符的最大数目为 15,包括内置分隔符。
/? 在命令提示符下显示帮助。

Remarks

  • 此命令类似于 UNIX 实用工具 SETENV。

  • 可以使用此命令从三个源之一(模式):命令行模式、注册表模式或文件模式之一设置用户和系统环境变量的值。

  • 此命令将变量写入注册表中的主环境。 Variables set with setx variables are available in future command windows only, not in the current command window.

  • HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE are the only supported hives. REG_DWORD, REG_EXPAND_SZ, REG_SZ, and REG_MULTI_SZ are the valid RegKey data types.

  • If you gain access to REG_MULTI_SZ values in the registry, only the first item is extracted and used.

  • 不能使用此命令删除添加到本地或系统环境的值。 可以将此命令与变量名称一起使用,并且没有值可从本地环境中删除相应的值。

  • REG_DWORD注册表值提取并用于十六进制模式。

  • 文件模式仅支持分析回车符和换行符(CRLF)文本文件。

  • 在现有变量上运行此命令会删除任何变量引用并使用扩展值。

    For instance, if the variable %PATH% has a reference to %JAVADIR%, and %PATH% is manipulated using setx, %JAVADIR% is expanded and its value is assigned directly to the target variable %PATH%. This means that future updates to %JAVADIR% will not be reflected in the %PATH% variable.

  • Be aware there's a limit of 1024 characters when assigning contents to a variable using setx.

    这意味着,如果超过 1024 个字符,则裁剪的内容是应用于目标变量的内容。 如果此裁剪的文本应用于现有变量,则可能会导致目标变量以前保留的数据丢失。

Examples

To set the MACHINE environment variable in the local environment to the value Brand1, type:

setx MACHINE Brand1

To set the MACHINE environment variable in the system environment to the value Brand1 Computer, 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 remote 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 remote 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 TZONE environment variable in the local environment of a remote 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 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 BUILD environment variable in the system environment of a remote 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 a file named Ipconfig.out, along with the contents' corresponding coordinates, type:

setx /f ipconfig.out /x

To set the IPADDR environment variable in the local environment to the value found at the coordinate 5,11 in the Ipconfig.out file, type:

setx IPADDR /f ipconfig.out /a 5,11

To set the OCTET1 environment variable in the local environment to the value found at the coordinate 5,3 in the Ipconfig.out file 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 the coordinate 0,7 with respect to the coordinate of Gateway in the Ipconfig.out file, type:

setx IPGATEWAY /f ipconfig.out /r 0,7 Gateway

To display the contents of the Ipconfig.out file, along with the contents' corresponding coordinates, on a computer named computer1, type:

setx /s computer1 /u maindom\hiropln /p p@ssW23 /f ipconfig.out /x