DOS命令添加, 删除, 查询系统环境变量(永久/临时生效)

时间:2022-12-12 09:32:51


DOS命令添加, 删除, 查询系统环境变量(永久/临时生效)


参考: cmd - Is there a command to refresh environment variables from the command prompt in Windows? - Stack Overflow   https://*.com/questions/171588/is-there-a-command-to-refresh-environment-variables-from-the-command-prompt-in-w


1、修改注册表的方法要重启/注销/重启explorer进程才生效(永久的)

reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v path /d "%path%;C:\" /f


2、使用WMIC,立即生效(永久的)(不影响已经打开的cmd, 只影响新打开的cmd)

wmic ENVIRONMENT create name="test1",username="<system>",VariableValue="test_val1"           新增变量

wmic ENVIRONMENT where "name='temp'" get UserName,VariableValue    获取变量
wmic ENVIRONMENT where "name='test1' and username='<system>'" set VariableValue="%path%;e:\tools"      修改变量
wmic ENVIRONMENT where "name='test1'" delete     删除变量


3、要在批处理中立即生效(只是临时的,退出批处理后消失),

批处理中加一句:

path=%path%;C:\





如果使用了上面的第2个方法(WMIC命令), 想要给当前正在运行的cmd刷新修改后的环境变量, 2个办法:

方法1:

::  在批处理脚本中添加以下代码即可
@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 | set /p dummy="Reading environment variables from registry. 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

:: Set these variables
call "%TEMP%\_env.cmd"

echo | set /p dummy="Done"
echo .




方法2:

以下代码保存为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

在需要刷新环境变量的批处理中添加以下代码:

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





以下批处理是通过操作注册表来添加, 删除, 查询系统环境变量


::DOS命令添加, 查询系统环境变量 
@echo off
echo 添加java环境变量
set regpath=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
set evname=JAVA_HOME
set javapath=c:\java\jdk
reg add "%regpath%" /v %evname% /d %javapath% /f

echo.
echo 查询path环境变量
REG QUERY "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path

echo.
echo 查询evname环境变量
REG QUERY "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v %evname%|findstr jdk

echo.
echo 获取path环境变量的具体值(具体值中不能有空格)
::delims=后面有一个空格
for /f "tokens=3 delims= " %%a in ('REG QUERY "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path') do (
set "a=%%a"
echo %%a
)

echo.
echo 获取evname环境变量的具体值(具体值中不能有空格)
::delims=后面有一个空格
for /f "tokens=3 delims= " %%a in ('REG QUERY "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v %evname%') do (
set "a=%%a"
echo %%a
)

echo.
echo 删除java环境变量
reg delete "%regpath%" /v "%evname%" /f
pause>nul
exit