Delete a Registry Key containing specific words using a Login Script

MattNicholas
MattNicholas used Ask the Experts™
on
Hi

We would like to use a login script to delete any registry keys called "BACKUP OF Default Outlook Profile".

The keys are located here

HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\

Would anyone here have the code to perform this?

Bare in mind the "Default Outlook Profile" is also in here, so we only want to delete any keys with the word "BACKUP" in it.

I have attached a screenshot. The highlighted key is the one we would like to delete. There can be hundreds in here with the same name, but it will have numbers at the end. I.e BACKUP OF Default Outlook Profile 001.

Many thanks
regkey.bmp
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I'm going to restrict my answer to suggesting a couple of possible methods of doing what you need of a script, but I will leave the suggestions about how to implement it (ie. where to put the file, etc) to other experts more experienced with network administration.

You can export the good registry key:
[HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\Default Outlook Profile]
to a *.reg or *.txt file in advance and that is the backup to be reinstated each time that user logs on.
Assuming that the only sub-keys that will exist under the "\Profiles" key are the "Default Outlook Profile" and all the "BACKUP OF Default Outlook Profile) sub-keys, you just have the script delete the entire "Profiles" key, and then immediately import the backed up *.reg file to recreate it without all the unwanted sub-keys:

@echo off
reg delete HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles
regedit /s backup.reg

Alternatively, have your *.reg key do the deleting and then recreating by adding an additional line above the existing good lines from the registry export like this:

[-HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles]

That minus sign just inside the opening square bracket will delete the "Profiles" key and ALL sub-keys and data within them.  Follow that with:

[HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles]
plus all your Data Values here
and it will recreate that key and values under it.

The REG DELETE command can be used to delete only a named registry key, and therefore doesn't serve your needs where you have many sub-keys all the same "Backup of..." name, but suffixed by incrementing numbers.  If that command accepted * wildcards, then it would be as simple as:

@echo off
set TargetKey=HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\BACKUP OF Default Outlook Profile
reg delete /f %TargetKey%*

It doesn't, so there is another more long-winded method of obtaining a LIST of ONLY those sub-keys named "BACKUP OF Default Outlook Profile xxx" and building a *.reg file on the fly that could be imported to delete only the named keys.  This would involve exporting the "Profiles" key to a *.txt file, running a FIND command to redirect ONLY the lines from the file containing the string you searched on to another file, and at the same time adding the required elements to make it into a working *.reg file.

I have tested the batch file in the CODE SNIPPET in Windows XP on a single user PC on which I created 9 new sub-keys suffixed by 001 to 009, and it runs silently.  Of course, the PATHS to the files would need to be modified to suit where they are going to be created in relation to where the batch file is located.

I've remarked the batch file to explain, but to let you see what the temporary text file looks like as it is made into a *.reg file, here are the file contents at the two crucial stages:

RegTemp02.txt after creation:

[HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\BACKUP OF Default Outlook Profile 001]
[HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\BACKUP OF Default Outlook Profile 002]
[HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\BACKUP OF Default Outlook Profile 003]
[HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\BACKUP OF Default Outlook Profile 004]

DelRegKeys.reg after creation:

Windows Registry Editor Version 5.00
 
[-HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\BACKUP OF Default Outlook Profile 001]    
[-HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\BACKUP OF Default Outlook Profile 002]    
[-HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\BACKUP OF Default Outlook Profile 003]    
[-HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\BACKUP OF Default Outlook Profile 004]    

One other issue is that whoever is logging in would have to have permission to have a batch file running under their profile call REGEIT.EXE.  I'm not experienced enough to know how to resolve this aspect other than to try and use the RUNAS command and specify administrator.

Hopefully this sketchy idea may help you create what you need.

@echo off
::
:: export the "profiles" registry key to temp text file
::
start /b /wait regedit /e RegTemp01.txt "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles"
::
:: Search file for ONLY lines that are the unwanted keys
:: and redirect to another temp text file
::
type RegTemp01.txt | find /i "\BACKUP OF Default Outlook Profile" > RegTemp02.txt
::
:: Start compiling a *.reg file on the fly
::
echo Windows Registry Editor Version 5.00 > DelRegKeys.reg
echo. >> DelRegKeys.reg
for /f "tokens=1,2,3* delims=[" %%i in (RegTemp02.txt) do @echo [-%%i %%j %%k %%l >> DelRegKeys.reg
echo. >> DelRegKeys.reg
::
::
:: Silently import *.reg file to registry
regedit /s DelRegKeys.reg
::
:: Cleanup temp text files and reg file
::
if exist RegTemp*.txt del RegTemp*.txt > nul
if exist DelRegKeys.reg del DelRegKeys.reg > nul
::
exit

Open in new window

You can change the following line:

for /f "tokens=1,2,3* delims=[" %%i in (RegTemp02.txt) do @echo [-%%i %%j %%k %%l >> DelRegKeys.reg

to:

for /f "delims=[" %%i in (RegTemp02.txt) do @echo [-%%i>> DelRegKeys.reg

It works better by eliminating blank space at the end of each line.
Thank you Matt

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