1

I am currently very annoyed by Dropbox and Nextcloud, which both battle the ShellIconOverlayIdentifier list. A problem which many people seem to have, when you search the internet.

Now I want to combine my annoyance with my intent to learn powershell (7.2.0).

I started with the following script, which shall retrieve all keys. And later I want to use regex via -match to find the entries I want to delete. For now I work with both Remove-Item -WhatIf and Get-ItemProperty to test it.

Currently my problem is that I can create my list as intended. But when I feed the list into the remove command I get that the path cannot be found. What am I doing wrong?

Push-Location -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlayIdentifiers

$list = Get-ChildItem -Path .

$filteredList = $list -match "DropboxExt10"

$filteredList

# Remove-Item -WhatIf -Recurse $filteredList
Get-ItemProperty $filteredList

Pop-Location

The error is Cannot find path 'Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlayIdentifiers\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlayIdentifiers\ DropboxExt10' because it does not exist. Apparantly it adds the path as relative path to the current location. Why doesn't it interpret as an absolute path? When I ommit the push-location part it trys to add the registry path to my current working directory in which the script lives. But this is wrong as well.

Thanks for your help in advance.

Gunter
  • 321
  • 4
  • 13

1 Answers1

1

Use one of the following:

$filteredList | Remove-Item -WhatIf -Recurse

# Alternatively:
Remove-Item -LiteralPath $filteredList.PSPath -WhatIf -Recurse

The above commands ensure that the registry items (keys) are bound by their full, provider-qualified paths to Remove-Item's -LiteralPath parameter, via their .PSPath property; in the case of registry paths, the path's provider prefix is Microsoft.PowerShell.Core\Registry:: (or, if identifying the originating module isn't also required, Registry::, as used in your Push-Location call)

What appears to be happening is that when the items are stringified - which is what happens when you pass them as a whole, as an argument (e.g. Remove-Item $filteredlist, which is the same as Remove-Item -Path $filteredlist), they lack the provider prefix and are represented as registry-native paths.

And given that a full registry-native path such as HKEY_LOCAL_MACHINE\... neither starts with a drive specification nor with \ or /, it is interpreted as a relative path, resulting in the symptom you saw.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Thanks for the elaborate explanation. Both variants work. Actually I came up with a third solution. Could you add it to your answer, so we have all options here? `foreach ($item in $filteredList) {Remove-Item -WhatIf -Recurse Registry::$item}`. Actually I was starting to like Powershell. But given that my version did not work, but your first proposed solution does, makes me wonder. – Gunter Nov 17 '21 at 20:36
  • Thanks for accepting, Gunter; I'm glad the explanation was helpful. Honestly, given that the solutions in the answer are more concise (no loop) and faster than a `foreach`-loop solution, I don't think the latter is worth adding. Yes, it's not obvious why `$filteredList | Remove-Item -WhatIf -Recurse` works (see [this answer](https://stackoverflow.com/a/69922416/45375)), and the quirks around passing paths can be frustrating. But, despite all its warts, PowerShell has a lot to offer. – mklement0 Nov 17 '21 at 22:45
  • 1
    Thanks for the additional link. I see I have to learn quite a bit to understand how Powershell cmdlets are actually processing information. And also thanks for pointing out that the loop solution would actually be slower. Indeed, Powershell has a lot to offer. I currently really like it for automating stuff, which took quite a few clicks otherwise (restarting services, deleting registry entries, restarting explorer.exe). So I expect a love-hate-relationship with powershell for now :-) – Gunter Nov 18 '21 at 07:19