How to export and import environment variables in windows?
Solution 1
You can use RegEdit to export the following two keys:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
HKEY_CURRENT_USER\Environment
The first set are system/global environment variables; the second set are user-level variables. Edit as needed and then import the .reg files on the new machine.
Solution 2
I would use the SET command from the command prompt to export all the variables, rather than just PATH as recommended above.
C:\> SET >> allvariables.txt
To import the variablies, one can use a simple loop:
C:\> for /F %A in (allvariables.txt) do SET %A
Solution 3
To export user variables, open a command prompt and use regedit with /e
Example :
regedit /e "%userprofile%\Desktop\my_user_env_variables.reg" "HKEY_CURRENT_USER\Environment"
Solution 4
Combine @vincsilver and @jdigital's answers with some modifications,
- export
.reg
to current directory - add date mark
code:
set TODAY=%DATE:~0,4%-%DATE:~5,2%-%DATE:~8,2%
regedit /e "%CD%\user_env_variables[%TODAY%].reg" "HKEY_CURRENT_USER\Environment"
regedit /e "%CD%\global_env_variables[%TODAY%].reg" "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
Output would like:
global_env_variables[2017-02-14].reg
user_env_variables[2017-02-14].reg
Solution 5
You can get access to the environment variables in either the command line or in the registry.
Command Line
If you want a specific environment variable, then just type the name of it (e.g. PATH
), followed by a >
, and the filename to write to. The following will dump the PATH environment variable to a file named path.txt.
C:\> PATH > path.txt
Registry Method
The Windows Registry holds all the environment variables, in different places depending on which set you are after. You can use the registry Import/Export commands to shift them into the other PC.
For System Variables:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
For User Variables:
HKEY_CURRENT_USER\Environment
Admin
Updated on September 29, 2021Comments
-
Admin over 2 years
I found it is hard to keep my environment variables sync on different machines. I just want to export the settings from one computer and import to other ones.
I think it should be possible, but don't know how to do it. Can anyone help me? Thanks.
-
PatrickT about 11 yearsOn my machine, PATH > (or PATH >>) returned an empty text file, while SET >> worked. Windows 7 x64 bits.
-
Kissaki almost 11 yearsWorked for me on Win7x64. Not sure what the requirements are.
-
Silvertiger almost 10 yearsrun the program regedit, highlight the keys in question and then use the "file -> export" option so save it as a file
-
Ash about 9 yearsHow do you import back all the exported Env. Vars. from allvariables.txt
-
thanos.a about 8 yearsthe import is done simply with double clicking the .reg file while having admin permissions.
-
ejbytes almost 8 yearsVery nice. I just lost all my path vars doing a bad path set and had to do a system restore. Luckily I had a recent update as of today earlier. I just did a backup with this command. Nice. That registry solution only holds the original vars, but nothing that "you" (as a programmer say for new builds) created or any new install created.
-
ejbytes almost 8 yearsNOTE: This doesn't get all Environment Variables(EV)! I just did a command set path and messed up all my EV. I went to this registry and only the original EV were there. I did a system restore and got all my missing EV back to the PATH var. This registry only holds a few necessary EV, but not any of your program's EV nor any paths you set manually. BEWARE! On a command line: echo path > mybackup.txt or set > mybackup.txt for the whole backup on ALL vars/paths and ALL sys vars/paths.
-
Alexander about 7 years@ejbytes This does copy your global variables that you set but it is probably better to export them with the CLI instead of through the registry.
-
Rakesh N over 6 yearsthe TODAY variable depends on %DATE% which is dependent on how Windows Locale preferences. The above command does not work for India. This works --> set TODAY=%DATE:~4,2%-%DATE:~7,2%-%DATE:~10,4%
-
GoldDragonTSU about 5 yearsOn Windows 7 64-bit, if there are two identically named variables at the User level and the System level, this command gives precedence to the User level variable and omits the System level one. This behavior makes sense, but figured it might be worth a mention in case anyone was expecting the full set from each variable type.
-
JinSnow almost 5 yearsthis command did work:
set > "C:\Users\xx\Desktop\envir variable.txt"
-
Azurespot over 4 yearsFrom which folder do you execute this command? Mine said could not find path.
-
Azurespot over 4 yearsThanks @JinSnow this worked for me. The command by Kushal gave me an "access denied" error.
-
Gabriel over 4 yearsTo answer Ash, you can use a simple for loop in the cmd prompt to import back all the variables:
for /F %A in (allvariables.txt) do SET %A
-
Lorem Ipsum almost 3 yearsThis appears to fail when importing paths containing spaces, such as "Program Files".
-
Henke over 2 yearsIn Windows 10, when importing, I believe most users will want to use
SETX
rather thanSET
– to make the change permanent. (Not having to import every time a new command window is opened.) -
Sn3akyP3t3 over 2 years@Henke is correct. Use SETX if you want it to persist. Also, warning that this approach will mash both User and System env variables into a single file on read which it will load from and save that mixed file into the User env variables. If that is not desirable then manually extract System env variables from that mixed file into a separate file and use the /M flag with those to import them as System env variables.