『楼 主』:
setx本地或系统环境中设置变量无需编写脚本。
使用 LLM 解释/回答一下
Setx
在本地或系统环境中设置环境变量,无需编写程序或制作脚本。Setx 还检索注册表项的值并将其写入到文本文件。
语法
setx User ]]] VariableValue
setx User ]]] /k Path
setx User ]]] /f FileName { {/a X,y | /r X,y "String"} | /x}
参数
/s Computer
指定远程计算机名称或 IP 地址(不能使用反斜杠)。该默认值是本地计算机。此参数适用于在命令中指定的所有文件和文件夹。
/u User
使用指定用户帐户的权限运行脚本。该默认值是系统权限。
/p
指定在 /u 参数中指定的用户帐户的密码。
Variable
指定要设置的环境变量的名称。
Value
指定要将环境变量设置到的值。
/k Path
指定根据来自注册表项的信息设置变量。指定 Path,格式为 \HIVE\KEY\...\Value(例如,HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TimeZoneInformation\StandardName)。
/f FileName
指定要使用的文件。
/a X,y
指定绝对坐标和偏移量作为搜索参数。
/r X,y "String"
指定来自 String 的相对坐标和偏移量作为搜索参数。
/m
指定设置系统环境中的 Variable。默认值是本地环境。
/x
显示文件坐标。忽略 /a、/r 和 /d 命令行选项。
/d Delimiters
指定附加的分隔符,如“,”或“\”。四个内置的分隔符为空格、跳格键、回车和换行符。可使用任何 ASCII 字符作为附加的分隔符。分隔符的最大值为 15,包括四个内置的分隔符。
/?
在命令提示符下显示帮助。
注释
• 使用 /s、/u 和 /p 命令行选项。只有在使用使用 /s 时才可使用 /u 和 /p 命令行选项。必须同时使用 /p 和 /u 才能提供用户密码。
• Setx 提供仅用于命令行或编程的方式直接且永久地设置系统环境值。系统环境变量可通过“控制面板”或通过注册表编辑器 (Regedit.exe) 手动配置。Set 命令为命令解释程序(即,Cmd.exe)的内部命令,仅为当前控制台窗口设置用户环境变量。
• 您可以使用 setx 从以下三个源之一为用户和系统环境变量设置值:命令行模式、注册表模式或文件模式。
• Setx 类似于 UNIX 实用程序 SETENV。
• Setx 将变量写入到注册表中的主环境。用 setx 设置的变量仅在未来的命令窗口中可用,而不是在当前的命令窗口。
• 只支持 HKEY_CURRENT_USER 和 HKEY_LOCAL_MACHINE 配置单元。REG_DWORD、REG_EXPAND_SZ、REG_SZ AND REG_MULTI_SZ 是有效的 RegKey 数据类型。
• 当在注册表中访问 REG_MULTI_SZ 值时,只有第一项被展开和使用。
• 无法使用 setx 删除添加到本地或系统环境中的值。可以使用带有变量名但不带值的 set 来删除本地环境中对应的值。
• REG_DWORD 注册表值将以十六进制的模式展开并使用。
• 文件模式仅支持对 CR-LF 文本文件的解析。
示例
要在本地环境中将 MACHINE 环境变量设置为 Brand1,请键入:
setx MACHINE brand1
要在系统环境中将 MACHINE 环境变量设置为 Brand1 Computer,请键入:
setx MACHINE "brand1 computer" /m
要在本地环境中设置 MYPATH 环境变量以使用在 PATH 环境变量中定义的搜索路径,请键入:
setx MYPATH %PATH%
要在本地环境中设置 MYPATH 环境变量以便在将 ~ 替换为 % 后使用在 PATH 环境变量中定义的搜索路径,请键入:
setx MYPATH ~PATH~
要在一台名为 Computer1 的计算机上将本地环境中的 MACHINE 环境变量设置为 BRAND1,请键入:
setx /s computer1 /u maindom\hiropln /p p@ssW23 MACHINE BRAND1
要在一台名为 Computer1 的计算机上将本地环境中的 MYPATH 环境变量设置为使用在 PATH 环境变量中定义的搜索路径,请键入:
setx /s computer1 /u maindom\hiropln /p p@ssW23 MYPATH %PATH%
要在本地环境中将 TZONE 环境变量设置为在 HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TimeZoneInformation\StandardName 注册表项中找到的值,请键入:
setx TZONE /k HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TimeZoneInformation\StandardName
要在系统环境中将 BUILD 环境变量设置为在 HKEY_LOCAL_MACHINE\Software\Microsoft\WindowsNT\CurrentVersion\CurrentBuildNumber 注册表项中找到的值,请键入:
setx BUILD /k "HKEY_LOCAL_MACHINE\Software\Microsoft\WindowsNT\CurrentVersion\CurrentBuildNumber" /m
要在一台名为 Computer1 的计算机上的本地环境中将 TZONE 环境变量设置为在 HKEY_LOCAL_MACHINE\ System\CurrentControlSet\Control\TimeZoneInformation\StandardName 注册表项中找到的值,请键入:
setx /s computer1 /u maindom\hiropln /p p@ssW23 TZONE /k HKEY_LOCAL_MACHINE\ System\CurrentControlSet\Control\TimeZoneInformation\StandardName
要在一台名为 Computer1 的计算机上的系统环境中将 BUILD 环境变量设置为在 HKEY_LOCAL_MACHINE\Software\Microsoft\WindowsNT\CurrentVersion\CurrentBuildNumber 注册表项中找到的值,请键入:
setx /s computer1 /u maindom\hiropln /p p@ssW23 BUILD /k "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\CurrentBuildNumber" /m
要显示文件 ipconfig.out 的内容及其对应的坐标,请键入:
setx /f ipconfig.out /x
要在本地环境中将 IPADDR 环境变量设置为在文件 ipconfig.out 中的 5,11 坐标处找到的值,请键入:
setx IPADDR /f ipconfig.out /a 5,11
要在本地环境中将 OCTET1 环境变量设置为在带有分隔符“#$*.”的文件 ipconfig.out 中 5,3 坐标处找到的值,请键入:
setx OCTET1 /f ipconfig.out /a 5,3 /d "#$*."
要在本地环境中将 IPGATEWAY 环境变量设置为在文件 ipconfig.out 中相对于“Gateway”坐标为 0,7 的坐标处找到的值,请键入:
setx IPGATEWAY /f ipconfig.out /r 0,7 Gateway
要显示一台名为 Computer1 的计算机上的文件 ipconfig.out 的内容及其对应的坐标,请键入:
setx /s computer1 /u maindom\hiropln /p p@ssW23 /f ipconfig.out /x
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
|