/build/static/layout/Breadcrumb_cap_w.png

VBSCRIPT TO DELETE REGISTRY VALUE DATA

Hi everyone,

Need an urgent help. Need a script to delete a registry value. [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows]
"AppInit_DLLs"="SAMPLE"

I only want to delete the data value "SAMPlE"

0 Comments   [ + ] Show comments

Answers (28)

Posted by: anonymous_9363 14 years ago
Red Belt
0
Seriously? Well, you asked for it! :) http://www.lmgtfy.com/?q=vbscript+delete+registry+value
Posted by: AngelD 14 years ago
Red Belt
0
While using the Google hint Ian gave you; you want to set an entry to empty ("") and not delete it
Posted by: gizmolala 14 years ago
Third Degree Blue Belt
0
Thanks for ur help.
I only want to delete the value data = "SAMPLE" if it exist in [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows]
"AppInit_DLLs"="SAMPLE"

The AppInit_DLLs has different values in it. Just want to delete the value "SAMPLE" in the value data
Posted by: pjgeutjens 14 years ago
Red Belt
0
Again, using what Ian pointed to:

1) read the current value
2) If Value = "SAMPLE" then Write value = ""
3) (Else do nothing)
Posted by: anonymous_9363 14 years ago
Red Belt
0

I only want to delete the value data = "SAMPLE" if it exist in [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows]
"AppInit_DLLs"="SAMPLE"

The AppInit_DLLs has different values in it. Just want to delete the value "SAMPLE" in the value data
It would have saved you a considerable amount of time if you had defined the problem more precisely at the outset.

