DOS命令新增, 刪除, 查詢系統環境變數(永久/臨時生效)

guyue35發表於2015-07-07


DOS命令新增, 刪除, 查詢系統環境變數(永久/臨時生效)


參考:
cmd - Is there a command to refresh environment variables from the command prompt in Windows? - Stack Overflow  
https://stackoverflow.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



相關文章