我在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提示符设置它时,这种方法总是有效的。为什么在从PowerShell设置变量时,这种行为是不同的?这个特性在PowerShell中被删除了吗?
我不希望执行以下操作,因为每次运行脚本时,它都会将变量添加到path中。
[environment]::SetEnvironmentVariable("PATH",$mypath+";"+$var,[system.environmentvariabletarget]::User) 发布于 2012-09-26 05:24:53
试着改变这一行:
[environment]::SetEnvironmentVariable("PATH",$mypath+";%TEST%",[system.environmentvariabletarget]::User) 通过以下方式:
$test =[environment]::GetEnvironmentVariable("test","user") # this retrieve the rigth variable value
[environment]::SetEnvironmentVariable("PATH", $mypath +";$test",[system.environmentvariabletarget]::User) %test%在powershell中没有任何意义,不能像在CMD中那样扩展。
$env:test只从系统环境变量检索,而不是从用户检索。
发布于 2012-09-26 13:39:49
您希望有效地设置使用REG_EXPAND_SZ的注册表值(对应于env )。看这个post for details on how to do that。
https://stackoverflow.com/questions/12594896
复制相似问题