1+ @ echo off
2+ :: This file is taken from chocolatey:
3+ :: https://github.com/chocolatey/choco/blob/master/src/chocolatey.resources/redirects/RefreshEnv.cmd
4+ ::
5+ :: RefreshEnv.cmd
6+ ::
7+ :: Batch file to read environment variables from registry and
8+ :: set session variables to these values.
9+ ::
10+ :: With this batch file, there should be no need to reload command
11+ :: environment every time you want environment changes to propagate
12+
13+ :: echo "RefreshEnv.cmd only works from cmd.exe, please install the Chocolatey Profile to take advantage of refreshenv from PowerShell"
14+ echo | set /p dummy = " Refreshing environment variables from registry for cmd.exe. Please wait..."
15+
16+ goto main
17+
18+ :: Set one environment variable from registry key
19+ :SetFromReg
20+ " %WinDir% \System32\Reg" QUERY " %~1 " /v " %~2 " > " %TEMP% \_envset.tmp" 2 > NUL
21+ for /f " usebackq skip=2 tokens=2,*" %%A IN (" %TEMP% \_envset.tmp" ) do (
22+ echo/set " %~3 =%%B "
23+ )
24+ goto :EOF
25+
26+ :: Get a list of environment variables from registry
27+ :GetRegEnv
28+ " %WinDir% \System32\Reg" QUERY " %~1 " > " %TEMP% \_envget.tmp"
29+ for /f " usebackq skip=2" %%A IN (" %TEMP% \_envget.tmp" ) do (
30+ if /I not " %%~A " == " Path" (
31+ call :SetFromReg " %~1 " " %%~A " " %%~A "
32+ )
33+ )
34+ goto :EOF
35+
36+ :main
37+ echo/@ echo off > " %TEMP% \_env.cmd"
38+
39+ :: Slowly generating final file
40+ call :GetRegEnv " HKLM\System\CurrentControlSet\Control\Session Manager\Environment" >> " %TEMP% \_env.cmd"
41+ call :GetRegEnv " HKCU\Environment" >> " %TEMP% \_env.cmd" >> " %TEMP% \_env.cmd"
42+
43+ :: Special handling for PATH - mix both User and System
44+ call :SetFromReg " HKLM\System\CurrentControlSet\Control\Session Manager\Environment" Path Path_HKLM >> " %TEMP% \_env.cmd"
45+ call :SetFromReg " HKCU\Environment" Path Path_HKCU >> " %TEMP% \_env.cmd"
46+
47+ :: Caution: do not insert space-chars before >> redirection sign
48+ echo/set " Path=%% Path_HKLM%% ;%% Path_HKCU%% " >> " %TEMP% \_env.cmd"
49+
50+ :: Cleanup
51+ del /f /q " %TEMP% \_envset.tmp" 2 > nul
52+ del /f /q " %TEMP% \_envget.tmp" 2 > nul
53+
54+ :: capture user / architecture
55+ SET " OriginalUserName = %USERNAME% "
56+ SET " OriginalArchitecture = %PROCESSOR_ARCHITECTURE% "
57+
58+ :: Set these variables
59+ call " %TEMP% \_env.cmd"
60+
61+ :: reset user / architecture
62+ SET " USERNAME = %OriginalUserName% "
63+ SET " PROCESSOR_ARCHITECTURE = %OriginalArchitecture% "
64+
65+ echo | set /p dummy = " Finished."
66+ echo .
0 commit comments