How to export and import environment variables in windows?

后端 未结 8 1450
粉色の甜心
粉色の甜心 2020-12-22 15:36

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 thi

相关标签:
8条回答
  • 2020-12-22 15:45

    My favorite method for doing this is to write it out as a batch script to combine both user variables and system variables into a single backup file like so, create an environment-backup.bat file and put in it:

    @echo off
    :: RegEdit can only export into a single file at a time, so create two temporary files.
    regedit /e "%CD%\environment-backup1.reg" "HKEY_CURRENT_USER\Environment"
    regedit /e "%CD%\environment-backup2.reg" "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
    
    :: Concatenate into a single file and remove temporary files.
    type "%CD%\environment-backup1.reg" "%CD%\environment-backup2.reg" > environment-backup.reg
    del "%CD%\environment-backup1.reg"
    del "%CD%\environment-backup2.reg"
    

    This creates environment-backup.reg which you can use to re-import existing environment variables. This will add & override new variables, but not delete existing ones :)

    0 讨论(0)
  • 2020-12-22 15:48

    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.

    0 讨论(0)
  • 2020-12-22 15:49

    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
    
    0 讨论(0)
  • 2020-12-22 15:52

    A PowerShell script based on @Mithrl's answer

    # export_env.ps1
    $Date = Get-Date
    $DateStr = '{0:dd-MM-yyyy}' -f $Date
    
    mkdir -Force $PWD\env_exports | Out-Null
    
    regedit /e "$PWD\env_exports\user_env_variables[$DateStr].reg" "HKEY_CURRENT_USER\Environment"
    regedit /e "$PWD\env_exports\global_env_variables[$DateStr].reg" "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
    
    0 讨论(0)
  • 2020-12-22 16:03

    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
    
    0 讨论(0)
  • 2020-12-22 16:04

    Here is my PowerShell method

    gci env:* | sort-object name | Where-Object {$_.Name -like "MyApp*"} | Foreach {"[System.Environment]::SetEnvironmentVariable('$($_.Name)', '$($_.Value)', 'Machine')"}
    

    What it does

    1. Scoops up all environment variables
    2. Filters them
    3. Emits the formatted PowerShell needed to recreate them on another machine (assumes all are set at machine level)

    So after running this on the source machine, simply transfer output onto the target machine and execute (elevated prompt if setting at machine level)

    0 讨论(0)
提交回复
热议问题