Presumably the entries in the value are separated in some way? If the entry is a REG_MULTI_SZ, the separator will be a null string (Chr(0)). Whatever the separator is, get the value, then use Split (I'm presuming that your script will be VBS-driven) to create an array. Then, you simply loop through the array, looking for "SAMPLE". Everything except that string gets written to a temporary variable, and, when the loop completes, write the contents of the temporary string to the registry entry.
Posted by: gizmolala 14 years ago
Third Degree Blue Belt
0
this is my script but it does not delete the strvalue.

Option Explicit
Const HKEY_LOCAL_MACHINE = &H80000002

Dim strComputer
Dim objRegistry
Dim strKeyPath
Dim strValueName
Dim strValue
Dim arrValues
Dim intValue

strComputer = "."
Set objRegistry=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")

'Get String value
strKeyPath = "Software\Microsoft\Windows NT\CurrentVersion\Windows"
strValueName = "AppInit_DLLs"
strValue = "PGPmapih.dll"
objRegistry.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue

'Delete String value
strKeyPath = "Software\Microsoft\Windows NT\CurrentVersion\Windows"
strValueName = "AppInit_DLLs"
objRegistry.DeleteValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
Posted by: WSPPackager 14 years ago
Senior Purple Belt
0
Hi,

Try the below code and let me know...

'------------------------------------------------------------
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")

strKeyPath = "Software\Microsoft\Windows NT\CurrentVersion\Windows"
strStringValueName = "AppInit_DLLs"

oReg.DeleteValue HKEY_LOCAL_MACHINE,strKeyPath,strStringValueName

'-----------------------------------------------------------------
Posted by: gizmolala 14 years ago
Third Degree Blue Belt
0
tnx. your script works but it deletes the StringValueName. All i want is the Strvalue "PGPmapih.dll" to be deleted not the all Strvaluename.
Posted by: anonymous_9363 14 years ago
Red Belt
0
Continue to ignore my post, that'll be key to your success. I wonder why I bother sometimes...
Posted by: gizmolala 14 years ago
Third Degree Blue Belt
0
@VBScab, am not trying to ignore ur post. The problem is am new to vbscripting and i don't know how to go about the logic you said. Would be glad if you can help with the script.
Posted by: anonymous_9363 14 years ago
Red Belt
0
TBH, you won't get very far in packaging without some scripting skills.

I don't normally spoon-feed, but what the hell...some array handling for you to pick the bones out of. It's completely off the top of my head and untested:Dim blnResult
Dim arrRegValue
Dim strTempRegValue
Dim strTemp
Dim intArrayIndex_RegValue
Dim blnMatchFound

Const strRegValueSeparator = "," '// or whatever the values are separated with
Const strSearch = "SAMPLE" '// this is the text you want removed from strValue

arrRegValue = Split(strValue, strRegValueSeparator)

strTempValue = ""

For intArrayIndex_RegValue = 0 To UBound(arrRegValue)
strTemp = arrRegValue(intArrayIndex_RegValue)
blnResult = StringMatch(strTemp, strSearch, blnMatchFound)
If blnResult Then
If Not blnMatchFound Then
'// The strings didn't match the one we're searching for so add it to the temporary variable
If Len(strTempValue) = 0 Then
strTempValue = strTemp
Else
strTempValue = strTempValue & strRegValueSeparator & strTemp
End If
End If
End If
Next

'// Now write strTempValue to registry



'//=========================================================================================================
'// Name: StringMatch
'// Purpose: Checks if two strings match
'// Why not use 'If strFirst = strSecond', you're asking?
'// Well, the 'Equals' operator :
'// - is not very fast (at string comparison)!
'// - compares strings left to right and is smart enough to stop comparing when it spots the first difference, but
'// - is too dumb to first do the most obvious test: comparing the lengths of the strings!
'// Input: strFirst - the first string
'// strSecond - the second string
'// blnMatch - a Boolean indicating whether or not the strings matched
'// Output: None
'// Returns: True/False
'//
'//=========================================================================================================
Function StringMatch(ByVal strFirst, ByVal strSecond, ByRef blnMatch)

StringMatch = True

blnMatch = False

If LenB(strFirst) = LenB(strSecond) Then
blnMatch = (InStrB(1, strFirst, strSecond, vbBinaryCompare) <> 0)
End If

End Function
Posted by: gizmolala 14 years ago
Third Degree Blue Belt
0
@VBScab. Tnx so much for ur help. here's a simple one that works but it deletes the whole value instead of the said value mention in script.
Dim WSHShell
Set WSHShell = WScript.CreateObject("WScript.Shell")
Dim returnval
returnval= WSHShell.RegRead("HKLM\Software\Microsoft\Windows NT\CurrentVersion\Windows\AppInit_DLLs")

if instr(returnval, "PGPmapih.dll") > 0 then
'"PGPmapih.dll" exists so set it to nothing
WSHShell.Regwrite "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Windows\AppInit_DLLs" , ""
end if
Posted by: anonymous_9363 14 years ago
Red Belt
0
it deletes the whole value instead of the said value It would.

You need to add the code I supplied. If you want me to do your job for you, do say and I'll provide a quote.

That may sound harsh but learning is mostly about following examples, getting it wrong, fixing it and so on.
Posted by: gizmolala 14 years ago
Third Degree Blue Belt
0
nice one mate. wots ur quote
Posted by: anonymous_9363 14 years ago
Red Belt
0
That's commercially sensitive information. PM me an email address and I'll send you a quote.
Posted by: gizmolala 14 years ago
Third Degree Blue Belt
0
[email=gizmolala@yahoo.com]gizmolala@yahoo.com[/email] ASAP.
Posted by: AutomateIT 14 years ago
Senior Yellow Belt
0
Hey gizmolala... first problem (in my opinion) was choosing VBScript for this. Registry manipulation is a built in function of AutoIT and SMS Installer and Wise InstallMaster and Wise Script Express... just to be more informative.
Posted by: AngelD 14 years ago
Red Belt
0
Auto,

It would be the same problem with the other solutions if you don't know how to use them, don't you think?
Posted by: AutomateIT 14 years ago
Senior Yellow Belt
0
Not at all... I'm seeing lines and lines of VBScript for what would be one line in tools that are designed for this type of scripting. Lets keep a focus on the processes and not get stuck on the tool.
Posted by: anonymous_9363 14 years ago
Red Belt
0
I would *love* to see a legible one-line script which reads a registry value, parses it for a string which needs to be removed, removes that string and then re-writes the edited string back to the registry...
Posted by: Jsaylor 14 years ago
Second Degree Blue Belt
0
ORIGINAL: AutomateIT

Not at all... I'm seeing lines and lines of VBScript for what would be one line in tools that are designed for this type of scripting. Lets keep a focus on the processes and not get stuck on the tool.


One line to instantiate the WMI registry provider. One line to read the value, One if/then block to determine if it's a value to be overwritten, one more line to write an empty string to the value.

Sounds like five whole lines to me, all the rest is error handling and abstraction that isn't strictly necessary. I realize tools with greater ease-of-use are appealing, but the readily available information and ridiculous flexibility of VBscript is what makes it the de facto tool of choice for a large number of people.
Posted by: AutomateIT 14 years ago
Senior Yellow Belt
0
With the addition of the logic to verify the current contents of the registry key value, which I see was added after the original posting. We are at about 4 lines of SMS Installer script.

My point is simply that there are many tools available with a perhaps more suitable "place" for each of them. Just trying to provide some insight.

Sample
Posted by: anonymous_9363 14 years ago
Red Belt
0
Insight is all very well but, as is all too common, the script sample has ZERO error-trapping. Hardly surprising, since error handling in SMS Installer (an early version of WiseScript, of course) is on par with DOS batch, i.e. to all intents and purposes utterly useless.

What happens if the registry key being queried is permissioned such that the user can't read it? Failure. Any message presented to the user? Nah...why would the user be interested whether the script worked or not, eh?

Oh, and BTW, you have also failed to read the thread, since your example merely sets the value data to a null string. The OP wants to remove ONE ELEMENT from the data therein.
Posted by: AutomateIT 14 years ago
Senior Yellow Belt
0
Perhaps you might differentiate opinion with fact, so those who know better disregard your posts.
Posted by: Jsaylor 14 years ago
Second Degree Blue Belt
0
Down to ad hominem in twelve posts? Most impressive.

The various wrapper "languages" that have been developed out there can certainly be effective, they have technical limitations in what they can accomplish. They are certainly easy to use, but why learn a product with inherent limitations, when you could be using a language with direct access to other API's, WMI, etc..., as well as reporting and control that's as robust and flexible as you can imagine?

I guess it doesn't make sense to me to use kiddie blocks when you could build with something real instead.
Posted by: gizmolala 14 years ago
Third Degree Blue Belt
0
Hi Guys. Tnx all for ur help. Got a working script at the end of the day and sharing it. Script is pasted below.


'**********************************************************************************************************************************************
' This Script performs the following tasks
' 1. Checks the Users Permissions to the registry
' 2. If the user has access to write to the registry searches to see if the
' following registry key exists and return the values: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\APPINIT_DLLS
' Searches to see if PGPmapih.dll is a value and strips it out
' Replaces the PGPmapih.dll with blanks
' 3. Writes all errors etc... to a log file c:\winnt\temp\pgpmapih_fix.log
'***********************************************************************************************************************************************

'**************
'Start of Code
'**************

On Error Resume Next

Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("C:\WINNT\Temp\pgpmapih_fix.log", ForWriting)

'Declare and Set any variables
pgpvalue = "PGPmapih.dll"
const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")

' Check Permissions to see if current user can write to registry
Set StdOut = WScript.StdOut
strKeyPath = "SYSTEM\CurrentControlSet"

oReg.CheckAccess HKEY_LOCAL_MACHINE, strKeyPath, KEY_QUERY_VALUE, _
bHasAccessRight
If bHasAccessRight = True Then
objFile.WriteLine (date & " " &Time &" User Has Query Value Access Rights")
Else
objFile.WriteLine (date & " " &Time &" User Does Not Have Query Value Access Rights")
End If

oReg.CheckAccess HKEY_LOCAL_MACHINE, strKeyPath, KEY_SET_VALUE, _
bHasAccessRight
If bHasAccessRight = True Then
objFile.WriteLine (date & " " &Time &" User Has Set Value Access Rights")
Else
objFile.WriteLine (date & " " &Time &" User Does not have Set Value Access Rights")
End If

oReg.CheckAccess HKEY_LOCAL_MACHINE, strKeyPath, KEY_CREATE_SUB_KEY, _
bHasAccessRight
If bHasAccessRight = True Then
objFile.WriteLine (date & " " &Time &" User Has Create SubKey Access Rights")
Else
objFile.WriteLine (date & " " &Time &" User Does Not Have Set Create Subkey Access Rights")
End If

oReg.CheckAccess HKEY_LOCAL_MACHINE, strKeyPath, DELETE, bHasAccessRight
If bHasAccessRight = True Then
objFile.WriteLine (date & " " &Time &" User Has Delete Access Rights")
Else
objFile.WriteLine (date & " " &Time &" User Does Not have Delete Access Rights")
End If

'Change Path to PGP One
strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows"
strValueName = "AppInit_DLLs"

'Search Registry to find the Key
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
objFile.WriteLine (date & " " &Time &" Value of Registry Key " & strvalue)

if isnull(strKeyPath) then
objFile.WriteLine (date & " " &Time &" ERROR: Registry Key Path Not Found")
objFile.Close
wscript.quit
else

If isNull(strvaluename) then
objFile.WriteLine (date & " " &Time &" ERROR: Registry Key Does Not Exist on PC")
objFile.Close
wscript.quit
else

If strvalue = "" then
objFile.WriteLine (date & " " &Time &" Registry Key VALUE Is Blank")
objFile.WriteLine (date & " " &Time &" No Need to Run Script, Now Quitting")
objFile.Close
wscript.quit
else

objFile.WriteLine (date & " " &Time &" Registry Key Found")

end if
end if

newstrvalue = instr(strvalue,pgpvalue)
if newstrvalue = 0 then
objFile.WriteLine (date & " " &Time &" PGP Value not found on machine")
objFile.WriteLine (date & " " &Time &" No Need to Run Script, Now Quitting")
objFile.Close
wscript.quit

else
End if
newstrvalue = Replace(strvalue,pgpvalue,"")
objFile.WriteLine (date & " " &Time &" Changing Registry key value to: " & newstrvalue)

'Write new Key
oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,newstrValue
objFile.WriteLine (date & " " &Time &" SUCCESS: Values Written to Registry")
objFile.WriteLine (date & " " &Time &" Closing Script... END...")
End if
objFile.Close
Wscript.quit
Posted by: anonymous_9363 14 years ago
Red Belt
0
There! Nice one. Wasn't so hard, was it? :)

Some notes...

- Please use the CODE tag when posting code or other lengthy text. Access that tag by clicking the button marked 'code' (or '<%' in older browsers/FireFox)
- The CheckAccess parameters can be added together so you can check "combined" permissions in one hit. See here for details.
- You need additional error-trapping. Check that object's got created (If IsObject...), even the common ones like FileSystemObject. You assume, for example, that strKeyPath exists. How does your current script behave if it doesn't exist? Remember: defensive programming. ALWAYS assume that a script WILL fail and code accordingly.
- Rather than continuously access an object, use the With...End With construct. For example, rather than:If strvalue = "" then
objFile.WriteLine (date & " " &Time &" Registry Key VALUE Is Blank")
objFile.WriteLine (date & " " &Time &" No Need to Run Script, Now Quitting")
objFile.Close
wscript.quit
else

objFile.WriteLine (date & " " &Time &" Registry Key Found")

end if
use:With objFile
If strvalue = "" Then
.WriteLine (date & " " &Time &" Registry Key VALUE Is Blank")
.WriteLine (date & " " &Time &" No Need to Run Script, Now Quitting")
.Close
wscript.quit
Else
.WriteLine (date & " " &Time &" Registry Key Found")
End If
End With

- You could build the log data as you go, only writing it to the log file at the end.
- Nice use of Replace. I hadn't thought of that.
Posted by: gizmolala 14 years ago
Third Degree Blue Belt
0
tnx VBScab
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