setx를 사용하여 사용자 PATH에 추가

setx를 사용하여 사용자 PATH에 추가

내 시도:

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%;NewPath"사용자 경로를 시스템 경로로 오염시킵니다.

-setx 경로 "%Path%;새 경로" /M사용자 경로로 시스템 경로를 오염시킵니다.

-오염:동영상

명령 프롬프트를 독점적으로 사용하는 NON-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%"

* 보이지 않는 문자를 처리할 때 16진수로 변환하면 식별하기가 더 쉽습니다. 다음을 수행하세요.

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- 1²³ 사용자 경로를 다음으로 %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%"

- 지금은 그냥 사용하세요%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-TL;DR:해!/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를 추가합니까? 아니면 귀하의 질문에 더 자세한 내용을 추가하여 이것이 어떻게 다른지 이해할 수 있습니다. 그러나 해당 페이지에는 귀하가 요청한 것과 동일하거나 유사한 문제에 대한 여러 가지 솔루션이 있습니다.

관련 정보