创建具有可扩展值的用户环境变量 [英] Create user environment variable with expandable value

查看:32
本文介绍了创建具有可扩展值的用户环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在 Powershell 脚本 (Windows 10) 中轻松创建用户环境变量.

I can create a user environment variable within a Powershell script (Windows 10) easily.

[System.Environment]::SetEnvironmentVariable('name', 'value', 'User')

当变量应该包含另一个用户环境变量(如%OTHERVAR%;static_part)时,我发现类型必须是ExpandString.这可以按如下方式完成

When the variable should contain another user environment variable like %OTHERVAR%;static_part, I found that the type must be ExpandString. This can be done as follows

Set-ItemProperty HKCU:\Environment 'name' 'value' -Type ExpandString

所以,我写了下面的代码

So, I wrote the following code

[System.Environment]::SetEnvironmentVariable('M2_HOME', 'C:\dev\app\apache-maven-3.3.9', 'User')
# special treatment to get expandable type
Set-ItemProperty HKCU:\Environment 'PATH' '%M2_HOME%\bin;C:\Users\UID20852\AppData\Local\Microsoft\WindowsApps;' -Type ExpandString

Write-Host ([System.Environment]::GetEnvironmentVariable('M2_HOME', 'User'))
Write-Host ([System.Environment]::GetEnvironmentVariable('PATH', 'User'))
Write-Host 

输出:

C:\dev\app\apache-maven-3.3.9
C:\dev\app\apache-maven-3.3.9\bin;C:\Users\UID20852\AppData\Local\Microsoft\WindowsApps;

一切看起来都很好,两个变量都存在.当我打开 Windows 控件编辑用户环境变量时,我看到两个变量和 PATH 的值被扩展.即使在 regedit 中,我也看到变量的类型是正确的.

Everything is looking fine, both variables exist. When I open the Windows control to edit the user environment variables, I see both variables and the value of PATH is expanded. Even in regedit I see the type of the variables is correct.

但是,当我打开一个新的 cmd 并尝试从 %M2_HOME%\bin 运行二进制文件(应该在 cmdcode>PATH 现在),它失败.

However, when I open a new cmd and try to run a binary from %M2_HOME%\bin (should be on the PATH now), it fails.

手动解决问题我只需要打开windows控件编辑用户环境变量,双击PATH变量,edit nothing,然后关闭再次(我猜它重写了它).

To resolve the problem manually I just need to open the Windows control to edit the user environment variables, double-click the PATH variable, edit nothing, and close it again (I guess it re-writes it).

然后,在新的 cmd 中,我可以从 %M2_HOME%\bin 运行二进制文件.

Then, in a new cmd, I can then run binaries from %M2_HOME%\bin.

有什么想法可以让这个 PATH 变量通过脚本工作吗?

Any ideas how I can make this PATH variable work just through the script?

推荐答案

感谢 @PetSerAl.他对首先在 PATH 变量前面加上占位符字符串的回答,然后创建环境变量,之后满足占位符 完美地工作.

Kudos to @PetSerAl. His answer to first prefix the PATH variable with the placeholder string and then create the environment variable that fulfils the placeholder afterwards works perfectly.

# special treatment to get expandable type
Set-ItemProperty HKCU:\Environment 'PATH' '%M2_HOME%\bin;C:\Users\UID20852\AppData\Local\Microsoft\WindowsApps;' -Type ExpandString
[System.Environment]::SetEnvironmentVariable('M2_HOME', 'C:\dev\app\apache-maven-3.3.9', 'User')

如果我理解他在评论中的解释是正确的,因为正常"变量的创建会触发一个事件,该事件立即用占位符更新变量.所以它在脚本完成后立即工作.

If I understand his explanation in the comments correct, because the creation of the "normal" variable fires an event that immediately updates the variable with the placeholder. So it works right after the script is done.

这篇关于创建具有可扩展值的用户环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