Solved

Delete registry key via batch script

Posted on 2014-01-23
17
2,029 Views
Last Modified: 2014-01-24
Hey Experts!  I am trying to delete out the registry keys relating to printers that point to our old print server.  I found the printers listed in the registry (HKCU\Printers\Connections) and would like to use a batch script to wipe out the entries referencing the old server.

The registry looks like this:
=Connections
== ,,oldserver, printer1
== ,,oldserver, printer2
== ,,newserver, printer1

How can I delete the registry keys to the old server?

I tried this with no success:
reg delete "HKCU\Printers\Connections" /v ",,oldserver" /f

Open in new window


Thanks experts!
0
Comment
Question by:samiam41
  • 5
  • 4
  • 3
  • +4
17 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39804786
with vbscript this is quite easy, see this scripting guy teaching article:
http://technet.microsoft.com/en-us/magazine/2006.08.scriptingguy.aspx
0
 
LVL 9

Author Comment

by:samiam41
ID: 39804811
Thank you for the post.  Any chance you have a batch script version?  I am all thumbs with VB.
0
 
LVL 29

Expert Comment

by:becraig
ID: 39804834
Are you open to using powershell  ?
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 10

Expert Comment

by:ienaxxx
ID: 39804868
Hi! :-)

here it is in batch:

reg delete <keyname> /v <valuename> /f

Open in new window



for the keyname pay attention: HKLM is HKEY_LOCAL_MACHINE, HKCU is HKEY_CURRENT_USER and so on.

more details with reg /?

Bye! :-)
0
 
LVL 10

Expert Comment

by:ienaxxx
ID: 39804902
you can try for example adding a string value "test" under HKEY_CURRENT_USER\Console from regedit and then deleting it with:

reg delete HKCU\Console /v test /f

Open in new window


Bye! :-)
0
 
LVL 52

Expert Comment

by:Joe Winograd, EE MVE
ID: 39805576
I do most of my programming/scripting these days in AutoHotkey (AHK), an excellent (free!) programming/scripting language. There have been several forks of the original language and my preferred one now is AutoHotkey_L. It comes with a Windows installer, as well as a compiler that turns the AHK source code (plain text) into a stand-alone/no-install executable (an EXE file).

If you're willing to give AutoHotkey a try, here's a 3-line script that does what you want:

Loop,HKCU,Printers\Connections,2,0
If (InStr(A_LoopRegName,"oldserver")<>0)
  RegDelete

To make you feel comfortable with the script, here's a description of what each line does:

(1) The Loop statement retrieves the contents of the specified registry subkey, one item at a time. The first param in the Loop statement is the root key (must be HKLM, HKU, HKCU, HKCR, or HKCC) and the second is the key. The "2" in the third param means that only the subkeys are retrieved (not the values), while the "0" in the fourth param means that subkeys are not recursed into.

(2) The Loop statement automatically creates several variables. One of them is A_LoopRegName, which is the name of the currently retrieved item. The InStr statement is a "find in string" function. The first param is the Haystack (in this case, the currently retrieved registry item) and the second param is the Needle (in this case, "old server"). InStr returns the position of an occurrence of the string Needle in the string Haystack, or zero if no occurrence is found. The "<>" operator means "not equal".

(3) Inside of a registry Loop statement, the RegDelete statement deletes the current item. If the current item is a key, it is deleted along with any subkeys and values it contains.

I think that makes it clear what the three lines of code are doing, but if you have any questions, let me know. Regards, Joe
0
 
LVL 52

Expert Comment

by:Joe Winograd, EE MVE
ID: 39805675
One other thought. If you'd like to make a test run first to see what it would delete, change the RegDelete statement to a MsgBox. For example,

Loop,HKCU,Printers\Connections,2,0
If (InStr(A_LoopRegName,"oldserver")<>0)
  MsgBox,4096,Test Run,Found old print server:`n%A_LoopRegName%

The above will give you a MsgBox where you have to hit Enter or click OK every time it finds an "oldserver", so that could be a pain if you have a lot of them. Here's a script that builds up the results in a single variable and then shows all of them in one MsgBox:

AllOldServers:=""
Loop,HKCU,Printers\Connections,2,0
If (InStr(A_LoopRegName,"oldserver")<>0)
  AllOldServers:=AllOldServers . A_LoopRegName . "`n"
MsgBox,4096,Test Run,Found old print servers:`n%AllOldServers%

Regards, Joe
0
 
LVL 14

Expert Comment

by:Rob Miners
ID: 39805840
If you want to delete the Key and contentents use this method.

reg delete "HKCU\Printers\Connections\oldserver" /f
0
 
LVL 52

Expert Comment

by:Joe Winograd, EE MVE
ID: 39806199
Btw, I should have been clearer in my previous posts that an AHK script is simply a plain text file that you can create with any text editor, such as Notepad. Just copy/paste the code in my posts into a text file, but name it with a file type of AHK (not TXT). After installing AutoHotkey, it will own the file type AHK, so simply double-clicking it in Windows Explorer (or whatever file manager you use) will run the script. Of course, you could also put the script's file name (with the .AHK file type) in a batch file to run it. Regards, Joe
0
 
LVL 53

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 39806384
Here is a small BAT script that should do the job:

@echo off
setlocal

set Server=YOURSERVER

for /f "tokens=*" %%A in ('reg query "HKCU\Printers\Connections" ^| find /i ",%Server%,"') do (
  reg delete "%%~A" /f
)

Open in new window

Or, if you just want to do it at a command line I think this might work there:

for /f "tokens=*" %A in ('reg query "HKCU\Printers\Connections" ^| find /i ",YOURSERVER,"') do @reg delete "%~A" /f

Open in new window

~bp
0
 
LVL 9

Author Comment

by:samiam41
ID: 39806689
Billprew, I copy and pasted that code and ran it with no results.  I threw a pause in there to see if there were any errors and I didn't see any.  I also tried the code from the command prompt and it doesn't throw an error and appears to "complete" but the registry doesn't show any changes.  Thoughts?
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 39806706
Did you change the "YOURSERVER" to the name of the server you are wanting to remove from the registry?

~bp
0
 
LVL 9

Author Comment

by:samiam41
ID: 39806763
rrjmin0, just tried that and no dice.  I get an error that the system was unable to find the specified registry key or value.
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 39806787
If you do the following at a command line, what does it display, and is the server you are trying to remove listed?

reg query "HKCU\Printers\Connections"

Open in new window

~bp
0
 
LVL 9

Author Comment

by:samiam41
ID: 39806851
billprew, it worked once I spelled out the HKEY_CURRENT_USER (another engineer here recommended I try that.)

Thanks!!
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 39806871
Interesting, although I tried it here with the shortened version and it worked.  I'm on Win7, you?  Maybe older versions of REG command didn't allow the shortening.  If you do the following at a command line, does it indicate the 4 character abbreviations are allowed?

REG QUERY /?

~bp
0
 
LVL 9

Author Closing Comment

by:samiam41
ID: 39806880
Thanks everyone for your help but billprew supplied a batch script version that I could easily plug into the environment and allow us to continue our migration.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
When you try to share a printer , you may receive one of the following error messages. Error message when you use the Add Printer Wizard to share a printer: Windows could not share your printer. Operation could not be completed (Error 0x000006…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
This Micro Tutorial will give you a basic overview of Windows Live Photo Gallery and show you various editing filters and touches to photos you can apply. This will be demonstrated using Windows Live Photo Gallery on Windows 7 operating system.

770 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