Solved

Delete registry key via batch script

Posted on 2014-01-23
17
1,955 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 28

Expert Comment

by:becraig
ID: 39804834
Are you open to using powershell  ?
0
 
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 51

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 51

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
IT, Stop Being Called Into Every Meeting

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 51

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 51

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 51

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 51

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 51

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
This Micro Tutorial will teach you how to change your appearance and customize your Windows 7 interface to your unique preference. This will be demonstrated using Windows 7 operating system.
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.

746 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

11 Experts available now in Live!

Get 1:1 Help Now