如何为注册表中的UninstallString值添加首尾引号
问题分析与解决方法
原命令的核心错误
- 注册表路径冗余:
HKLM:已经是HKEY_LOCAL_MACHINE的PowerShell驱动器映射,路径里重复写HKEY_LOCAL_MACHINE会导致路径无效。 - 字符串引号转义错误:双引号字符串中嵌套双引号时未正确转义,PowerShell无法识别完整的Value内容。
- 命令选型错误:如果
UninstallString属性已存在,New-ItemProperty会触发冲突报错,应使用Set-ItemProperty修改现有属性。
正确命令写法
写法一:用反引号转义双引号
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Microsoft Visual Studio 2010 Tools for Office Runtime (x64)" -Name "UninstallString" -Value `"C:\Program Files\Common Files\Microsoft Shared\VSTO\10.0\Microsoft Visual Studio 2010 Tools for Office Runtime (x64)\install.exe`" -PropertyType String
写法二:用单引号包裹值内容(更简洁)
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Microsoft Visual Studio 2010 Tools for Office Runtime (x64)" -Name "UninstallString" -Value '"C:\Program Files\Common Files\Microsoft Shared\VSTO\10.0\Microsoft Visual Studio 2010 Tools for Office Runtime (x64)\install.exe"' -PropertyType String
补充:创建新属性(确认属性不存在时使用)
如果确定UninstallString不存在,可使用New-ItemProperty并添加-Force参数避免冲突:
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Microsoft Visual Studio 2010 Tools for Office Runtime (x64)" -Name "UninstallString" -Value '"C:\Program Files\Common Files\Microsoft Shared\VSTO\10.0\Microsoft Visual Studio 2010 Tools for Office Runtime (x64)\install.exe"' -PropertyType String -Force
验证修改结果
执行命令后,可通过以下命令查看修改后的注册表值:
Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Microsoft Visual Studio 2010 Tools for Office Runtime (x64)" -Name "UninstallString"
内容的提问来源于stack exchange,提问作者KyleXY
相关产品推荐
相关产品推荐

