
我的嘗試:
setx PATH "%PATH%;%~dp0"
如何在系統 PATH 中的所有內容都不會在使用者 PATH 中重複的情況下執行此操作?
答案1
如何在不使系統路徑在使用者路徑中重複的情況下執行此操作?
使用該/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 行相關的所有內容的絕佳參考。
- 設定x- 永久設定環境變量,SETX可用於設定機器(HKLM)或目前登入使用者(HKCU)的環境變數。
答案2
[CMD、WMIC、HKLM、HKCU、SETX、PATH,2001 年]
這個問題可能已有20多年歷史了:
-setx 路徑“%Path%;NewPath”用系統路徑污染用戶路徑。
-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(與沒有相同)
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
資訊:只有使用管理權限啟動上一個指令時才能更改系統路徑;
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? 或者可以為您的問題添加更多詳細信息,以便我們了解其有何不同。但是,該頁面有多種解決方案可以解決與您所問問題相同或相似的問題。