Powershell can not delete registry key, shows Cannot delete a subkey tree because the subkey does not exist

Hi Gays,
I want to use powershell to delete registry key, but failed.
this is my code
Remove-Item -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ics\UserChoice" -Force

and it shows `Remove-Item : Cannot delete a subkey tree because the subkey does not exist.
At line:1 char:1

  • Remove-Item -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Ex …
  •   + CategoryInfo          : WriteError: (HKEY_CURRENT_US....ics\UserChoice:String) [Remove-Item], ArgumentException
      + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.RemoveItemCommand`

and I find C# .net can do it.

internal class Program
        static void Main(string[] args)
            RegistryKey FileExts = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts");
            RegistryKey hdr = FileExts.OpenSubKey(".ics", true);

            foreach (String key in hdr.GetSubKeyNames()) 

and I want to use it in powershell
then I use this $file=[Microsoft.Win32.Registry]::CurrentUser.CreateSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\") $hdr=$file.OpenSubKey(".ics",$true) $hdr

but nothing find in powershell.

does any body know what shoud I do?

You should ALWAYS carefully read the help for the cmdlets you’re about to use including the examples.

There’s even an example for your particular case with subkeys. :wink:

And please BTW: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

sir, I have tried to this way, but failed…

and the registry is exist…

Hmmm … it looks like there are special rights applied to those keys and you cannot delete them easily with PowerShell. A workaround could be to create a *.reg file and use Regedit /s to import it in order to delete those keys.
If you create a file C:\sample\removeRegKey.reg with the content of

Windows Registry Editor Version 5.00


!! Notice the minus sign right before the path !!

And run the following command in an administrative PowerShell or CMD

regedit /s 'C:\sample\removeRegKey.reg'

it should work then …