使用PowerShell删除ShellIconOverlayIdentifiers时路径报错如何解决
问题原因
你筛选得到的$filteredList是RegistryKey类型的对象,当你直接将该对象作为路径参数传给Get-ItemProperty/Remove-Item时,PowerShell会默认调用对象的ToString()方法或者取Name属性做隐式类型转换,而Name属性返回的是不带PowerShell注册表提供器前缀的路径(格式为HKEY_LOCAL_MACHINE\XXX),不是可被直接识别的完整PowerShell路径。
此时系统会把这个路径视为当前工作目录下的相对路径进行拼接,最终就出现了你看到的路径重复、找不到路径的报错。
解决方法
选任意一种即可:
- 方案1:显式传递注册表项的完整有效路径属性
PSPath# 读取属性 Get-ItemProperty $filteredList.PSPath # 测试删除 # Remove-Item -WhatIf -Recurse $filteredList.PSPath - 方案2:通过管道传递对象,PowerShell会自动绑定
PSPath属性,无需手动指定路径# 读取属性 $filteredList | Get-ItemProperty # 测试删除 # $filteredList | Remove-Item -WhatIf -Recurse - 方案3:手动给
Name属性加上注册表提供器前缀# 读取属性 Get-ItemProperty "Registry::$($filteredList.Name)" # 测试删除 # Remove-Item -WhatIf -Recurse "Registry::$($filteredList.Name)"
修正后的完整测试脚本
Push-Location -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlayIdentifiers $list = Get-ChildItem -Path . $filteredList = $list -match "DropboxExt10" $filteredList # 管道传递的方式最简洁 $filteredList | Get-ItemProperty # $filteredList | Remove-Item -WhatIf -Recurse Pop-Location
内容的提问来源于stack exchange,提问作者Gunter
相关产品推荐
相关产品推荐

