如果修改或添加环境变量,则必须重新启动命令提示符。是否有一个命令,我可以执行,将这样做而不重新启动CMD?


当前回答

您可以使用vbs脚本捕获系统环境变量,但是需要使用bat脚本来实际更改当前环境变量,因此这是一种组合解决方案。

创建一个名为resetvars的文件。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("System")
for each sitem in oEnv 
    oFile.WriteLine("SET " & sitem)
next
path = oEnv("PATH")

set oEnv=oShell.Environment("User")
for each sitem in oEnv 
    oFile.WriteLine("SET " & sitem)
next

path = path & ";" & oEnv("PATH")
oFile.WriteLine("SET PATH=" & path)
oFile.Close

创建另一个包含以下代码的文件名resetvars.bat,相同的位置:

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

当您想要刷新环境变量时,只需运行resetvars.bat


护教学:

我提出这个解决方案时遇到的两个主要问题是

a.我找不到一个直接的方法将环境变量从vbs脚本导出回命令提示符,并且

b. PATH环境变量是用户变量和系统PATH变量的连接。

我不确定用户和系统之间的冲突变量的一般规则是什么,所以我选择让用户覆盖系统,除了在PATH变量中,它是专门处理的。

我使用奇怪的vbs+bat+临时bat机制来解决从vbs导出变量的问题。

注意:此脚本不删除变量。

这可能是可以改进的。

添加

如果你需要将环境从一个cmd窗口导出到另一个窗口,使用这个脚本(让我们称之为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)
next
oFile.Close

exportvars运行。VBS,然后切换到要导出的窗口,输入:

"%TEMP%\resetvars.bat"

其他回答

根据设计,Windows没有内置机制来将环境变量添加/更改/删除传播到已经运行的cmd.exe中,无论是从另一个cmd.exe还是从“我的电脑->属性->高级设置->环境变量”。

如果在现有的开放命令提示符范围之外修改或添加新的环境变量,则需要重新启动命令提示符,或者在现有的命令提示符中使用SET手动添加。

最新接受的答案通过手动刷新脚本中的所有环境变量显示了部分解决方案。该脚本处理在“My Computer…”中全局更改环境变量的用例。但是如果一个环境变量在一个cmd.exe中被更改,脚本不会将其传播到另一个运行cmd.exe的环境变量中。

我喜欢巧克力之后的方法,就像匿名懦夫的回答,因为这是一个纯粹的批量处理方法。但是,它会留下一个临时文件和一些临时变量。我给自己做了一个更干净的版本。

在路径的某个地方创建一个文件refreshv .bat。通过执行refreshEnv来刷新控制台环境。

@ECHO OFF
REM Source found on https://github.com/DieterDePaepe/windows-scripts
REM Please share any improvements made!

REM Code inspired by http://stackoverflow.com/questions/171588/is-there-a-command-to-refresh-environment-variables-from-the-command-prompt-in-w

IF [%1]==[/?] GOTO :help
IF [%1]==[/help] GOTO :help
IF [%1]==[--help] GOTO :help
IF [%1]==[] GOTO :main

ECHO Unknown command: %1
EXIT /b 1 

:help
ECHO Refresh the environment variables in the console.
ECHO.
ECHO   refreshEnv       Refresh all environment variables.
ECHO   refreshEnv /?        Display this help.
GOTO :EOF

:main
REM Because the environment variables may refer to other variables, we need a 2-step approach.
REM One option is to use delayed variable evaluation, but this forces use of SETLOCAL and
REM may pose problems for files with an '!' in the name.
REM The option used here is to create a temporary batch file that will define all the variables.

REM Check to make sure we don't overwrite an actual file.
IF EXIST %TEMP%\__refreshEnvironment.bat (
  ECHO Environment refresh failed!
  ECHO.
  ECHO This script uses a temporary file "%TEMP%\__refreshEnvironment.bat", which already exists. The script was aborted in order to prevent accidental data loss. Delete this file to enable this script.
  EXIT /b 1
)

REM Read the system environment variables from the registry.
FOR /F "usebackq tokens=1,2,* skip=2" %%I IN (`REG QUERY "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"`) DO (
  REM /I -> ignore casing, since PATH may also be called Path
  IF /I NOT [%%I]==[PATH] (
    ECHO SET %%I=%%K>>%TEMP%\__refreshEnvironment.bat
  )
)

REM Read the user environment variables from the registry.
FOR /F "usebackq tokens=1,2,* skip=2" %%I IN (`REG QUERY HKCU\Environment`) DO (
  REM /I -> ignore casing, since PATH may also be called Path
  IF /I NOT [%%I]==[PATH] (
    ECHO SET %%I=%%K>>%TEMP%\__refreshEnvironment.bat
  )
)

REM PATH is a special variable: it is automatically merged based on the values in the
REM system and user variables.
REM Read the PATH variable from the system and user environment variables.
FOR /F "usebackq tokens=1,2,* skip=2" %%I IN (`REG QUERY "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PATH`) DO (
  ECHO SET PATH=%%K>>%TEMP%\__refreshEnvironment.bat
)
FOR /F "usebackq tokens=1,2,* skip=2" %%I IN (`REG QUERY HKCU\Environment /v PATH`) DO (
  ECHO SET PATH=%%PATH%%;%%K>>%TEMP%\__refreshEnvironment.bat
)

