Windows – Can’t delete registry key using VBS

vbscriptwindowswindows 7

I am trying to delete a registry key. Here is my code:

Option Explicit
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )  
Dim strDelete
strDelete = objShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\test\")
WScript.echo(strDelete)
objShell.RegDelete "HKEY_LOCAL_MACHINE\SOFTWARE\test\"
WScript.Quit

When I run this, I get this output:

Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

test
N:\RogueDevelopment\test.vbs(23, 1) WshShell.RegDelete: Invalid root in registry key "HKEY_LOCAL_MACHINE\SOFTWARE\test\".

My registry key is located at "HKEY_LOCAL_MACHINE\SOFTWARE\test\" and it has a default value of "test". The value in the key is being read correctly, but the key cannot be deleted. I am running this script on Windows 7. Why is this happening?

Best Answer

You don't have permission to open HKEY_LOCAL_MACHINE\SOFTWARE\test\ for delete access. If you really need to modify keys under HKLM, you will need to elevate your process.

Related Topic