How to export and import environment variables in windows?

224,508

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,

  1. export .reg to current directory
  2. 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
Share:
224,508
Admin
Author by

Admin

Updated on September 29, 2021

Comments

  • Admin
    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
    PatrickT about 11 years
    On my machine, PATH > (or PATH >>) returned an empty text file, while SET >> worked. Windows 7 x64 bits.
  • Kissaki
    Kissaki almost 11 years
    Worked for me on Win7x64. Not sure what the requirements are.
  • Silvertiger
    Silvertiger almost 10 years
    run the program regedit, highlight the keys in question and then use the "file -> export" option so save it as a file
  • Ash
    Ash about 9 years
    How do you import back all the exported Env. Vars. from allvariables.txt
  • thanos.a
    thanos.a about 8 years
    the import is done simply with double clicking the .reg file while having admin permissions.
  • ejbytes
    ejbytes almost 8 years
    Very 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
    ejbytes almost 8 years
    NOTE: 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
    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
    Rakesh N over 6 years
    the 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
    GoldDragonTSU about 5 years
    On 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
    JinSnow almost 5 years
    this command did work: set > "C:\Users\xx\Desktop\envir variable.txt"
  • Azurespot
    Azurespot over 4 years
    From which folder do you execute this command? Mine said could not find path.
  • Azurespot
    Azurespot over 4 years
    Thanks @JinSnow this worked for me. The command by Kushal gave me an "access denied" error.
  • Gabriel
    Gabriel over 4 years
    To 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
    Lorem Ipsum almost 3 years
    This appears to fail when importing paths containing spaces, such as "Program Files".
  • Henke
    Henke over 2 years
    In Windows 10, when importing, I believe most users will want to use SETX rather than SET – to make the change permanent. (Not having to import every time a new command window is opened.)
  • Sn3akyP3t3
    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.