need some powershell help

Discussion in 'Scripting' started by RanCorX2, Dec 20, 2017.

  1. RanCorX2

    RanCorX2 MDL Addicted

    Jul 19, 2009
    999
    554
    30
    does anyone know if this is correct?

    Get-ChildItem -path 'hkcu:\' -Recurse | where { $_.Name -match 'NAMEOFREGKEY'} | Remove-Item -Force

    it's to scan the registry and remove all keys matching "NAMEOFREGKEY" (obviously an example name)

    it seems to work but when it's running i get lots of popups saying;

    The item at
    Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Software\Microsoft\Windows\Cur
    rentVersion\XXXXXX\XXXXX 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?

    how do i make it silent and is the recurse option in the wrong place? i google for the exact info to do this but can't seem to make it run without prompts.
     
  2. GodHand

    GodHand MDL Addicted

    Jul 15, 2016
    534
    926
    30
    You need to use -recurse with Remove-Item, too. Also make use of ErrorAction to continue silently on errors.

    Code:
    Get-ChildItem -Path HKCU: -Recurse -EA SilentlyContinue | ? { $_.Name -match "RegistryKey" } | Remove-Item -Recurse -Force -EA SilentlyContinue
    
    And like always, first use the -WhatIf switch to make sure your code is doing what you want it to do, especially when it comes to removing registry keys in a loop.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...