Search code examples
vbscriptregistry

Error: "Invalid root in registry key" with Shell.RegWrite


When running this code:

CreateObject("WScript.Shell").RegWrite "HKLM\Whatever\", "MyValue", "REG_SZ"

I have problems with .RegWrite only on Windows 8 where I get the error:

"Invalid root in registry key".


Solution

  • Well, with so unhelpful error description I need to do some tests to find out that it was permission issue. However, elevate the script solve this. And to make this real answer I should post what I done, right?

    Call ElevateUAC
    
    ' ---------------------------
    ' my duty task goes here...
    ' ---------------------------
    
    
    Sub ElevateUAC
        If Not WScript.Arguments.Named.Exists("elevated") Then
            'Launch the script again as administrator
            With CreateObject("Shell.Application")
            .ShellExecute "wscript.exe", """" & _
                WScript.ScriptFullName & """ /elevated", "", "runas", 1
                WScript.Quit
            End With
        End If
    End Sub