231

I need to access an environment variable remotely. To do this, I think the best way is to read it from registry.

Where are environment variables stored in the Windows Registry?

4 Answers 4

340

Here's where they're stored on Windows XP through Windows Server 2012 R2:

User Variables

HKEY_CURRENT_USER\Environment 

System Variables

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment 
Sign up to request clarification or add additional context in comments.

14 Comments

Remember to restart your system in order to put your changes into effect.
You don't need to restart. Just kill Explorer.exe and bring it back alive. It's the parent process for e.g. cmd.exe (when started from the Start menu)
Processes read in system env variables at the time they start. So with something like IIS, restarting that service should bring in the updated values.
User path variables (My Documents, AppData, etc) are stored at HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
Windows 7 uses the same registry locations.
|
25

There is a more efficient way of doing this in Windows 7. SETX is installed by default and supports connecting to other systems.

To modify a remote system's global environment variables, you would use

setx /m /s HOSTNAME-GOES-HERE VariableNameGoesHere VariableValueGoesHere 

This does not require restarting Windows Explorer.

/M Specifies that the variable should be set in the system wide (HKEY_LOCAL_MACHINE) environment. The default is to set the variable under the HKEY_CURRENT_USER environment.

/S system Specifies the remote system to connect to.

1 Comment

Be careful with this, as setx truncates everything after the 1024 charachters!!! superuser.com/questions/387619/…
12

System environment variables followed by User environment variables:

CMD:

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

PowerShell:

Get-Item "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" Get-Item HKCU:\Environment 

Powershell/.NET: (see EnvironmentVariableTarget Enum)

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

Comments

3

I always had problems with that, and I made a getx.bat script:

:: getx %envvar% [\m] :: Reads envvar from user environment variable and stores it in the getxvalue variable :: with \m read system environment @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 a delimiter @FOR /F "delims= " %%A IN (getxfile.tmp.txt) DO ( @set getxvalue=%%A ) @del getxfile.tmp.txt @echo ON 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.