【发布时间】:2012-09-26 05:08:27
【问题描述】:
我在我的PATH 变量中插入一个变量字符串。我按以下方式设置变量:
$var="MyTestPath"
$mypath=[environment]::GetEnvironmentVariable("PATH",[system.environmentvariabletarget]::User)
[environment]::SetEnvironmentVariable("TEST",$var,[system.environmentvariabletarget]::User)
[environment]::SetEnvironmentVariable("PATH",$mypath+";%TEST%",[system.environmentvariabletarget]::User)
上面的代码对我不起作用。当我检查新 shell 中的路径时,%TEST% 变量不会自行扩展。它显示以%TEST% 结尾的新路径。当我从 GUI 或 Windows shell 提示符设置它时,这一直有效。当从 PowerShell 设置变量时,为什么这种行为会有所不同? PowerShell 中是否删除了此功能?
我不想执行以下操作,因为它会在我每次运行脚本时不断将我的变量添加到路径中。
[environment]::SetEnvironmentVariable("PATH",$mypath+";"+$var,[system.environmentvariabletarget]::User)
【问题讨论】: