Tyl*_*r S 29 windows powershell environment-variables
Hello我正在尝试使用powershell在这里添加系统变量:

我使用$ env尝试了两种方式:MyTestVariable ="我的测试变量".然而,他们似乎都没有添加到这一部分.我已经尝试重新启动计算机,看看它是否会生效.我和无数其他网站一起看过technet.请帮忙!
Tes*_*ler 47
以管理员身份运行PowerShell(以获取必要的注册表访问权限),然后调用.Net框架进行设置:
[Environment]::SetEnvironmentVariable("MyTestVariable", "MyTestValue", "Machine")
Run Code Online (Sandbox Code Playgroud)
NB.它不会在同一个进程中生效,你必须创建一个新的PowerShell进程才能看到它.
以管理员身份运行PowerShell。如果您要修改环境扩展或环境路径之类的内容,请不要使用它。无需运行refreshEnv,也无需打开新的PowerShell窗口即可看到它
$variableNameToAdd = "mytestVariableName"
$variableValueToAdd = "some environmental value to add"
[System.Environment]::SetEnvironmentVariable($variableNameToAdd, $variableValueToAdd, [System.EnvironmentVariableTarget]::Machine)
[System.Environment]::SetEnvironmentVariable($variableNameToAdd, $variableValueToAdd, [System.EnvironmentVariableTarget]::Process)
[System.Environment]::SetEnvironmentVariable($variableNameToAdd, $variableValueToAdd, [System.EnvironmentVariableTarget]::User)
Run Code Online (Sandbox Code Playgroud)