Manipulating the registry is one of the most common tasks a Windows system administrator runs into. It can often become a time consuming task. When you have to change a registry setting on hundreds or even thousands of computers, manipulating a particular key or setting can be next to impossible if you had to do it all manually.
So here are a series of subs and functions to help you manipulate the registry from VBScript. It uses “regedit.exe” and it’s “/s” switch to read and write text files formatted as “.reg” files from and to the registry silently. So, obviously, you’ll need to have the regedit.exe in the path system variable (or in the same directory as this script).
One drawback you may encounter about the “RegRead” function is that it pulls registry key recursively throughout the whole key. So if you specify a root key, or a key farther up in the folder structure you could pull back massive amounts of data that you hadn’t intentionally meant to capture. So when you pass the registry key, be as specific as possible when you’re deciding on which key to read with this function.
Function RegRead(Key) 'This script is provided under the Creative Commons license located 'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not 'be used for commercial purposes with out the expressed written consent 'of NateRice.com Const OpenAsDefault = -2 Const ForReading = 1 Const FailIfNotExist = 0 Set oFSO = WScript.CreateObject("Scripting.FileSystemObject") Set oShell = CreateObject("WScript.Shell") sTempFile = oFSO.GetTempName oShell.Run "regedit /s /e """ & sTempFile & """ """ & Key & """",0,True If oFSO.FileExists(sTempFile) And oFSO.GetFile(sTempFile).Size > 0 Then Set oReadFile = oFSO.OpenTextFile(sTempFile, ForReading, False, OpenAsDefault) RegRead = oReadFile.ReadAll oReadFile.Close oFSO.DeleteFile sTempFile End If End Function Sub RegWrite(RegFile) 'This script is provided under the Creative Commons license located 'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not 'be used for commercial purposes with out the expressed written consent 'of NateRice.com Const ForWriting = 2 Set oFSO = CreateObject("Scripting.FileSystemObject") Set oShell = CreateObject("WScript.Shell") sTempFile = oFSO.GetAbsolutePathName(oFSO.GetTempName) oFSO.CreateTextFile(sTempFile) Set fTempFile = oFSO.GetFile(sTempFile) Set oTempFile = fTempFile.OpenAsTextStream(ForWriting, True) oTempFile.Write(RegFile) oTempFile.Close oShell.Run "regedit /s """ & sTempFile & """", 0, True oFSO.DeleteFile sTempFile, True End Sub Sub DeleteKey(Key) 'This script is provided under the Creative Commons license located 'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not 'be used for commercial purposes with out the expressed written consent 'of NateRice.com Const ForWriting = 2 Set oFSO = CreateObject("Scripting.FileSystemObject") Set oShell = CreateObject("WScript.Shell") sHeader = "Windows Registry Editor Version 5.00" & vbCrLf & vbCrLf sData = sHeader & "[-" & Key & "]" & vbCrLf WScript.Echo sData sTempFile = oFSO.GetAbsolutePathName(oFSO.GetTempName) oFSO.CreateTextFile(sTempFile) Set fTempFile = oFSO.GetFile(sTempFile) Set oTempFile = fTempFile.OpenAsTextStream(ForWriting, True) oTempFile.Write(sData) oTempFile.Close WScript.Echo "regedit /s """ & sTempFile & """" oShell.Run "regedit /s """ & sTempFile & """", 0, True oFSO.DeleteFile sTempFile, True End Sub Sub RenameKey(OldKeyName, NewKeyName) 'This script is provided under the Creative Commons license located 'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not 'be used for commercial purposes with out the expressed written consent 'of NateRice.com RegWrite Replace(RegRead(OldKeyName), OldKeyName, NewKeyName) DeleteKey OldKeyName End Sub