setxsetx

在用户或系统环境中创建或修改环境变量,无需编程或编写脚本。Creates or modifies environment variables in the user or system environment, without requiring programming or scripting. Setx命令还检索注册表项的值,并将它们写入文本文件。The Setx command also retrieves the values of registry keys and writes them to text files.

备注

此命令提供了唯一直接、永久地设置系统环境值的命令行或编程方式。This command provides the only command-line or programmatic way to directly and permanently set system environment values. 可以通过 "控制面板" 或通过注册表编辑器手动配置系统环境变量。System environment variables are manually configurable through Control Panel or through a registry editor. Set命令(在命令解释器内部 ( # A0) )仅为当前控制台窗口设置用户环境变量。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>]

parametersParameters

参数Parameter 说明Description
/s <computer>/s <computer> 指定远程计算机的名称或 IP 地址。Specifies the name or IP address of a remote computer. 不要使用反斜杠。Do not use backslashes. 默认值为本地计算机的名称。The default value is the name of the local computer.
/u [<domain>\]<user name>/u [<domain>\]<user name> 用指定用户帐户的凭据运行脚本。Runs the script with the credentials of the specified user account. 默认值为 "系统权限"。The default value is the system permissions.
/p [ <password> ]/p [<password>] 指定在 /u 参数中指定的用户帐户的密码。Specifies the password of the user account that is specified in the /u parameter.
<variable> 指定要设置的环境变量的名称。Specifies the name of the environment variable that you want to set.
<value> 指定要将环境变量设置为的值。Specifies the value to which you want to set the environment variable.
遇到 <path>/k <path> 指定根据注册表项中的信息设置变量。Specifies that the variable is set based on information from a registry key. 路径 使用以下语法: \\<HIVE>\<KEY>\...\<Value>The path uses the following syntax: \\<HIVE>\<KEY>\...\<Value>. 例如,你可以指定以下路径: HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TimeZoneInformation\StandardNameFor example, you might specify the following path: HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TimeZoneInformation\StandardName
/f <filename>/f <filename> 指定要使用的文件。Specifies the file that you want to use.
/a <X>,<Y>/a <X>,<Y> 指定绝对坐标,偏移量作为搜索参数。Specifies absolute coordinates and offset as search parameters.
/r <X>,<Y> <String>/r <X>,<Y> <String> 指定相对坐标和作为搜索参数的 字符串 的偏移量。Specifies relative coordinates and offset from String as search parameters.
/m/m 指定在系统环境中设置变量。Specifies to set the variable in the system environment. 默认设置为本地环境。The default setting is the local environment.
/x/x 显示文件坐标,并忽略 /a/r/d 命令行选项。Displays file coordinates, ignoring the /a, /r, and /d command-line options.
/d <delimiters>/d <delimiters> 指定除 \ 空格、制表符、回车和换行以外,还可以使用除四个内置分隔符以外的分隔符。Specifies delimiters such as , or \ to be used in addition to the four built-in delimiters — SPACE, TAB, ENTER, and LINEFEED. 有效的分隔符包含任何 ASCII 字符。Valid delimiters include any ASCII character. 最大分隔符数为15,其中包括内置分隔符。The maximum number of delimiters is 15, including built-in delimiters.
/?/? 在命令提示符下显示帮助。Displays help at the command prompt.

备注Remarks

  • 此命令类似于 UNIX 实用工具 SETENV。This command is similar to the UNIX utility SETENV.

  • 你可以使用此命令从以下三个源之一 (模式设置用户和系统环境变量的值) :命令行模式、注册表模式或文件模式。You can use this command to set values for user and system environment variables from one of three sources (modes): Command Line Mode, Registry Mode, or File Mode.

  • 此命令将变量写入注册表中的主环境。This command writes variables to the master environment in the registry. 使用 setx 变量设置的变量仅在以后的命令窗口中可用,而不能在当前的命令窗口中使用。Variables set with setx variables are available in future command windows only, not in the current command window.

  • HKEY_CURRENT_USERHKEY_LOCAL_MACHINE 是唯一受支持的配置单元。HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE are the only supported hives. REG_DWORD、REG_EXPAND_SZ、REG_SZ 和 REG_MULTI_SZ 都是有效的 RegKey 数据类型。REG_DWORD, REG_EXPAND_SZ, REG_SZ, and REG_MULTI_SZ are the valid RegKey data types.

  • 如果获得对注册表中 REG_MULTI_SZ 值的访问权限,则只提取并使用第一项。If you gain access to REG_MULTI_SZ values in the registry, only the first item is extracted and used.

  • 无法使用此命令删除添加到本地或系统环境的值。You can't use this command to remove values added to the local or system environments. 可以将此命令与变量名称和无值一起使用,以从本地环境中删除相应的值。You can use this command with a variable name and no value to remove a corresponding value from the local environment.

  • REG_DWORD 在十六进制模式下提取和使用注册表值。REG_DWORD registry values are extracted and used in hexadecimal mode.

  • 文件模式支持仅) 文本文件 (CRLF 分析回车符和换行。File mode supports the parsing of carriage return and line feed (CRLF) text files only.

  • 对现有变量运行此命令将删除所有变量引用并使用扩展值。Running this command on an existing variable removes any variable references and uses expanded values.

    例如,如果变量% PATH% 引用了% JAVADIR%,而使用 setx操作了% path%,则将展开% JAVADIR%,并将其值直接分配给目标变量% PATH%。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%. 这意味着,% JAVADIR% 的将来更新 将不 会反映在% PATH% 变量中。This means that future updates to %JAVADIR% will not be reflected in the %PATH% variable.

  • 请注意,使用 setx将内容分配到变量时,会出现1024个字符的限制。Be aware there's a limit of 1024 characters when assigning contents to a variable using setx.

    这意味着,如果您超过1024个字符,则会裁剪内容,并且裁剪后的文本会应用于目标变量。This means that the content is cropped if you go over 1024 characters, and that the cropped text is what's applied to the target variable. 如果此裁剪文本应用于现有变量,则可能会导致目标变量以前保存的数据丢失。If this cropped text is applied to an existing variable, it can result in loss of data previously held by the target variable.

示例Examples

若要将本地环境中的 计算机 环境变量设置为 Brand1值,请键入:To set the MACHINE environment variable in the local environment to the value Brand1, type:

setx MACHINE Brand1

若要将系统环境中的 计算机 环境变量设置为值 Brand1 计算机,请键入:To set the MACHINE environment variable in the system environment to the value Brand1 Computer, type:

setx MACHINE Brand1 Computer /m

若要将本地环境中的 MYPATH 环境变量设置为使用 path 环境变量中定义的搜索路径,请键入: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%

若要在将替换为之后将本地环境中的 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~

若要在名为computer1的远程计算机上将本地环境中的计算机环境变量设置为Brand1 ,请键入: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

若要在本地环境中设置MYPATH环境变量以使用在名为computer1的远程计算机上的path环境变量中定义的搜索路径,请键入: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%

若要将本地环境中的 TZONE 环境变量设置为 HKEY_LOCAL_MACHINE \system\currentcontrolset\control\timezoneinformation\standardname 注册表项中找到的值,请键入: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

若要将名为computer1的远程计算机的本地环境中的TZONE环境变量设置为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

若要在系统环境中将 生成 环境变量设置为在 HKEY_LOCAL_MACHINE \software\microsoft\windowsnt\currentversion\currentbuildnumber 注册表项中找到的值,请键入: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

若要在名为 Computer1 的远程计算机的系统环境中将生成环境变量设置为在 HKEY_LOCAL_MACHINE \software\microsoft\windowsnt\currentversion\currentbuildnumber 注册表项中找到的值,请键入: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

若要显示名为 Ipconfig. out的文件的内容,以及内容的对应坐标,请键入:To display the contents of a file named Ipconfig.out, along with the contents' corresponding coordinates, type:

setx /f ipconfig.out /x

若要将本地环境中的IPADDR环境变量设置为在文件的坐标5,11中找到的值,请键入: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

若要将本地环境中的OCTET1环境变量设置为在文件中的坐标5、3处找到的值,请在带有分隔符 *# $ *** 的Ipconfig*文件中键入: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 #$*.

若要将本地环境中的IPGATEWAY环境变量设置为在文件的**网关坐标中的坐标0、7上找到的值,请键入: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

若要在名为computer1的计算机上显示Ipconfig文件的内容以及内容的对应坐标,请键入: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

其他参考Additional References