[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Search for Values in a registry key and delete them

Posted on 2011-10-25
4
Medium Priority
?
1,787 Views
Last Modified: 2012-06-27
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....
0
Comment
Question by:brittonv
  • 2
  • 2
4 Comments
 
LVL 9

Expert Comment

by:chrismerritt
ID: 37028387
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

0
 
LVL 8

Author Comment

by:brittonv
ID: 37031599
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.
0
 
LVL 9

Accepted Solution

by:
chrismerritt earned 2000 total points
ID: 37031842
This should work as a function:

Function DeleteRegProperty
{
	param
	(
		$RegPath,
		$ValueName,
		[switch]$WhatIf
	)
	
	$RemovalArray = @()

	New-PSDrive -Name HKU -PSProvider Registry -Root Registry::HKEY_USERS -ErrorAction "SilentlyContinue" | Out-Null
	Set-Location HKU:
	$RegPathContent = Get-Item $RegPath

	for ($i = 0 ; $i -lt $RegPathContent.ValueCount ; $i++)
	{
		$CurrentProperty = $RegPathContent.Property[$i]
		if ($CurrentProperty -match $ValueName)
		{
			Write-Host "found match: $CurrentProperty"
			
			$TempArray = @()
			$TempArray = "" | Select KeyPath, PropertyName
			
			[string]$TempArray.KeyPath = $RegPath
			[string]$TempArray.PropertyName = $CurrentProperty
			
			$RemovalArray += $TempArray
		}
	}
	
	if ($RemovalArray.Count -gt 0)
	{
		foreach ($Item in $RemovalArray)
		{
			if ($WhatIf.IsPresent -eq $False)
			{
				Remove-ItemProperty $Item.KeyPath $Item.PropertyName
			}
			else
			{
				Write-Host "Would Remove Here: $($Item.KeyPath) $($Item.PropertyName)"
			}
		}
	}
	
}

Open in new window


You can then call the function like this:

$RegPath = "HKU:\S-1-5-20\Software\Microsoft\Windows NT\CurrentVersion\Devices"
$ValueName = "Apple"

DeleteRegProperty $RegPath $ValueName -WhatIf

Open in new window


Leave the -WhatIf switch off to delete the keys, if you have -WhatIf on it will just echo back to you the keys it would delete.
0
 
LVL 8

Author Closing Comment

by:brittonv
ID: 37032419
Awesome, thanks!!!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question