Change registry key name with a script

Posted on 2009-04-19
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.
Question by:KhaosRogue
    LVL 10

    Expert Comment

    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?

    Author Comment

    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.  
    LVL 10

    Accepted Solution

    This script should do what you are looking for.
    Const ForReadIng = 1, ForWriting = 2, ForAppending = 8, _
    	HKEY_CURRENT_USER = &H80000001, HKEY_LOCAL_MACHINE = &H80000002
    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("")
    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
    		strNewKey = strMSConfigKey1
    		strOldKey = strMSConfigKey
    	End if
    Sub ChangeKey
    	If objFSO.FileExists("C:\tmp") then
    		Set objExec2 = objShell.Exec("cmd /c type C:\tmp")
    		Do Until objExec2.Status
       			Wscript.Sleep 250
    		strTMP = objExec2.stdout.readall
    		set objNewFile = objFSO.CreateTextFile("C:\tmp2",True)
    		set objWriteNewFile = objFSO.OpenTextFile("C:\tmp2",ForWriting)
    		objWriteNewFile.write replace(strTMP,strOldKey,strNewKey)
    		Set objExec3 = objShell.Exec("regedit /s C:\tmp2")
    		Do Until objExec3.Status
       			Wscript.Sleep 250
    		Set objExec4 = objShell.Exec("reg delete "&chr(34)&"HKLM\"&strOldKey&chr(34)&" /f")
    		Do Until objExec4.Status
       			Wscript.Sleep 250
    		If objFSO.FileExists("C:\tmp") then objFSO.DeleteFile("C:\tmp")
    		If objFSO.FileExists("C:\tmp2") then objFSO.DeleteFile("C:\tmp2")
    	End If
    End Sub

    Open in new window


    Author Closing Comment

    Thank you TakedaT that did the trick :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    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

    Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
    Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    759 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

    13 Experts available now in Live!

    Get 1:1 Help Now