powershellregedit

Deleting all subkeys in regedit with powershell


I want to delete all the subkeys of this path

HKEY_CURRENT_USER\Software\Microsoft\Workspaces\Feeds

Here I have {E3CA8511-C9A2-4B55-A008-E2823464A6C0} which needs to be deleted.

Regedit

But I could have something else as a name

When I type:

Get-Item -Path "HKCU:\Software\Microsoft\Workspaces\Feeds\*"

or the following

Remove-Item -Path "HKCU:\Software\Microsoft\Workspaces\Feeds\*" -Recurse

I don't get anything in return. It seems like it's not existent from powershell's point of view.

And when I type:

Remove-Item -Path "HKCU:\Software\Microsoft\Workspaces\Feeds\{E3CA8511-C9A2-4B55-A008-E2823464A6C0}"

It says that the path doesn't exist, although it actually exists. Remove-item

I've searched this article: and also this article about recreating the key, and it actually recreates the key in powershell but still doesn't delete it in the registery:

Here the example:

New-Item -Path "HKCU:\Software\Microsoft\Workspaces\Feeds\{E3CA8511-C9A2-4B55-A008-E2823464A6C0}" -Force
Remove-Item -Path "HKEY_CURRENT_USER\Software\Microsoft\Workspaces\Feeds\{E3CA8511-C9A2-4B55-A008-E2823464A6C0}" -Recurse

Any ideas why this is happening and how to solve it?


Solution

  • The path passed to Remove-Item in your last snippet, as also shown in the screenshot, is incorrect:

    Remove-Item -Path "HKEY_CURRENT_USER\..." cannot work, because you can not use a native registry path that starts with a hive name such as HKEY_CURRENT_USER directly, as PowerShell will interpret it as a relative file-system path.[1]

    Instead, you must:

    Thus, to delete all subkeys of HKEY_CURRENT_USER\Software\Microsoft\Workspaces\Feeds, use one of the following:

    # Use PowerShell's HKCU: drive to target the HKEY_CURRENT_USER registry hive.
    Remove-Item -Recurse -Force 'HKCU:\Software\Microsoft\Workspaces\Feeds\*'
    
    # Alternatively, prepend the provider prefix to the native registry path:
    Remove-Item -Recurse -Force 'registry::HKEY_CURRENT_USER\Software\Microsoft\Workspaces\Feeds\*'
    

    [1] The only exception is if PowerShell's current location happens to be the root of that very hive (e.g., only after executing Set-Location HKCU:\, does something like Get-ChildItem HKEY_CURRENT_USER\ happen to work

    [2] Note that the Core part of Microsoft.PowerShell.Core is unrelated to the to the PowerShell (Core) 7 edition of PowerShell. Microsoft.PowerShell.Core is simply the name of a built-in module that is present in both PowerShell editions (i.e. also in Windows PowerShell).