Solved

Delete registry keys

Posted on 2008-06-12
13
706 Views
Last Modified: 2008-06-26
I need a little script to delete some registry keys on a terminal server that I'm administering.  I basically need the same key deleting for every user, the key is in the SOFTWARE section, and I want all subkeys of the named key deleting too.  I've tried to do this in .NET and I can work it on my PC but it won't work on the server, I get one of those helpful non-specific error messages - I've a separate question open for that but I'm getting tumbleweeds at the moment.

So in essence I need to iterate through the keys in HKEY_USERS and check for existence of a key under SOFTWARE, deleting it and all its subkeys if it exists.

Can anyone assist please?
0
Comment
Question by:Belazir
  • 7
  • 5
13 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 21769891

use Win32::TieRegistry (Delimiter => '/');
 

delete $Registry->{"HKEY_USERS/$_/path/to/the/key/you/want/to/delete/"} foreach (keys %{ $Registry->{HKEY_USERS} });

Open in new window

0
 

Author Comment

by:Belazir
ID: 21770111
Looks close, but the key won't be in every reg entry under Users.  How would I add to this to (a) test whether the key name ends with _Classes (in which case it won't be there) and (b) whether the key is actually there?  Or will this not error and just ignore it if the key isn't there?
0
 
LVL 9

Expert Comment

by:Suhas .
ID: 21770114
Please go through the module Win32::Registry

http://search.cpan.org/~jdb/Win32-Registry-0.08/Registry.pm

to delete a key:
 $reg_obj->DeleteKey($sub_key_name);

to delete a value:
 $reg_obj->DeleteValue($value_name);
0
 

Author Comment

by:Belazir
ID: 21770167
Su - with all due respect, if I had time to do that I probably wouldn't have posted a question here.  I'm looking for a quick win.  Thx B
0
 
LVL 39

Expert Comment

by:Adam314
ID: 21771610
The Win32::Registry module is obsolete, and it's documentation even states this on the FIRST line, stateing you should use Win32::TieRegistry!!!

The code I gave will simply skip that key if the registry key does not exist.  It will not cause an error.

For checking if the key name ends in _Classes, and if it exists (which is probably not necessary from your description, but anyways)

foreach (keys %{ $Registry->{HKEY_USERS} }) {

    next if /_Classes$/;    #skip keys ending in _Classes

    next unless exists($Registry->{'HKEY_USERS/$_/path/to/key/to/delete'}) #skip if key doesn't exist

    delete $Registry->{"HKEY_USERS/$_/path/to/the/key/to/delete/"}; #delete

}

Open in new window

0
 

Author Comment

by:Belazir
ID: 21820063
So now I have the attached (modified slightly).

However the keys aren't deleting.  I know they're now passing both the conditions because my print line is running, so it knows the key exists - but it's not deleting.

Any idea why, or how I find out what the error is?
use Win32::TieRegistry (Delimiter => '/');

 

foreach (keys %{ $Registry->{HKEY_USERS} }) {

    next if /_Classes/;    #skip keys ending in _Classes

    next unless exists($Registry->{'HKEY_USERS/'.$_.'SOFTWARE/MyKey/'}); #skip if key doesn't exist

    print 'HKEY_USERS/'.$_.'SOFTWARE/MyKey/'."\n";

    delete $Registry->{'HKEY_USERS/'.$_.'SOFTWARE/MyKey/'}; #delete

} 

Open in new window

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 39

Expert Comment

by:Adam314
ID: 21823954
Does the user running the script have permission to delete keys for other users?  This probably means the user running the script has to be Administrator.  

Also, if the key you are trying to delete has subkeys, they will have to be deleted first.  Attached is a script that will delete recursively all the subkeys, then delete the requested key.
use Win32::TieRegistry (Delimiter => '/');

 

foreach (keys %{ $Registry->{HKEY_USERS} }) {

    next if /_Classes/;    #skip keys ending in _Classes

    next unless exists($Registry->{'HKEY_USERS/'.$_.'SOFTWARE/MyKey/'}); #skip if key doesn't exist

    print 'HKEY_USERS/'.$_.'SOFTWARE/MyKey/'."\n";

    DeleteRecursive("HKEY_USERS/${_}SOFTWARE/MyKey/");

}
 

sub DeleteRecursive {

	my $key=shift;

	$key .= "/" unless $key =~ m|/$|;  #make sure key ends in slash

	DeleteRecursive("$key$_") foreach (keys %{$Registry->{$key}});

	delete $Registry->{$key};

}

Open in new window

0
 

Author Comment

by:Belazir
ID: 21856768
Yes, it's an admin running it, but yes, the key has subkeys.  I'll try the recursion then.
0
 

Author Comment

by:Belazir
ID: 21874789
I get an error:

  value data cannot be a HASH reference at clearReg.pl line 13
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
ID: 21875908
I didn't test with the sub-folders having values - only more sub-folders.  This version takes care of that.
sub DeleteRecursive {

	my $key=shift;

	return unless $key =~ m|/$|;

	foreach (keys %{$Registry->{$key}}) {

		next if m|^/|;               #skip non-folders

		DeleteRecursive("$key$_");   #delete sub-folders

	}

	delete $Registry->{$key};

}

Open in new window

0
 

Author Comment

by:Belazir
ID: 21876123
That seems to be doing the trick.  Wish I could have got this working in .NET as this is very slow, the keys I'm deleting are big...
0
 
LVL 39

Expert Comment

by:Adam314
ID: 21876291
I doubt it would have been faster in .NET.  I'm guessing either way, the program is making calls to the same windows API.  
0
 

Author Comment

by:Belazir
ID: 21876326
There's an API though in .NET that deletes all rather than using recursion so it doesn't need to keep returning control to the application.

Anyway it's done the trick, thanks.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now