본문으로 바로가기

아래와 같이 유저 환경 변수를 추가하고.. 시스템에 반영할 수 있음.

C:\> reg add "HKEY_CURRENT_USER\Environment" /v "TEST" /d "C:\Test" /f
C:\> powershell -command "[Environment]::SetEnvironmentVariable('TEST', 'C:\Test', 'User')"

 

단 시스템 환경변수에 TEST가 이미 있다면 시스템 환경변수가 우선함.

 

그리고.. 환경 변수 추가 전에 특정 프로그램이 실행중이었고 TEST 환경변수를 참조하고 있다면..

프로그램을 재시작해야 함. 이럴 땐 아래와 같이 환경변수 추가 후 메시지 박스를 띄워 주는 것도 도움이 됨.

C:\> powershell -Command "Add-Type -AssemblyName PresentationFramework; [System.Windows.MessageBox]::Show('Please restart the Application.', 'Alert')"

 

이걸 배치파일로 한다면..

REM env.bat

reg add "HKEY_CURRENT_USER\Environment" /v "TEST" /d "C:\Test" /f
powershell -command "[Environment]::SetEnvironmentVariable('TEST', 'C:\Test', 'User')"
powershell -Command "Add-Type -AssemblyName PresentationFramework; [System.Windows.MessageBox]::Show('Please restart the HyperWorks.', 'Alert')"