How to delete Registry Key using QTP/VBScript?

As I have already talked about How to read/modify and create Registry Settings in previous posts, today I will be talking about how to delete Registry Keys. Again, there are 2 ways to delete Registry Keys:

  • Using VBScript: This method only works for keys with no child key or subkey

 Dim objShell, RegLocate

 'create windows shell object
 Set objshell =CreateObject("WScript.Shell")

 RegLocate ="HKCU\Software\Microsoft\Internet Explorer\New Windows\PopupMgr"
 'Reading Registry will throw an error if path doesn't exists
 On Error Resume Next
 reg_value=objshell.RegRead (RegLocate)
 On Error Goto 0
 'check for the error and then delete if it exists (error number is 0)
 If Err.Number = 0 Then
 objShell.RegDelete RegLocate
 msgbox "Registry does not exists"
 End If
 Set objshell = Nothing

  • Using WMI: This method also works if there are subkeys for a key

 Const HKCU = &H80000001
 Dim objReg
 strComputer = "."
 Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
 objReg.EnumKey HKCU, strKeyPath, arrSubkeys
 If IsArray(arrSubkeys)=True Then
 For Each strSubkey In arrSubkeys
 DeleteSubkeys HKCU, strKeyPath & "\" & strSubkey
 End If
 objReg.DeleteKey HKCU, strKeyPath


Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s