我需要获取"UninstallString"在
电脑 HKEY_LOCAL_MACHINE 微软软件 WOW6432Node Windows CurrentVersion Uninstall{1535 caa3 - 9 - f33 - 414 - e - 8987 - 0365169 - be741}
调用:
Get-Item -path HKLM:SOFTWAREWOW6432NodeMicrosoftWindowsCurrentVersionUninstall{1535CAA3-9F33-414E-8987-0365169BE741}
结果Get-Item:无法找到接受参数'1535CAA3-9F33-414E-8987-0365169BE741'的位置参数。
补充js2010的有用答案,其中指出缺少引号是您的主要问题({
和}
是PowerShell元字符):
在PowerShell v5及以上版本中,您可以使用Get-ItemPropertyValue
cmdlet直接返回与注册表项值相关的数据:
# Note the use of '...' around the registry path
# and the value name UninstallString at the end.
Get-ItemPropertyValue 'HKLM:SOFTWAREWOW6432NodeMicrosoftWindowsCurrentVersionUninstall{1535CAA3-9F33-414E-8987-0365169BE741}' UninstallString
至于你试过的:
未加引号的参数,如HKLM:...{1535CAA3-9F33-414E-8987-0365169BE741}
,处理如下:
因为
{...}
创建了一个脚本块,它被认为是一个单独的参数的开始,并且字符串'HKLM:...'
和脚本块{1535CAA3-9F33-414E-8987-0365169BE741}
作为两个参数传递。因为
Get-Item
没有预料到一个额外的(position)参数,它相应地发出了抱怨,并在错误消息中使用了脚本块的字符串表示。- 脚本块的字符串表示是它的逐字内容,不包括
{
和}
,您可以使用{1535CAA3-9F33-414E-8987-0365169BE741}.ToString()
进行验证。
- 脚本块的字符串表示是它的逐字内容,不包括
如前所述,引号是解决方案,并且由于路径既不包含PowerShell变量引用也不包含子表达式,因此使用单引号,即原形字符串字量('...'
)是最好的。
(通常不太理想的)替代方法是坚持使用不加引号的参数,并使用单独转义PowerShell元字符的`
(所谓的反引号);例:Write-Output HKLM:...`{1535CAA3-9F33-414E-8987-0365169BE741`}
像这样加上引号,因为花括号看起来像powershell的scriptblock:
get-itemproperty 'HKLM:SOFTWAREMicrosoftWindowsCurrentVersionUninstall{013DB423-A8DE-4423-9E50-D45ED1041789}' uninstallstring |
% uninstallstring
MsiExec.exe /I{013DB423-A8DE-4423-9E50-D45ED1041789}
尽管对于msi,您可以在powershell 5.1中使用此卸载:
get-package *chrome* | uninstall-package
对于其他非msi安装,uninstallstring在这里。通常你需要添加一个额外的选项,比如"/S"用于静默卸载。
get-package *firefox* | % { $_.metadata['uninstallstring'] }
"C:Program FilesMozilla Firefoxuninstallhelper.exe"
正如我所评论的那样,您需要在""
中包装路径,但这仍然不起作用。要在powershell中获取注册表值,请使用Registry::HKEY_LOCAL_MACHINE...
作为路径。一旦获得RegistryKey
对象,就可以使用GetValue(string name)
来获得UninstallString
的值。所以你的命令看起来像这样:
(Get-Item -path "Registry::HKEY_LOCAL_MACHINESOFTWAREWOW6432NodeMicrosoftWindowsCurrentVersionUninstall{1535CAA3-9F33-414E-8987-0365169BE741}").GetValue("UninstallString")