Is there a command to refresh environment variables from the command prompt in Windows? Is there a command to refresh environment variables from the command prompt in Windows? windows windows

Is there a command to refresh environment variables from the command prompt in Windows?


On Windows 7/8/10, you can install Chocolatey, which has a script for this built-in.

After installing Chocolatey, just type refreshenv.


You can capture the system environment variables with a vbs script, but you need a bat script to actually change the current environment variables, so this is a combined solution.

Create a file named resetvars.vbs containing this code, and save it on the path:

Set oShell = WScript.CreateObject("WScript.Shell")filename = oShell.ExpandEnvironmentStrings("%TEMP%\resetvars.bat")Set objFileSystem = CreateObject("Scripting.fileSystemObject")Set oFile = objFileSystem.CreateTextFile(filename, TRUE)set oEnv=oShell.Environment("System")for each sitem in oEnv     oFile.WriteLine("SET " & sitem)nextpath = oEnv("PATH")set oEnv=oShell.Environment("User")for each sitem in oEnv     oFile.WriteLine("SET " & sitem)nextpath = path & ";" & oEnv("PATH")oFile.WriteLine("SET PATH=" & path)oFile.Close

create another file name resetvars.bat containing this code, same location:

@echo off%~dp0resetvars.vbscall "%TEMP%\resetvars.bat"

When you want to refresh the environment variables, just run resetvars.bat


Apologetics:

The two main problems I had coming up with this solution were

a. I couldn't find a straightforward way to export environment variables from a vbs script back to the command prompt, and

b. the PATH environment variable is a concatenation of the user and the system PATH variables.

I'm not sure what the general rule is for conflicting variables between user and system, so I elected to make user override system, except in the PATH variable which is handled specifically.

I use the weird vbs+bat+temporary bat mechanism to work around the problem of exporting variables from vbs.

Note: this script does not delete variables.

This can probably be improved.

ADDED

If you need to export the environment from one cmd window to another, use this script (let's call it exportvars.vbs):

Set oShell = WScript.CreateObject("WScript.Shell")filename = oShell.ExpandEnvironmentStrings("%TEMP%\resetvars.bat")Set objFileSystem = CreateObject("Scripting.fileSystemObject")Set oFile = objFileSystem.CreateTextFile(filename, TRUE)set oEnv=oShell.Environment("Process")for each sitem in oEnv     oFile.WriteLine("SET " & sitem)nextoFile.Close

Run exportvars.vbs in the window you want to export from, then switch to the window you want to export to, and type:

"%TEMP%\resetvars.bat"


Here is what Chocolatey uses.

https://github.com/chocolatey/choco/blob/master/src/chocolatey.resources/redirects/RefreshEnv.cmd

@echo off:::: RefreshEnv.cmd:::: Batch file to read environment variables from registry and:: set session variables to these values.:::: With this batch file, there should be no need to reload command:: environment every time you want environment changes to propagate::echo "RefreshEnv.cmd only works from cmd.exe, please install the Chocolatey Profile to take advantage of refreshenv from PowerShell"echo | set /p dummy="Refreshing environment variables from registry for cmd.exe. Please wait..."goto main:: Set one environment variable from registry key:SetFromReg    "%WinDir%\System32\Reg" QUERY "%~1" /v "%~2" > "%TEMP%\_envset.tmp" 2>NUL    for /f "usebackq skip=2 tokens=2,*" %%A IN ("%TEMP%\_envset.tmp") do (        echo/set "%~3=%%B"    )    goto :EOF:: Get a list of environment variables from registry:GetRegEnv    "%WinDir%\System32\Reg" QUERY "%~1" > "%TEMP%\_envget.tmp"    for /f "usebackq skip=2" %%A IN ("%TEMP%\_envget.tmp") do (        if /I not "%%~A"=="Path" (            call :SetFromReg "%~1" "%%~A" "%%~A"        )    )    goto :EOF:main    echo/@echo off >"%TEMP%\_env.cmd"    :: Slowly generating final file    call :GetRegEnv "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" >> "%TEMP%\_env.cmd"    call :GetRegEnv "HKCU\Environment">>"%TEMP%\_env.cmd" >> "%TEMP%\_env.cmd"    :: Special handling for PATH - mix both User and System    call :SetFromReg "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" Path Path_HKLM >> "%TEMP%\_env.cmd"    call :SetFromReg "HKCU\Environment" Path Path_HKCU >> "%TEMP%\_env.cmd"    :: Caution: do not insert space-chars before >> redirection sign    echo/set "Path=%%Path_HKLM%%;%%Path_HKCU%%" >> "%TEMP%\_env.cmd"    :: Cleanup    del /f /q "%TEMP%\_envset.tmp" 2>nul    del /f /q "%TEMP%\_envget.tmp" 2>nul    :: capture user / architecture    SET "OriginalUserName=%USERNAME%"    SET "OriginalArchitecture=%PROCESSOR_ARCHITECTURE%"    :: Set these variables    call "%TEMP%\_env.cmd"    :: Cleanup    del /f /q "%TEMP%\_env.cmd" 2>nul    :: reset user / architecture    SET "USERNAME=%OriginalUserName%"    SET "PROCESSOR_ARCHITECTURE=%OriginalArchitecture%"    echo | set /p dummy="Finished."    echo .