WMI StdRegProv SetBinaryValue fails in HKLM

In Windows 8.1, I cannot set a binary value in HKLM, but can in HKCU - this example script demonstrating the issue and is derived from http://blogs.technet.com/b/heyscriptingguy/archive/2005/02/24/how-can-i-write-binary-data-to-the-registry.aspx

const HKEY_CURRENT_USER = &H80000001
const HKEY_LOCAL_MACHINE = &H80000002

strComputer = "."

Set objRegistry = GetObject _
    ("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

strKeyPath = "Software"
strValueName = "BinaryTest"
arrValues = Array(1,2,3,4,5,6,7,8,9,10)

errReturn = objRegistry.SetBinaryValue(HKEY_CURRENT_USER, strKeyPath, strValueName, arrValues)

If errReturn = 0 Then
        Wscript.Echo "Worked in HKCU."
Else
        WScript.Echo "Failed in HKCU"
End If

errReturn = objRegistry.SetBinaryValue(HKEY_LOCAL_MACHINE, strKeyPath, strValueName, arrValues)

If errReturn = 0 Then
        Wscript.Echo "Worked in HKLM."
Else
        WScript.Echo "Failed in HKLM. Return code: " & errReturn
End If

The return value on failure is 5. Script is running with Administrative rights.



  • Edited by StuartDunkeld Saturday, February 07, 2015 4:00 PM edit
February 7th, 2015 6:59pm

You are writing to a protected key.  A return code of 5 means "access denied"

Don't forget to be an administrator running as an administrator.

Free Windows Admin Tool Kit Click here and download it now
February 7th, 2015 9:06pm

You are writing to a protected key.  A return code of 5 means "access denied"

Don't forget to be an administrator running as an administrator.

February 8th, 2015 2:50am

Yes but RegEdit forces you to run as a full admin.  VBScript does not.  Run elevated and it will work.
Free Windows Admin Tool Kit Click here and download it now
February 8th, 2015 3:04am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics