title: search registry for value if match found delete parent key
body: <p>I need help with a script that would traverse through the registry for a particular value and once match is found delete the parent key. I have found a code but it does not work. I suspect that is in not traversing through the registry key for match.</p>
  
  <pre><code>Option Explicit
  
  Const HKEY_LOCAL_MACHINE   = &amp;H80000002 
  Const cRegKeyStartingPath  = "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
  Const cRegValueToFind      = "Ossec HIDS"
  Const cRegDataToMatch      = "DisplayName" 
  
  Dim oReg, subkey, arrSubKeys, sCurrentKey, sCurrentValuePath, iDeletedCount
  iDeletedCount = 0
  
  Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
  oReg.EnumKey HKEY_LOCAL_MACHINE, cRegKeyStartingPath, arrSubKeys
  
  On Error Resume Next
  For Each subkey In arrSubKeys
      If Err.Number = 0 Then 'in case the collection is empty
          sCurrentKey = "HKLM\" &amp; cRegKeyStartingPath &amp; subkey
          sCurrentValuePath = sCurrentKey &amp; "\" &amp; cRegValueToFind
          If customRegRead(sCurrentValuePath) = cRegDataToMatch Then
              WScript.Echo "Going to delete "&amp;sCurrentKey
              DeleteRegKey sCurrentKey&amp;"\"
              iDeletedCount = iDeletedCount + 1
          End If
      Else
          iDeletedCount = -1
      End If
  Next
  
  Select Case iDeletedCount
      Case 0
          WScript.Echo "No matching keys found"
      Case -1
          WScript.Echo "No subkeys found below HKLM\"&amp;cRegKeyStartingPath
      Case Else
          WScript.Echo "Deleted " &amp; iDeletedCount &amp; " keys"
  End Select
  
  Function customRegRead(sRegValue)
  
      Dim oShell
      Dim sRegReturn
  
      Set oShell = CreateObject("WScript.Shell")
  
      On Error Resume Next
      Err.Clear
      sRegReturn = oShell.RegRead(sRegValue)
      If Err.Number&lt;&gt;0 Then
          customRegRead = "Failed To Read Value"
      Else
          customRegRead = sRegReturn
      End If  
  
  End Function
  
  Sub DeleteRegKey(sKey)
  
      Dim oShell
      Set oShell = CreateObject("Wscript.Shell")
      oShell.RegDelete sKey
  
  End Sub
  </code></pre>
  
  <p>If there is something cleaner/better please advise.</p>