?
Solved

Change registry key name with a script

Posted on 2009-04-19
4
Medium Priority
?
1,128 Views
Last Modified: 2012-05-06
Hi there im trying to find a script or a batch file that i can use to change the name of a registry key. Im trying to rename HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\MSConfig so that when i disable items from the start up it will put the sys back in normal startup vs selective. Even a simple change to "MSConfig1" would work.
0
Comment
Question by:KhaosRogue
  • 2
  • 2
4 Comments
 
LVL 10

Expert Comment

by:TakedaT
ID: 24184582
From what I know, there is no way to actually "rename" a key programmatically.  To achieve almost the same effect, you can read the existing key, write it to a new key, then delete the old key.  This can become quite a lengthy script using vbscript depending on the depth and amount of subkeys you are talking about.  What exactly are you trying to accomplish?  Do you want to query to see if the MSConfig key is there and if it is, rename it to MSConfig1 and the opposite, if MSConfig1 is there rename it to MSConfig, basically toggleing between the two? Or are you looking for something else?
0
 

Author Comment

by:KhaosRogue
ID: 24186406
Not really a need to check for it i know the key is going to be there. Basically its just cutting time down at my job. After i edit the msconfig and do a cleanup on a system i want to be able to just run a script or a batch file or something of that nature to auto rename or read/write/delete it to a new name, Msconfig1, so that it puts the computer back in normal start up. I wouldn't need it to toggle back and forth just a one time change.  
0
 
LVL 10

Accepted Solution

by:
TakedaT earned 500 total points
ID: 24186479
This script should do what you are looking for.
Const ForReadIng = 1, ForWriting = 2, ForAppending = 8, _
	HKEY_CURRENT_USER = &H80000001, HKEY_LOCAL_MACHINE = &H80000002
 
strComputer="."
strMSConfigKey = "SOFTWARE\Microsoft\Shared Tools\MSConfig"
strMSConfigKey1 = "SOFTWARE\Microsoft\Shared Tools\MSConfig1"
strParentKeyPath = "SOFTWARE\Microsoft\Shared Tools"
Set objReg=GetObject("winmgmts:{impersonationlevel=impersonate}!\\" &_
	strComputer & "\root\default:StdRegProv")
Set objShell = CreateObject("Wscript.shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
objReg.EnumKey HKEY_LOCAL_MACHINE, strParentKeyPath, arrSubKeys
 
For Each subkey In arrSubKeys
	If subkey = "MSConfig" then
		Set objExec = objShell.Exec("regedit /e C:\tmp "&chr(34)&"HKEY_LOCAL_MACHINE\"&strMSConfigKey&chr(34))
		Do Until objExec.Status
   			Wscript.Sleep 250
		Loop
		strNewKey = strMSConfigKey1
		strOldKey = strMSConfigKey
		ChangeKey
	End if
Next
Sub ChangeKey
	If objFSO.FileExists("C:\tmp") then
		Set objExec2 = objShell.Exec("cmd /c type C:\tmp")
		Do Until objExec2.Status
   			Wscript.Sleep 250
		Loop
		strTMP = objExec2.stdout.readall
		set objNewFile = objFSO.CreateTextFile("C:\tmp2",True)
		objNewFile.close
		set objWriteNewFile = objFSO.OpenTextFile("C:\tmp2",ForWriting)
		objWriteNewFile.write replace(strTMP,strOldKey,strNewKey)
		objWriteNewFile.close
		Set objExec3 = objShell.Exec("regedit /s C:\tmp2")
		Do Until objExec3.Status
   			Wscript.Sleep 250
		Loop
		Set objExec4 = objShell.Exec("reg delete "&chr(34)&"HKLM\"&strOldKey&chr(34)&" /f")
		Do Until objExec4.Status
   			Wscript.Sleep 250
		Loop
		If objFSO.FileExists("C:\tmp") then objFSO.DeleteFile("C:\tmp")
		If objFSO.FileExists("C:\tmp2") then objFSO.DeleteFile("C:\tmp2")
	Else
		wscript.quit
	End If
End Sub

Open in new window

0
 

Author Closing Comment

by:KhaosRogue
ID: 31572125
Thank you TakedaT that did the trick :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month17 days, 5 hours left to enroll

862 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