registry - variables中文 - windows set environment variable command line




環境變量在註冊表中存儲在哪裡? (3)

CMD:

reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
reg query HKEY_CURRENT_USER\Environment

電源外殼:

gi "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
gi HKCU:\Environment

Powershell / .NET:(請參閱https://msdn.microsoft.com/en-us/library/system.environmentvariabletarget(v=vs.110).aspx

[System.Environment]::GetEnvironmentVariables([System.EnvironmentVariableTarget]::Machine)
[System.Environment]::GetEnvironmentVariables([System.EnvironmentVariableTarget]::User)

我需要遠程訪問一個環境變量。 為此,我認為最好的方法是從註冊表中讀取它。

環境變量在註冊表中存儲在哪裡?


以下是它們通過Server 2012 R2存儲在XP上的位置:

用戶變量

HKEY_CURRENT_USER\Environment

系統變量

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

我總是遇到問題,我做了一個getx.bat:

::getx %envvar% [\m]
::reads envvar from user enviroment variable and stores it in getxvalue variable
::with \m read system enviroment

@SETLOCAL EnableDelayedExpansion
@echo OFF

@set l_regpath="HKEY_CURRENT_USER\Environment"
@if "\m"=="%2" set l_regpath="HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"

::REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PATH /t REG_SZ /f /d "%PATH%"
::@REG QUERY %l_regpath% /v %1 /S

@FOR /F "tokens=*" %%A IN ('REG QUERY %l_regpath% /v %1 /S') DO (
@  set l_a=%%A
@   if NOT "!l_a!"=="!l_a:    =!" set l_line=!l_a! 
)

::delimiter is four spaces change it to tab \t
@set l_line=!l_line!
@set l_line=%l_line:    =   %

@set getxvalue=

@FOR /F "tokens=3* delims=  " %%A IN ("%l_line%") DO (
@   set getxvalue=%%A
)
@set getxvalue=!getxvalue!
@echo %getxvalue% > getxfile.tmp.txt
@ENDLOCAL

::we already used tab as delimiter
@FOR /F "delims=    " %%A IN (getxfile.tmp.txt) DO (
    @set getxvalue=%%A
)
@del getxfile.tmp.txt

@echo ON