[英]Replace string in system path with Powershell
I need to change C:\\Program Files (x86) in C:\\Progra~2 in the system path variable. 我需要在系统路径变量的C:\\ Progra~2中更改C:\\ Program Files(x86) 。 I was thinking to do this with Powershell, so I came up with the following script. 我想用Powershell做这件事,所以我提出了以下脚本。
$NewPath = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path
$OldValue = ${Program Files (x86)}
$NewValue = "Progra~2"
$NewPath = $NewPath -replace $OldValue, $NewValue
Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH –Value $newPath
$NewPath
The output is unfortunately not what I expected. 不幸的是,输出不是我的预期。
Progra~2oProgra~2mProgra~2eProgra~21Progra~2\Progra~2oProgra~2pProgra~2mProgra~2nProgra~2\Progra~2bProgra~2iProgra~2nProgra~2;Progra~2CProgra~2:
Progra~2\Progra~2OProgra~2rProgra~2aProgra~2cProgra~2lProgra~2eProgra~2\Progra~2MProgra~2iProgra~2dProgra~2dProgra~2lProgra~2eProgra~2wProgra~2a
Can anyone help me out with this? 任何人都可以帮我解决这个问题吗?
$OldValue = ${Program Files (x86)}
should be replaced by 应该被替换
$OldValue = "Program Files \(x86\)"
Backslashes \\
escape the parens interpreted as a regex
by -replace
反斜杠\\
转义被-replace
解释为regex
的parens
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.