PowerShell Delete Value From Registry With String In File-Name

Member_2_8171182
Member_2_8171182 used Ask the Experts™
on
I have A PowerShell Script that will delete Key with string in name.
I need help with A script that will delete Value with string in name

This will delete by string in folder name from registry.
# Folders (Key)
Get-ChildItem -Path HKLM:\ -Recurse -Include *String* -ErrorAction SilentlyContinue | Remove-Item -Force -Recurse -confirm:$false -Whatif

Open in new window


I have use this to delete string in file-name.
cd $env:SystemRoot
$string = "String"
get-childitem -recurse| where-object {$_.Name -like "*$string*"} | foreach ($_) {remove-item $_.fullname -recurse -confirm:$false}

Open in new window


Thanks in advanced.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2014

Commented:
Let's throw out the terms folder and file when referring to the registry (unless we were talking about the actual files that the registry is based on).
We've got registry keys, which are shown in the left pane in regedit; and we've got registry values, which are shown in the right pane in regedit.  Registry values have a name, type, and data.

Are you asking to delete any registry value where its name matches (even partially) a particular string?

*Item cmdlets are used to work with with keys (though output formatting from the Get-Item and Get-ChildItem cmdlets shows some info about values as well, since PS 3.0+), while *-ItemProperty cmdlets are used with values.

Author

Commented:
Are you asking to delete any registry value where its name matches (even partially) a particular string?

Yes Actually, Values or Data. The first script will delete keys containing string from the registry. (Apologies for my ignorance.)

The second script is for file and directories containing string in file name.
Top Expert 2014
Commented:
You could try something like the following:
$search = "sometext"
$regPath = "HKCU:\"
Get-ChildItem $regPath -Recurse | ForEach `
{
    $valueNames = $_.GetValueNames()
    foreach ($valueName in $valueNames)
    {
        If ( $valueName -match $search )
        { Remove-ItemProperty $_.PsPath -Name $valueName -WhatIf }
        ElseIf ( $_.GetValue($valueName) -match $search )
        { Remove-ItemProperty $_.PsPath -Name $valueName -WhatIf }
    }
}

Open in new window

You would need to remove the -WhatIf parameters to actually delete anything.  This should go without saying, but make sure you test before using, as there's potential to really screw things up if you put in a wrong search string.  You can change the $regPath variable to limit where you're searching.

I would recommend looking at tools like Nirsoft's RegScanner or Registry-Finder (don't know the maker) for searches.  They may help to validate the above, and just using them (maybe in conjunction with regedit) might be enough for you, especially if you don't have a whole lot of values to delete.
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Author

Commented:
Many Thanks! A quick follow up question,
Can the
-ErrorAction SilentlyContinue
parameter be used?
Top Expert 2014

Commented:
Simple answer is, "yes", but it's a pretty vague question.
Where would you put it and what are you hoping to achieve?

Author

Commented:
Similar to the first script, to ignore all errors.
Top Expert 2014

Commented:
Sure, it won't pose any problem.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial