public
Last active

Refresh `cmd` environment from registry

  • Download Gist
README.md
Markdown

This script refreshes environment variables for its parent cmd.exe process according to the values from Registry.

Written for my SuperUser post.

update-env.cmd
Batchfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
@echo off
:: <grawity@gmail.com>, WTFPL v2 <http://sam.zoy.org/wtfpl>
:main
set __append_path=n
:: NOTE: There should be exactly one tab after 'delims=' -- NOT spaces.
for /f "usebackq tokens=1,2,* delims= " %%a in (`reg query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment"`) do call :process "%%~a" "%%~b" "%%~c"
set __append_path=y
for /f "usebackq tokens=1,2,* delims= " %%a in (`reg query HKCU\Environment`) do call :process "%%~a" "%%~b" "%%~c"
set __append_path=
goto :eof
 
:process
set "__var_name=%~1"
if not "%__var_name:~0,4%"==" " goto :eof
set "__var_name=%__var_name:~4%"
if /i "%__var_name%"=="PATH" (
if "%__append_path%"=="y" (
set "%__var_name%=%PATH%;%~3"
goto :process_next
)
)
set "%__var_name%=%~3"
 
:process_next
set __var_name=
goto :eof

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.