Link to home
Start Free TrialLog in
Avatar of brittonv
brittonvFlag for United States of America

asked on

Search for Values in a registry key and delete them

I am trying to create a powershell script that will delete all entries where the word apple is.  

I can get to the HK users using example:
$null = New-PSDrive -Name HKU -PSProvider Registry -Root Registry::HKEY_USERS 
Get-ChildItem HKU:'\S-1-5-20\Software\Microsoft\Windows NT\CurrentVersion\Devices'

Open in new window

However how do I loop throgh those values and delete the ones that say "apple"???

I am starting to think that it isn't possible.

if it can't be done 'cleanly', can a script output to a text file then another script use taht file to delete the entries?  I would prefer if it could be done with a single script but I am starting to give up....
Avatar of chrismerritt
chrismerritt

Try this one, will delete all sub key properties that contain the word "Apple".

$RemovalArray = @()

New-PSDrive -Name HKU -PSProvider Registry -Root Registry::HKEY_USERS
Set-Location HKU:
$RegPathContent = Get-Item "HKU:\S-1-5-20\Software\Microsoft\Windows NT\CurrentVersion\Devices"

for ($i = 0 ; $i -lt $RegPathContent.ValueCount ; $i++)
{
	$CurrentProperty = $RegPathContent.Property[$i]
	if ($CurrentProperty -match "Apple")
	{
		Write-Host "found match: $CurrentProperty"
		
		$TempArray = @()
		$TempArray = "" | Select KeyPath, PropertyName
		
		[string]$TempArray.KeyPath = "HKU:\S-1-5-20\Software\Microsoft\Windows NT\CurrentVersion\Devices"
		[string]$TempArray.PropertyName = $CurrentProperty
		
		$RemovalArray += $TempArray
	}
}

if ($RemovalArray.Count -gt 0)
{
	foreach ($Item in $RemovalArray)
	{
		Remove-ItemProperty $Item.KeyPath $Item.PropertyName
	}
}

Open in new window

Avatar of brittonv

ASKER

This is beyond awesome, thanks!

One more question, is it possible to convert something like this in to a function such that I could run:
delkey -key "HKU:\S-1-5-20\Software\Microsoft\Windows NT\CurrentVersion\Devices" -value "apples"

I understand that I would have to mount HKU first.
ASKER CERTIFIED SOLUTION
Avatar of chrismerritt
chrismerritt

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Awesome, thanks!!!