REM Load the variable definitions from our temporary file.
CALL %TEMP%\__refreshEnvironment.bat

REM Clean up after ourselves.
DEL /Q %TEMP%\__refreshEnvironment.bat

ECHO Environment successfully refreshed.

我想到的最好的方法是只做一个Registry查询。下面是我的例子。

In my example I did an install using a Batch file that added new environment variables. I needed to do things with this as soon as the install was complete, but was unable to spawn a new process with those new variables. I tested spawning another explorer window and called back to cmd.exe and this worked but on Vista and Windows 7, Explorer only runs as a single instance and normally as the person logged in. This would fail with automation since I need my admin creds to do things regardless of running from local system or as an administrator on the box. The limitation to this is that it does not handle things like path, this only worked on simple enviroment variables. This allowed me to use a batch to get over to a directory (with spaces) and copy in files run .exes and etc. This was written today from may resources on stackoverflow.com

原批处理调用新批处理:

testenvget。cmd SDROOT(或任何变量)

@ECHO OFF
setlocal ENABLEEXTENSIONS
set keyname=HKLM\System\CurrentControlSet\Control\Session Manager\Environment
set value=%1
SET ERRKEY=0

REG QUERY "%KEYNAME%" /v "%VALUE%" 2>NUL| FIND /I "%VALUE%"
IF %ERRORLEVEL% EQU 0 (
ECHO The Registry Key Exists 
) ELSE (
SET ERRKEY=1
Echo The Registry Key Does not Exist
)

Echo %ERRKEY%
IF %ERRKEY% EQU 1 GOTO :ERROR

FOR /F "tokens=1-7" %%A IN ('REG QUERY "%KEYNAME%" /v "%VALUE%" 2^>NUL^| FIND /I "%VALUE%"') DO (
ECHO %%A
ECHO %%B
ECHO %%C
ECHO %%D
ECHO %%E
ECHO %%F
ECHO %%G
SET ValueName=%%A
SET ValueType=%%B
SET C1=%%C
SET C2=%%D
SET C3=%%E
SET C4=%%F
SET C5=%%G
)

SET VALUE1=%C1% %C2% %C3% %C4% %C5%
echo The Value of %VALUE% is %C1% %C2% %C3% %C4% %C5%
cd /d "%VALUE1%"
pause
REM **RUN Extra Commands here**
GOTO :EOF

:ERROR
Echo The the Enviroment Variable does not exist.
pause
GOTO :EOF

还有另一种方法,我从各种不同的想法中想出的。请看下文。这基本上会得到最新的路径变量从注册表然而,这将导致一些问题,因为注册表查询将给出变量本身,这意味着到处都有一个变量,这将不会工作,所以为了解决这个问题,我基本上加倍的路径。非常讨厌的。更可取的方法是: 设置路径=%路径%;C:\Program Files\Software....\

不管这是新的批处理文件,请谨慎使用。

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
set org=%PATH%
for /f "tokens=2*" %%A in ('REG QUERY "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path ^|FIND /I "Path"') DO (
SET path=%%B
)
SET PATH=%org%;%PATH%
set path

您可以使用vbs脚本捕获系统环境变量,但是需要使用bat脚本来实际更改当前环境变量,因此这是一种组合解决方案。

创建一个名为resetvars的文件。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("System")
for each sitem in oEnv 
    oFile.WriteLine("SET " & sitem)
next
path = oEnv("PATH")

set oEnv=oShell.Environment("User")
for each sitem in oEnv 
    oFile.WriteLine("SET " & sitem)
next

path = path & ";" & oEnv("PATH")
oFile.WriteLine("SET PATH=" & path)
oFile.Close

创建另一个包含以下代码的文件名resetvars.bat,相同的位置:

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

当您想要刷新环境变量时,只需运行resetvars.bat


护教学:

我提出这个解决方案时遇到的两个主要问题是

a.我找不到一个直接的方法将环境变量从vbs脚本导出回命令提示符,并且

b. PATH环境变量是用户变量和系统PATH变量的连接。

我不确定用户和系统之间的冲突变量的一般规则是什么,所以我选择让用户覆盖系统,除了在PATH变量中,它是专门处理的。

我使用奇怪的vbs+bat+临时bat机制来解决从vbs导出变量的问题。

注意:此脚本不删除变量。

这可能是可以改进的。

添加

如果你需要将环境从一个cmd窗口导出到另一个窗口,使用这个脚本(让我们称之为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)
next
oFile.Close

exportvars运行。VBS,然后切换到要导出的窗口,输入:

"%TEMP%\resetvars.bat"

这并没有直接回答你的问题,但如果你只是想要进程间通信,你可以使用PowerShell,你可以只使用剪贴板。

在一个过程中

Set-Clipboard (MyText”)

在单独的过程中

$ clipValue = Get-Clipboard

然后你可以使用clipValue作为任何其他字符串。这实际上使您能够使用CSV文本字符串将整个环境变量列表发送到另一个进程