![使用 setx 附加到用户 PATH](https://linux22.com/image/1492285/%E4%BD%BF%E7%94%A8%20setx%20%E9%99%84%E5%8A%A0%E5%88%B0%E7%94%A8%E6%88%B7%20PATH.png)
我的尝试:
setx PATH "%PATH%;%~dp0"
我该如何做到这一点而不让系统 PATH 中的所有内容在用户 PATH 中重复?
答案1
我该如何做到这一点而不让系统 PATH 在用户 PATH 中重复?
使用该/m
选项。
setx /m PATH "%PATH%;%~dp0"
笔记:
/m
- 在系统环境 HKLM 中设置变量。(默认为本地环境 HKCU)
setx
用法
F:\test>setx /?
SetX has three ways of working:
Syntax 1:
SETX [/S system [/U [domain\]user [/P [password]]]] var value [/M]
Syntax 2:
SETX [/S system [/U [domain\]user [/P [password]]]] var /K regpath [/M]
Syntax 3:
SETX [/S system [/U [domain\]user [/P [password]]]]
/F file {var {/A x,y | /R x,y string}[/M] | /X} [/D delimiters]
Description:
Creates or modifies environment variables in the user or system
environment. Can set variables based on arguments, regkeys or
file input.
Parameter List:
/S system Specifies the remote system to connect to.
/U [domain\]user Specifies the user context under which
the command should execute.
/P [password] Specifies the password for the given
user context. Prompts for input if omitted.
var Specifies the environment variable to set.
value Specifies a value to be assigned to the
environment variable.
/K regpath Specifies that the variable is set based
on information from a registry key.
Path should be specified in the format of
hive\key\...\value. For example,
HKEY_LOCAL_MACHINE\System\CurrentControlSet\
Control\TimeZoneInformation\StandardName.
/F file Specifies the filename of the text file
to use.
/A x,y Specifies absolute file coordinates
(line X, item Y) as parameters to search
within the file.
/R x,y string Specifies relative file coordinates with
respect to "string" as the search parameters.
/M Specifies that the variable should be set in
the system wide (HKEY_LOCAL_MACHINE)
environment. The default is to set the
variable under the HKEY_CURRENT_USER
environment.
/X Displays file contents with x,y coordinates.
/D delimiters Specifies additional delimiters such as ","
or "\". The built-in delimiters are space,
tab, carriage return, and linefeed. Any
ASCII character can be used as an additional
delimiter. The maximum number of delimiters,
including the built-in delimiters, is 15.
/? Displays this help message.
NOTE: 1) SETX writes variables to the master environment in the registry.
2) On a local system, variables created or modified by this tool
will be available in future command windows but not in the
current CMD.exe command window.
3) On a remote system, variables created or modified by this tool
will be available at the next logon session.
4) The valid Registry Key data types are REG_DWORD, REG_EXPAND_SZ,
REG_SZ, REG_MULTI_SZ.
5) Supported hives: HKEY_LOCAL_MACHINE (HKLM),
HKEY_CURRENT_USER (HKCU).
6) Delimiters are case sensitive.
7) REG_DWORD values are extracted from the registry in decimal
format.
进一步阅读
- Windows CMD 命令行的 AZ 索引- 与 Windows cmd 行相关的所有事物的绝佳参考。
- 设置- 永久设置环境变量,SETX 可用于为机器(HKLM)或当前登录用户(HKCU)设置环境变量。
答案2
[CMD、WMIC、HKLM、HKCU、SETX、PATH,2001]
这个问题可能已经存在20多年了:
-setx 路径“%Path%;新路径”用系统路径污染用户路径。
-setx 路径 "%Path%;NewPath" /M用用户路径污染系统路径。
-污染:视频
我不确定是否存在一个安全的本机解决方案,该解决方案考虑专门使用命令提示符的非 ANSI 目录。
这是我的尝试!
- 使用 Microsoft Windows [版本 10.0.19042.868]
0- 检查路径
echo %Path%
reg query "HKCU\Environment" /v Path
reg query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v Path
reg EXPORT "HKCU\Environment" UserEnvironment.reg /y
reg EXPORT "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" SystemEnvironment.reg /y
wmic ENVIRONMENT where "username='<SYSTEM>' AND name='Path'" get variablevalue
wmic ENVIRONMENT where "username='%COMPUTERNAME%\\%USERNAME%' AND name='Path'" get variablevalue
提示:wmic ENVIRONMENT get *
[查看更多]
信息:使用上述命令执行了注册表备份。文件已保存在当前目录中。
1- 备份用户路径%USERPATH%
- 命令预先假设:
- 非空路径;
- 功能路径;
- 如果无效的或者无法运作小路只做步 ”2“
wmic ENVIRONMENT where "username='%COMPUTERNAME%\\%USERNAME%' AND name='Path'" get variablevalue | more +1 > %temp%\userpathvalue.txt
信息:字符串末尾有一些污染*。
set /P USERPATH=<%temp%\userpathvalue.txt
- 添加'/'
以更好地查看空间
set "USERPATH=%USERPATH%/"
- 删除最多 15 个结尾空格,然后删除“/”
set "USERPATH=%USERPATH: /=/%"
set "USERPATH=%USERPATH: /=/%"
set "USERPATH=%USERPATH: /=/%"
set "USERPATH=%USERPATH: /=/%"
set "USERPATH=%USERPATH:/=%"
信息:在我的测试中,引入的空格数量并不一致。
echo %USERPATH%[checking final string]
setx USERPATH "%USERPATH%"
* 处理不可见字符时,将其转换为十六进制会更容易识别它们,请执行以下操作:
wmic ENVIRONMENT where "username='%COMPUTERNAME%\\%USERNAME%' AND name='Path'" get variablevalue | more +1 > %tmp%\0 && set /P TEMPREADER=<%tmp%\0
echo %TEMPREADER% >%tmp%\1 && echo %TEMPREADER%/ && certutil -f -encodehex "%tmp%\1" "%tmp%\2" 11 | more +4 && type "%tmp%\2"
REM to open on notepad, type: certutil -f -encodehex "%tmp%\1" "%tmp%\2" 5 && cmd /C START "" /MAX notepad %tmp%\2
提示:在 encodehex 中使用 4、5、10、11(与 none 相同)
2- 将¹²³ 用户路径设置为%USERPATH%
唯一
setx PATH ^%USERPATH^%
完毕!
¹信息:setx PATH "%USERPATH%"
设置变量价值而不是文字 %USERPATH%
;
²信息:使用文字更好,因为行为变得动态而不是静态。即,通过使用文字而不是值,%USERPATH%
将来只需要更改变量。然而,通过使用值(即变量内部的地址),用户变量%PATH%
也必须在更改时更新%USERPATH%
[这是不可取的]。
³信息:不推荐以下方法!虽然可行,但更容易出错。
reg add "HKEY_CURRENT_USER\Environment" /v PATH /d ^%USERPATH^% /f
wmic ENVIRONMENT set name="PATH", variablevalue=^%USERPATH^%, username="%COMPUTERNAME%\\%USERNAME%"
3- 现在只需使用%USERPATH%
setx USERPATH "%USERPATH%;ENTER-NEW-PATH-HERE"
提示:继续之前,请关闭所有程序,尤其是终端。
4- 重新检查
echo %Path%
reg query "HKCU\Environment" /v Path
reg query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v Path
wmic ENVIRONMENT where "username='<SYSTEM>' AND name='path'" get variablevalue
wmic ENVIRONMENT where "username='%COMPUTERNAME%\\%USERNAME%' AND name='path'" get variablevalue
5- 目视检查
rundll32 sysdm.cpl,EditEnvironmentVariables
信息:仅当以管理员权限启动前一个命令时才可以更改系统 PATH;
6-总结:做吧!/or don't!
REM the commands pre-suppose
REM - non-null path;
REM - functional path;
REM if null or unfunctionl path don't progress!
reg query "HKCU\Environment" /v Path
reg query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v Path
reg EXPORT "HKCU\Environment" UserEnvironment.reg /y
reg EXPORT "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" SystemEnvironment.reg /y
wmic ENVIRONMENT where "username='%COMPUTERNAME%\\%USERNAME%' AND name='Path'" get variablevalue | more +1 > %temp%\userpathvalue.txt
set /P USERPATH=<%temp%\userpathvalue.txt
set "USERPATH=%USERPATH%/"
set "USERPATH=%USERPATH: /=/%"
set "USERPATH=%USERPATH: /=/%"
set "USERPATH=%USERPATH: /=/%"
set "USERPATH=%USERPATH: /=/%"
set "USERPATH=%USERPATH:/=%"
echo %USERPATH%[checking final string] && echo %USERPATH% > %tmp%\0
certutil -f -encodehex "%tmp%\0" "%tmp%\1" 11 | more +4 && type "%tmp%\1"
setx USERPATH "%USERPATH%"
setx PATH ^%USERPATH^%
echo done!
REM setx USERPATH "%USERPATH%;ENTER-NEW-PATH-HERE"
REM please close all programs
干杯!
答案3
看起来此页面可能涵盖了您要执行的操作: 使用 SETX 或 PATHMAN 或其他东西添加 PATH? 或者也许可以为您的问题添加更多细节,以便我们了解这有何不同。但是,该页面针对您询问的相同或类似问题提供了多种解决方案。