Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I've been trying to run a VBScript file every time my system starts up, and have tried modifying the registry using VBScript to add it to the list of programs to run, but I'm facing a problem with permissions even though I'm the computers administrator. I'm trying to add VB_Start.vbs to the list of startup tasks by modifying the registry with the following VBScript code:

Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
WshShell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\Visual Basic" , "VB_Start.vbs" , "REG_SZ"
Set WshShell = Nothing

When I execute this code I'm getting the following error:

Error: Invalid root in registry key HKEY_LOCAL_MACHINE\SOFTWARE\......
Error Code: 80070005
Source: WshShell.RegWrite
share|improve this question

2 Answers 2

80070005 indicates an access denied error. Just because you're an admin doesn't mean your VBS is being executed automatically with admin rights. Add the following code to the top of the script:

If WScript.Arguments.Named.Exists("elevated") = False Then
  CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """ /elevated", "", "runas", 1
  WScript.Quit
End If

The code will detect if the script is running elevated, else relaunch itself with admin rights (you'll still see the prompt of course).

share|improve this answer
Const HKEY_CURRENT_USER = &H80000001

strComputer = "."

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &
strComputer & "\root\default:StdRegProv")

strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"
strValueName = "Hidden"
strStringValues = "0"

oReg.SetDWORDValue HKEY_CURRENT_USER,strKeyPath,strValueName,strStringValues

This is the format of code I use for adding registry keys. All you'd have to change is what's in strValueName, the strStringValues, and the initial CONST to point to the proper section in the registry. Those codes are:

Const HKEY_CLASSES_ROOT     = &H80000000
Const HKEY_CURRENT_USER     = &H80000001
Const HKEY_LOCAL_MACHINE    = &H80000002
Const HKEY_USERS        = &H80000003
Const HKEY_CURRENT_CONFIG   = &H80000005
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.