MDT Scripting: Deleting Registry Keys

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.

About Andrew Barnes

A Scripting and Deployment Specialist.
This entry was posted in MDT 2010, MDT 2012, Scripting and tagged , . Bookmark the permalink.

7 Responses to MDT Scripting: Deleting Registry Keys

  1. Francisco Raigoza says:

    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.

    Like

  2. Francisco Raigoza says:

    it appears that errors were deleted:

    “”

    Like

  3. Francisco Raigoza says:

    ZTI ERROR – Unhandled error returned by Configure-SetWindowsUpdateDefaults: Unable to remove registry key “HKEY_LOCAL_MACHINE\SOFTWARE\cnb”. (-2147024894 0x80070002)

    Like

  4. Ricardo says:

    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.

    Like

Leave a comment