Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Refresh `cmd` environment from registry

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

Written for my SuperUser post.

@echo off
:: <>, WTFPL v2 <>
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
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"
set __var_name=
goto :eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment