none
Recurse does not work in Remove-Item command RRS feed

  • Question

  • Hi,

    I try to delete Windows Update registry key and alls subkeys using powershell.

    Unfortunaley the following command does not work:

    Remove-Item -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\" -recurse -force -confirm:$false
    Remove-Item : Cannot delete a subkey tree because the subkey does not exist.
    At line:1 char:1
    + Remove-Item -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Wi ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : WriteError: (HKEY_LOCAL_MACH...\WindowsUpdate\:String) [Remove-Item], ArgumentException
        + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.RemoveItemCommand

    If I use the same command without -recurse parameter, then the output wants that I should confirm it. But the command should not ask to confirm it:

    Remove-Item -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\" -force -confirm:$false
    Confirm
    The item at HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\ has children and the Recurse parameter was
    not specified. If you continue, all children will be removed with the item. Are you sure you want to continue?
    [Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):

    How can I solve -recurse issue? 

    Best regards

    Birdal

    Wednesday, October 9, 2019 12:52 PM

All replies

  • Do not use "Recurse".

    You should not delete this key or any keys in this section.  They are owned by the OS and deleting them can create issues that you will not be able to resolve.

    When deleting a key do not add a trailing "\".


    \_(ツ)_/

    Wednesday, October 9, 2019 2:01 PM
  • Hi,

    >>> Do not use "Recurse"

    OK, what should I use?

    >>> You should not delete this key or any keys in this section. 

    I know what I do. Do you know the answer, how I can solve the issue?Best regards

    Birdal

    Wednesday, October 9, 2019 2:11 PM
  • I posted:

    When deleting a key do not add a trailing "\".


    \_(ツ)_/

    Wednesday, October 9, 2019 2:14 PM
  • Hi,

    I tried before all options, also without "\".

    The same issue.

    Best regards

    Birdal

    Wednesday, October 9, 2019 2:38 PM
  • Documentation:

    https://docs.microsoft.com/en-us/powershell/scripting/samples/working-with-registry-keys

    At the bottom:

    If you wanted to remove all items within HKCU:\CurrentVersion but not HKCU:\CurrentVersion itself, you could instead use:

    Remove-Item -Path HKCU:\CurrentVersion\* -Recurse
    (Hopefully this caveat is unnecessary: The above Remove-Item command from the documentation is only an example. Do not run this command.)

    -- Bill Stewart [Bill_Stewart]

    Wednesday, October 9, 2019 2:59 PM
    Moderator
  • Yes - that removes the "contents" of the key and leaves the key. In this case "Recurse" is necessary. Removing the key itself directly should not need the "Recurse" but requires the :Force". When using a trailing backslash the wildcard is required when removing subkeys/folders.

    Removing this key and its contents can have a devastating effect on the whole WU subsystem.


    \_(ツ)_/

    Wednesday, October 9, 2019 4:03 PM
  • Agreed; One should read documentation before arbitrarily deleting registry subkeys that are used by the system.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, October 9, 2019 4:21 PM
    Moderator
  • Hi Bill,

    the same erroe also. I had had test also it before.

    Best regards

    Birdal

    Thursday, October 10, 2019 11:14 AM
  • Hi,

    The issue was related to permissions of this registry key.

    I tested keys / subkeys (created own), and then aplied the command

    Remove-Item -Path "HKLM:\SOFTWARE\myKey\" -recurse -force -confirm:$false

    It dleted as I expected "myKey" and all subkeys.

    It seems so that that "WindowsUpdate" key cannot be deleted using this command. But it is very interesting that the same admin user can delete "WindowsUpdate" and subkeys on registry console directly.

    Very strange.

    Best regards

    Birdal

    Thursday, October 10, 2019 11:20 AM