windows 如何在windows中导出和导入环境变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/559816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:04:33  来源:igfitidea点击:

How to export and import environment variables in windows?

windowsenvironment-variableswindows-xp

提问by max_y

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.

我认为应该是可能的,但不知道该怎么做。谁能帮我?谢谢。

回答by jdigital

You can use RegEdit to export the following two keys:

您可以使用 RegEdit 导出以下两个密钥:

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.

第一组是系统/全局环境变量;第二组是用户级变量。根据需要进行编辑,然后在新机器上导入 .reg 文件。

回答by Kushal Paudyal

I would use the SET command from the command prompt to export all the variables, rather than just PATH as recommended above.

我会在命令提示符下使用 SET 命令来导出所有变量,而不仅仅是上面推荐的 PATH。

C:\> SET >> allvariables.txt

回答by vincsilver

To export user variables, open a command prompt and use regedit with /e

要导出用户变量,请打开命令提示符并使用带有 /e 的 regedit

Example :

例子 :

regedit /e "%userprofile%\Desktop\my_user_env_variables.reg" "HKEY_CURRENT_USER\Environment"

回答by Mithril

Combine @vincsilverand @jdigital's answers with some modifications,

结合@vincsilver@jdigital的答案进行一些修改,

  1. export .regto current directory
  2. add date mark
  1. 导出.reg到当前目录
  2. 添加日期标记

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

回答by Gavin Bunney

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.

如果您想要一个特定的环境变量,那么只需键入它的名称(例如PATH),然后是 a>和要写入的文件名。下面将把 PATH 环境变量转储到一个名为 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.

Windows 注册表将所有环境变量保存在不同的位置,具体取决于您所使用的设置。您可以使用注册表导入/导出命令将它们转移到另一台 PC 中。

For System Variables:

对于系统变量:

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

For User Variables:

对于用户变量:

HKEY_CURRENT_USER\Environment

回答by chunk_split

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.batfile and put in it:

我最喜欢的方法是将它写成一个批处理脚本,将用户变量和系统变量组合成一个备份文件,如下所示,创建一个environment-backup.bat文件并将其放入:

@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.regwhich you can use to re-import existing environment variables. This will add & overridenew variables, but notdelete existing ones :)

这将创建environment-backup.reg可用于重新导入现有环境变量的变量。这将添加和覆盖新变量,但不会删除现有变量:)

回答by fiat

Here is my PowerShell method

这是我的 PowerShell 方法

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)
  1. 获取所有环境变量
  2. 过滤它们
  3. 发出在另一台机器上重新创建它们所需的格式化 PowerShell(假设所有都在机器级别设置)

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

因此,在源机器上运行后,只需将输出传输到目标机器并执行(如果在机器级别设置,则提升提示)