A lot has happened since I wrote my articles on MDT Scripting last year. For one thing, I’ve learned how to compile my code in a script block on my blog.
Two of my readers have asked about deleting registry keys using MDT Scripts but there does not appear to be a function for it in the ZTIUtility.vbs.
I’ve knocked up something quickly. It’s a simple function devoid of error handling. It will delete a key named “HKEY_LOCAL_MACHINE\SOFTWARE\ScriptimusExMachina\Unwanted Registry Key”
To Delete a Registry Key
<job id="Z-Sample"> <script language="VBScript" src="ZTIUtility.vbs"/> <script language="VBScript"> ' //*************************************************************************** ' // ' // Solution: Scriptimus Ex Machina - Example Script ' // File: ZTI-Delete-Key.wsf ' // ' // Purpose: This script will delete a Registry Key ' // ' // Usage: cscript ZTI-Delete-Key.wsf [/debug:true] ' // ' // Customer Build Version: 1.0.0 ' // Customer Script Version: 1.0.0 ' // Customer History: ' // ' // ***** End Header ***** ' //*************************************************************************** '//---------------------------------------------------------------------------- '// '// Global constant and variable declarations '// '//---------------------------------------------------------------------------- Option Explicit Dim iRetVal '//---------------------------------------------------------------------------- '// End declarations '//---------------------------------------------------------------------------- '//---------------------------------------------------------------------------- '// Main routine '//---------------------------------------------------------------------------- On Error Resume Next iRetVal = ZTIProcess ProcessResults iRetVal On Error Goto 0 '//--------------------------------------------------------------------------- '// '// Function: ZTIProcess() '// '// Input: None '// '// Return: Success - 0 '// Failure - non-zero '// '// Purpose: Perform main ZTI processing '// '//--------------------------------------------------------------------------- ' To Delete a Registry Key Value Function RegDelete( sRegValue ) RegDelete = oShell.RegDelete ( sRegValue ) end Function Function ZTIProcess() iRetVal = Success ZTIProcess = iRetval ' Call function and delete key Dim sRegDeleteValue, sRegKey sRegKey = "HKEY_LOCAL_MACHINE\SOFTWARE\ScriptimusExMachina\Unwanted Registry Key" oLogging.CreateEntry "Preparing to delete Registry key " & sRegKey, LogTypeInfo sRegDeleteValue=regDelete(sRegKey) End Function </script> </job>
Any issues with this, remember to read my first article Hello World.
Hi Andrew,
Before Anything else, Thank so much for the great post. Sorry to bother but I’m hoping you can help me. I am trying to run this script on a Windows 8.1 machine and i keep getting errors.
I verified and re-verified that i could delete the key by using reg delete HKEY_LOCAL_MACHINE\SOFTWARE\cnb command successfully. Any pointers would be appreciated. Thank You.
LikeLike
it appears that errors were deleted:
“”
LikeLike
Indeed!
LikeLike
ZTI ERROR – Unhandled error returned by Configure-SetWindowsUpdateDefaults: Unable to remove registry key “HKEY_LOCAL_MACHINE\SOFTWARE\cnb”. (-2147024894 0x80070002)
LikeLike
You should implement some error handling to trap the error you’re getting and return the error code. error -2147024894 is: The system cannot find the file specified.
LikeLike
Hi Andrew – So i have a registry key called
HKEY_LOCAL_MACHINE\SOFTWARE\ZOHO Corp\ManageEngine AssetExplorer\Agent\
inside that registry, there is a key called AgentId – how can I delete JUST THAT KEY when running this script?
Thanks in advance for the help.
LikeLike
Try a search for deleting registry key values using vbscript and merge it with this this example.
LikeLike