OS 레벨 구성에서 환경 변수를 제거하는 명령 행
Windows에는 다음 setx
명령이 있습니다.
Description:
Creates or modifies environment variables in the user or system
environment.
따라서 다음과 같이 변수를 설정할 수 있습니다.
setx FOOBAR 1
그리고 다음과 같이 값을 지울 수 있습니다.
setx FOOBAR ""
그러나 변수는 제거되지 않습니다. 레지스트리에 남아 있습니다.
그렇다면 실제로 변수를 어떻게 제거 하시겠습니까?
현재 환경 (에서 변수를 제거 하지 영구적으로) :
set FOOBAR=
사용자 환경 에서 변수를 영구적으로 제거하려면 (기본 위치 setx
) :
REG delete HKCU\Environment /F /V FOOBAR
시스템 환경 에서 변수가 설정되어있는 경우 (예 : 원래로 설정 한 경우 setx /M
) 관리자로 다음을 실행하십시오.
REG delete "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /F /V FOOBAR
참고 : REG
위 의 명령은 기존 프로세스 (및 기존 프로세스에서 분기 된 일부 새 프로세스)에는 영향을 미치지 않으므로 변경 사항이 즉시 적용되는 것이 중요한 경우 로그 아웃했다가 다시 로그인하거나 재부팅 할 수 있습니다.
변수를 영구적으로 제거하지 않고 현재 명령 세션에서 변수를 제거하려면 일반 내장 set
명령을 사용하십시오 . 등호 뒤에 아무것도 넣지 마십시오 .
set FOOBAR=
확인하려면 set
인수없이 실행 하고 현재 환경을 확인하십시오. 변수가 목록에서 완전히 없어야합니다.
참고 : 현재 환경 에서만 변수가 제거 되며 레지스트리에 대한 변경 내용은 유지되지 않습니다. 새로운 명령 프로세스가 시작되면 변수가 다시 나타납니다.
이것은 상당히 다루어졌지만 중요한 정보가 빠져 있습니다. 바라건대, 나는 이것이 어떻게 작동하는지 정리하고 지친 여행자에게 약간의 구호를 줄 수 있습니다. :-)
현재 프로세스에서 삭제
분명히 모든 사람은 현재 프로세스에서 환경 변수를 삭제하기 위해이 작업을 수행한다는 것을 알고 있습니다.
set FOO=
영구 삭제
시스템 전체와 사용자의 두 가지 환경 변수 세트가 있습니다.
사용자 환경 변수 삭제 :
reg delete "HKCU\Environment" /v FOO /f
시스템 전체 환경 변수 삭제 :
REG delete "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /F /V FOO
재부팅하지 않고 값 적용
여기에 빠진 마술 정보가 있습니다! 왜 그렇게 한 후에 새 명령 창을 시작할 때 환경 변수가 여전히 있는지 궁금합니다. 그 이유는 explorer.exe가 환경을 업데이트하지 않았기 때문입니다. 한 프로세스가 다른 프로세스를 시작하면 새 프로세스는 프로세스를 시작한 프로세스에서 환경을 상속합니다.
재부팅하지 않고이 문제를 해결하는 방법에는 두 가지가 있습니다. 가장 무차별적인 방법은 explorer.exe 프로세스를 종료하고 다시 시작하는 것입니다. 작업 관리자 에서이를 수행 할 수 있습니다 . 그러나이 방법은 권장하지 않습니다.
다른 방법은 explorer.exe에 환경이 변경되었으며 다시 읽도록 알리는 것입니다. 이는 Windows 메시지 (WM_SETTINGCHANGE)를 브로드 캐스트하여 수행됩니다. 간단한 PowerShell 스크립트로이를 수행 할 수 있습니다. 이 작업을 쉽게 수행 할 수는 있지만 스크립트 변경 후 업데이트 창 설정 에서 하나를 발견했습니다 .
if (-not ("win32.nativemethods" -as [type])) {
add-type -Namespace Win32 -Name NativeMethods -MemberDefinition @"
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SendMessageTimeout(
IntPtr hWnd, uint Msg, UIntPtr wParam, string lParam,
uint fuFlags, uint uTimeout, out UIntPtr lpdwResult);
"@
}
$HWND_BROADCAST = [intptr]0xffff;
$WM_SETTINGCHANGE = 0x1a;
$result = [uintptr]::zero
[win32.nativemethods]::SendMessageTimeout($HWND_BROADCAST, $WM_SETTINGCHANGE,[uintptr]::Zero, "Environment", 2, 5000, [ref]$result);
요약
따라서 "FOO"라는 사용자 환경 변수를 삭제하고 나중에 실행하는 프로세스에 변경 사항을 반영하려면 다음을 수행하십시오.
- PowerShell 스크립트를 파일에 저장합니다 (updateenv.ps1이라고 함).
- 명령 행에서이를 수행하십시오. reg delete "HKCU \ Environment"/ v FOO / f
- updateenv.ps1을 실행하십시오.
- 명령 프롬프트를 닫았다가 다시 열면 환경 변수가 더 이상 정의되지 않은 것을 볼 수 있습니다.
Note, you'll probably have to update your PowerShell settings to allow you to run this script, but I'll leave that as a Google-fu exercise for you.
I agree with CupawnTae.
SET
is not useful for changes to the master environment.
FYI: System variables are in HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
(a good deal longer than user vars).
The full command for a system var named FOOBAR therefore is:
REG delete "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /F /V FOOBAR
(Note the quotes required to handle the space.)
It is too bad the setx
command doesn't support a delete syntax. :(
PS: Use responsibly - If you kill your path variable, don't blame me!
The command in DougWare's answer did not work, but this did:
reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v FOOBAR /f
The shortcut HKLM
can be used for HKEY_LOCAL_MACHINE
.
From PowerShell you can use the .NET [System.Environment]::SetEnvironmentVariable()
method:
To remove a user environment variable named
FOO
:[Environment]::SetEnvironmentVariable('FOO', $null, 'User')
Note that $null
is used to better signal the intent to remove the variable, though technically it is effectively the same as passing ''
in this case.
To remove a system (machine-level) environment variable named
FOO
- requires elevation (must be run as administrator):[Environment]::SetEnvironmentVariable('FOO', $null, 'Machine')
Aside from faster execution, the advantage over the reg.exe
-based method is that other applications are notified of the change, via a WM_SETTINGCHANGE
message (though not all applications listen to that message).
setx FOOBAR ""
just causes the value of FOOBAR to be a null string. (Although, it shows with the set
command with the "" so maybe double-quotes is the string.)
I used:
set FOOBAR=
and then FOOBAR was no longer listed in the set command. (Log-off was not required.)
Windows 7 32 bit, using the command prompt, non-administrator is what I used. (Not cmd or Windows + R, which might be different.)
BTW, I did not see the variable I created anywhere in the registry after I created it. I'm using RegEdit not as administrator.
You can also create a small VBScript script:
Set env = CreateObject("WScript.Shell").Environment("System")
If env(WScript.Arguments(0)) <> vbNullString Then env.Remove WScript.Arguments(0)
Then call it like %windir%\System32\cscript.exe //Nologo "scriptname.vbs" FOOBAR
.
The disadvantage is you need an extra script, but it does not require a reboot.
'IT' 카테고리의 다른 글
iPhone Safari Web App이 새 창에서 링크를 엽니 다 (0) | 2020.06.09 |
---|---|
Eclipse IDE에서 키보드 단축키를 수정하는 방법은 무엇입니까? (0) | 2020.06.09 |
자식 요소 위로 드래그하면 부모 요소의 'dragleave'가 발생합니다. (0) | 2020.06.09 |
SOAP 요청을 보내고 응답을받는 클라이언트 (0) | 2020.06.09 |
Visual Studio Code에 Nuget 패키지 설치 (0) | 2020.06.09 |