Install a .REG file without using REGEDIT.EXE

This is pure VBScript code, that also works when access to REGEDIT is denied by policy. Can be easily called from loginscripts. Not feature-complete (only strings and DWORDs) but works with most .REG files.

Script

Visual Basic
Edit Script
'========================================================================== 
' 
' NAME: Reg.vbs 
' 
' AUTHOR: Dennis C. Eijkelboom 
' DATE  : 23-12-2010 
' 
' SYNTAX:  
'    cscript reg.vbs <regfilepaths> 
' 
' EXAMPLE: 
'    cscript reg.vbs proxyserver.reg searchscopes.reg 
' 
'========================================================================== 

Option Explicit 

Dim FileSystemObject: Set FileSystemObject = CreateObject("Scripting.FileSystemObject") 
Dim objWSHShell: Set objWSHShell = CreateObject("WScript.Shell") 
Call Main 
Set FileSystemObject = Nothing 
Set objWSHShell = Nothing 

Sub Main 'Process all registry export files that are passed through as commandline parameters 
    Const ForReading = 1 
    Const OpenAsUnicode = -1 
    Dim RegFile 
    Dim argument 
    For Each argument In WScript.Arguments 
        Set RegFile = FileSystemObject.OpenTextFile(argument,ForReading,,OpenAsUnicode) 
        ProcessRegFile RegFile 
        RegFile.Close 
        Set RegFile = Nothing 
    Next 
End Sub 

Sub ProcessRegFile(RegFile) 'Read through the registry file and write each registry value 
    Dim line: line = "" 
    Dim key: key = "" 
    Dim name: name = "" 
    Dim value: value = "" 
    While Not RegFile.AtEndOfStream 
        line = RegFile.ReadLine 
        If Left(line,1) = "[" Then 'key line 
            key = Mid(line,2,Len(line)-2) 
        ElseIf Left(line,1) = """" Then 'value line 
            line = Mid(line,2,Len(line)-1) 
            name = Left(line,InStr(line,"=")-2) 
            value = Mid(line,Len(name)+3) 
            If Len(key) > 0 And Len(name) > 0 And Len(value) > 0 Then 
                ProcessRegValue key,name,value 
            End If 
        End If 
    Wend 
End Sub 

Sub ProcessRegValue(key,name,value) 'Write the registry value under passed name and key 
    key = Replace(Replace(key,"HKEY_lOCAL_MACHINE","HKLM"),"HKEY_CURRENT_USER","HKCU") 
    If Left(value,6) = "dword:" Then 
        value = Mid(value,7) 
        Dim wordvalue: wordvalue = CLng("&H" & value) 
        objWSHShell.RegWrite key & "" & name,wordvalue,"REG_DWORD" 
    Else 
        value = Replace(Mid(value,2,Len(value)-2),"\","") 
        objWSHShell.RegWrite key & "" & name,value,"REG_SZ" 
    End If 
End Sub
Verified on the following platforms:
Windows Server 2008 R2 Yes
Windows Server 2008 Yes
Windows Server 2003 Yes
Windows 7 Yes
Windows Vista Yes
Windows XP Yes
Windows 2000 Yes
This script is tested on these platforms.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.