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

**URL:** <https://forums.powershell.org/t/powershell-can-not-delete-registry-key-shows-cannot-delete-a-subkey-tree-because-the-subkey-does-not-exist/20537>\
**Category:** PowerShell Help\
**Created:** [September 27, 2022, 9:38am UTC](https://forums.powershell.org/t/powershell-can-not-delete-registry-key-shows-cannot-delete-a-subkey-tree-because-the-subkey-does-not-exist/20537 "2022-09-27T09:38:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chen.Chen](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/chen.chen/32/1689_2.png) [@Chen.Chen](https://forums.powershell.org/u/Chen.Chen)\
**Post date:** [September 27, 2022, 9:38am UTC](https://forums.powershell.org/t/powershell-can-not-delete-registry-key-shows-cannot-delete-a-subkey-tree-because-the-subkey-does-not-exist/20537/1 "2022-09-27T09:38:53Z")

</div>

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 …
- 

```auto
  + 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.

```auto
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()) 
                
                hdr.DeleteSubKey(key);
            hdr.Close();
            
        }
    }

```

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?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 27, 2022, 9:50am UTC](https://forums.powershell.org/t/powershell-can-not-delete-registry-key-shows-cannot-delete-a-subkey-tree-because-the-subkey-does-not-exist/20537/2 "2022-09-27T09:50:41Z")

</div>

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

> **[Remove-Item (Microsoft.PowerShell.Management) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/remove-item?view=powershell-7.2#example-5-delete-subkeys-recursively)**
>
> The Remove-Item cmdlet deletes one or more items. Because it is supported by many providers, it can delete many different types of items, including files, folders, registry keys, variables, aliases, and functions.

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

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](https://aws1.discourse-cdn.com/business5/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) \<---- Click 👆🏽 😉

---

<div class="post-metadata">

**Author:** ![Chen.Chen](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/chen.chen/32/1689_2.png) [@Chen.Chen](https://forums.powershell.org/u/Chen.Chen)\
**Post date:** [September 28, 2022, 1:21am UTC](https://forums.powershell.org/t/powershell-can-not-delete-registry-key-shows-cannot-delete-a-subkey-tree-because-the-subkey-does-not-exist/20537/3 "2022-09-28T01:21:18Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/c/cbfa8a22945a5fb4850ff56f5debc7f0c14cae24.png)

and the registry is exist…

 ![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/5/5899af534a6c043d3f70f8e16dbbdeeffd77b385.png)

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 28, 2022, 8:14am UTC](https://forums.powershell.org/t/powershell-can-not-delete-registry-key-shows-cannot-delete-a-subkey-tree-because-the-subkey-does-not-exist/20537/4 "2022-09-28T08:14:29Z")

</div>

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

```auto
Windows Registry Editor Version 5.00

[-HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ics\UserChoice]

```

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

And run the following command in an administrative PowerShell or CMD

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

```

it should work then …

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/powershell-can-not-delete-registry-key-shows-cannot-delete-a-subkey-tree-because-the-subkey-does-not-exist/20537/5 "2024-05-16T20:16:35Z")

</div>


