VBScript to Delete HKCU values or keys

Posted on Updated on

Based upon my VBScript to create HKCU registry keys and/or values, I’ve created a script that does the opposite of deleting keys and/or values.  This code was also updated to locate the default user profile path for Windows 7 (and newer).

' Author: Nick Moseley, https://t3chn1ck.wordpress.com
' Comments: This script will parse all User profiles on the computer, load their  HKCU hive,
'    then set the appropriate registry keys.
' History: '    1.0 (04/07/2009) - Initial script
'    1.1 (06/03/2009) - Added example for setting dword values based on MyITForum question
'    1.2 (06/05/2009) - Added additional comments to identify the section where to define the values to be set
'    1.3 (09/23/2010) - Corrected comment typos
'    2.0 (11/12/2012) - Changed base script to DELETE specified keys/values
Option Explicit
Const ForAppending = 8
Const HKLM = &H80000002
' ************************************************
' Configure the following values to define the HKCU keys to be deleted
' ************************************************
Const sUserKey = "\Software\SampleKey"
Const sUserKeyValueName = "SampleValueName"
' ************************************************
Dim oReg, oFSO, oFile, oUserSubkey, aUserProfiles, oShell
Dim sProfileLCase, sRegExe, sRegLoad, sRegUnload, sHiveName, sSubPath, sProfile, sValueName, sKeyPathUserProfiles, sValue, ReturnVal
Set oReg = GetObject("winmgmts:\\.\root\default:StdRegProv")
Set oShell = CreateObject ("WScript.Shell")
Set oFSO = CreateObject ("Scripting.FileSystemObject")
' Begin configuration of existing user profiles
sValueName = "ProfileImagePath"
sKeyPathUserProfiles = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
sRegExe = "C:\Windows\system32\reg.exe"
oReg.EnumKey HKLM, sKeyPathUserProfiles, aUserProfiles
' Parse all user keys
For Each oUserSubkey In aUserProfiles
    sSubPath = sKeyPathUserProfiles & "\" & oUserSubkey
    oReg.GetExpandedStringValue HKLM,sSubPath,sValueName,sValue
    sProfile = Split(sValue, "\")
    sProfileLCase = LCase(sProfile(2))
     If sProfileLCase = "system32" Then
         ' Do nothing
     ElseIf sProfileLCase = "localservice" Then
        ' Do nothing
     ElseIf sProfileLCase = "networkservice" Then
        ' Do nothing
      ElseIf sProfileLCase = "serviceprofiles" Then
        ' Do nothing
          sHiveName = "TempHive_" & sProfileLCase
      ' Load user's profile hive into a temp location
          sRegLoad = " LOAD HKLM\" & sHiveName & " """ & sValue & "\ntuser.dat"""
          oShell.Run sRegExe & sRegLoad, 0, True
         ' Call subroutine to change registry key
        SetConfigUserHive (sHiveName)
         ' Unload user's profile hive
        sRegUnload = " UNLOAD HKLM\" & sHiveName
       oShell.Run sRegExe & sRegUnload, 0, True
      End If
' Default User Profile sHiveName = "TempHive_DefaultUser"
sRegLoad = " LOAD HKLM\" & sHiveName & " ""C:\Users\Default\ntuser.dat"""
oShell.Run sRegExe & sRegLoad, 0, True
SetConfigUserHive (sHiveName)
sRegUnload = " UNLOAD HKLM\" & sHiveName
oShell.Run sRegExe & sRegUnload, 0, True
' End script
WScript.Quit ()
Sub SetConfigUserHive (sTempHive)
    Dim sTempHiveKeyPath
    ' Path of registry keys
    sTempHiveKeyPath = sTempHive & sUserKey
   'wscript.echo "sHiveName = " & sTempHiveKeyPath
    ' Delete value
    ReturnVal = oReg.DeleteValue(HKLM, sTempHiveKeyPath & "\", sUserKeyValueName)
     ' Delete key
     ReturnVal = oReg.DeleteKey(HKLM, sTempHiveKeyPath & "\", sUserKey)
End Sub

5 thoughts on “VBScript to Delete HKCU values or keys

    Scripting HKCU Changes « t3chn1ck said:
    November 12, 2012 at 8:16 pm

    […] NOTE: If you’re looking for a sample script to delete a registry key or value, see https://t3chn1ck.wordpress.com/2012/11/12/vbscript-to-delete-hkcu-values-or-keys/ […]

    Clif said:
    January 13, 2015 at 7:41 am

    How can I modify this to delete entire registry key ie HKCU\software\microsoft\mscrmclient

    Clif said:
    January 14, 2015 at 5:28 am

    Thanks for the reply. the issue I am experiencing is the script above seems not to be loading the registry hive prior to running the delete sub routine. i have replaced the line for oShell.Run sRegExe & sRegUnload, 0, True just to test with the same thing running a cmd and I was able to see the error messages pop-up saying I cannot load the hive i must use the registry itself.

      N. Moseley responded:
      January 14, 2015 at 6:33 am

      Since you are running it manually while in Windows, this error is likely happening when it gets to your user profile in the sequence. For testing, try adding code to exclude your username. Also add some echo statements to give you a message on what profile is currently in process.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s