Modify the Vista Registry with VBScript

Modify the Windows Vista Registry with VBScript

Before we create a VBScript to modify the Vista registry, it is wise to at least consider the alternatives.  For example, would a group policy be a better way of achieving our goal?  Is it possible to achieve your aim with a .Reg file?  You could even create a logon script containing the command:
regedit /s xyz.reg

At the very least, let us have a walk-though with regedit; we want to make sure we are scripting in the correct area of the registry.  A walk-through will also prove that the change we are contemplating will have the desired effect. 

Much as I love VBScript, my lingering concern is that if you don’t know what you are doing, then you could destroy your registry, the worst case scenario is you would need a complete re-install of the operating sytem.

Topics for Modifying the Vista Registry with VBScript.

 ♦

.RegWrite

If you wish to control the Vista registry with VBScript, then .RegWrite is the most important and versatile method.  The simplest task for .RegWrite is to adjust data.  For example, suppose you want to display the Build Number on the Vista desktop.  You need .RegWrite to change the DWORD value for PaintDesktopVersion from zero, meaning off, to 1, meaning on.

.RegWrite is powerful because not only will it adjust data in the registry’s leaf objects, but also it will create new twigs, or even new branches, of the registry.  In practical terms, what I mean is if a value called PaintDesktopVersion did not exist, then .RegWrite could create it, all you have to do is specify the data type.  For example:
.RegWrite(HKEY_CURRENT_USER\Control Panel\Desktop\PaintDesktopVersion,"00000001", "REG_DWORD").

To take it a stage further .RegWrite would create PainDesktopVersion’s parent folder, Desktop, if it did not exist.

VBScript basics

We are overdue for a review of VBScript’s basic points.  To begin with, we need is to create an object, let us name it objShell.  This is the command to create our object:
Set objShell = CreateObject("WScript.Shell").

Once we have created objShell, then we can employ one of its many methods.  In VBScript such methods are preceded with a dot.  On this page, we will consider just three methods for modifying the registry, .RegWrite, .RegRead and .RegDelete.  However, in  other contexts VBScripts can employ different methods, such as .Run to launch programs.

Instructions to create your VBScript

  1. Copy and paste the script below into notepad.  Alternatively, get your free trial of OnScript.
  2. Save the file with .vbs extension e.g. Build.vbs
  3. Double click your VBScript, then ‘OK’ the message box.
  4. To help you understand what is happening in the registry, I recommend that you launch regedit and navigate to the section specified by strRoot.
  5. To observe the registry change, simply logoff and logon again.  You should now see
    VBScript Example for Windows Vista

.RegWrite Example

‘ Build.vbs
‘ Example VBScript to display the Build Number on the desktop.
‘ Author Guy Thomas http: //computerperformance.co.uk
‘ Version 1.2 – March 2007
‘ —————————————————————‘

Option Explicit
Dim objShell, strRoot, strModify, strDelete
strRoot = "HKEY_CURRENT_USER\Control Panel\Desktop\PaintDesktopVersion"
‘ Create the Shell object
Set objShell = CreateObject("WScript.Shell")
strModify = objShell.RegWrite(strRoot,"00000001", "REG_DWORD")
WScript.Echo "Error No: " & err.number & " check " & strRoot
strModify = null
WScript.Quit

‘ End of .RegWrite example script.
 

Guy Recommends: WMI Monitor and It’s Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

.RegRead

I don’t use .RegRead much.  What this method does is extract the string value or dword value from a registry setting.  What I find limiting is that .RegRead only examines the leaf value.  Let me amplify what I mean with an example, let us study this location:
HKEY_CURRENT_USER\Control Panel\Desktop\PaintDesktopVersion:dword =00000001.

With .RegRead you can read only the very end value, in this case, 1.  My frustration is there is no way of reading PaintDesktopVersion.

Possible uses of .RegRead

What you could do is read a value from the registry, then depending on the result, modify values in another area of the registry.  For example, if UserName = "Fred", then disable the UAC.  You could do this via the registry setting ConsentPromptBehavior.  Still, there is a better way of disabling the User Account Control for Fred, that would be by implementing a group policy.  It is precisely this situation where I would recommend that before using VBScript, see if there is another method.  And this is advice from someone who loves VBScript.

.RegRead Example 1

‘ BuildRead.vbs
‘ Example VBScript to read the value of PaintDesktopVersion.
‘ Author Guy Thomas http: //computerperformance.co.uk
‘ Version 1.2 – March 2007
‘ —————————————————————‘

Option Explicit
Dim objShell, strRoot, strModify
strRoot = "HKEY_CURRENT_USER\Control Panel\Desktop\PaintDesktopVersion"
‘ Create the Shell object
Set objShell = CreateObject("WScript.Shell")
strModify = objShell.RegRead(strRoot)
WScript.Echo "Value of PaintDesktopVersion is: " & strModify
strModify = null
WScript.Quit

‘ End of .RegRead example script.

 

.RegRead Example 2

Here is an example with primitive ‘If’ Logic.

‘ BuildRead.vbs
‘ Example VBScript to read the value of PaintDesktopVersion.
‘ Author Guy Thomas http: //computerperformance.co.uk
‘ Version 1.3 – March 2007
‘ —————————————————————‘

Option Explicit
Dim objShell, strRoot, strModify, strBuild
strRoot = "HKEY_CURRENT_USER\Control Panel\Desktop\PaintDesktopVersion"
‘ Create the Shell object
Set objShell = CreateObject("WScript.Shell")
strModify = objShell.RegRead(strRoot)
If strModify = "1" then
   strBuild = " This means enabled"
   Else strBuild =" no build number"
   End If
WScript.Echo "Value of PaintDesktopVersion is: " & strModify & strBuild
strModify = null
WScript.Quit

‘ End of .RegRead example script.
 

Solarwinds Config GeneratorGuy Recommends: The Free Config Generator

SolarWinds’ Config Generator is a free tool, which puts you in charge of controlling changes to network routers and other SNMP devices.  Boost your network performance by activating network device features you’ve already paid for.

Guy says that for newbies the biggest benefit of this free tool is that it will provide the impetus for you to learn more about configuring the SNMP service with its ‘Traps’ and ‘Communities’. Try Config Generator now – it’s free!

Download your free copy of Config Generator

.RegDelete

Before you even think about using .RegDelete, brush-up your VBScript skills.  Then focus on the .RegDelete syntax.  In particular, pay close attention to any trailing slash \Desktop\.  Beware if you don’t understand the significance of the last backslash, you could delete a whole branch of your registry with catastrophic results.  For example, if this is where you are working:
HKEY_CURRENT_USER\Control Panel\Desktop\PaintDesktopVersion

A command to delete:
HKEY_CURRENT_USER\Control Panel\Desktop\PaintDesktopVersion will just delete one value PaintDesktopVersion.  Observe, no trailing \.

However a command to delete
HKEY_CURRENT_USER\Control Panel\Desktop\ will delete all the values in the Desktop folder.  Note the final slash, Desktop\.

Worse, a command to delete
HKEY_CURRENT_USER\Control Panel\  would delete a whole branch of the registry.

Deleting HKEY_CURRENT_USER\ does not bear thinking about.

Perhaps you can now see why you should, take precautions before you experiment with .RegDelete.  What you can do is backup the registry via a backup of the system state.  You could also export the particular branch that’s the focus of your experiment. 

As a matter of learning technique, I also strongly recommend that you start by creating a test registry folder, and master deleting that before you practice on a live registry folder and its values.  Another obvious precaution is to backup the registry, or at the very least, Export that particular branch.

.RegDelete Example 1

Firstly, let us create a new registry folder called AGuy.  Then we will add a DWORD called Computer Performance.  Once we have done that, we can check it, and then use example 2 (see below) to delete this registry entry.

‘ CreateAGuy.vbs
‘ Example VBScript to create registry settings.
‘ Author Guy Thomas http: //computerperformance.co.uk
‘ Version 1.4 – March 2007
‘ —————————————————————‘

Option Explicit
Dim objShell, strRoot, strModify, strDelete
strRoot = "HKEY_CURRENT_USER\Control Panel\AGuy\ComputerPerformance"
‘ Create the Shell object
Set objShell = CreateObject("WScript.Shell")
strModify = objShell.RegWrite(strRoot,"00000001", "REG_DWORD")
WScript.Echo "Check " & strRoot
strModify = null
WScript.Quit

‘ End of .RegWrite example script.

 

.RegDelete Example 2 – The actual deletion

‘ Delete AGuy.vbs
‘ Example VBScript to delete a registry entry.
‘ Author Guy Thomas http: //computerperformance.co.uk
‘ Version 1.5 – March 2007
‘ —————————————————————‘

Option Explicit
Dim objShell, strRoot, strModify, strDelete
strRoot = "HKEY_CURRENT_USER\Control Panel\AGuy\"
‘ Create the Shell object
Set objShell = CreateObject("WScript.Shell")
strModify = objShell.RegDelete(strRoot)
WScript.Echo "Check " & strRoot
strModify = null
WScript.Quit

‘ End of .RegDelete example script.

There are a number of commands that we could use to achieve our goal.  We could assign the following values the variable strRoot:

  1. HKEY_CURRENT_USER\Control Panel\AGuy\    (Note the backslash)
  2. HKEY_CURRENT_USER\Control Panel\AGuy\ComputerPerformance

Monitor Your Network with the Real-time Traffic AnalyzerSolarwinds Real-time Traffic Analyzer

The main reason to monitor your network is to check that your all your servers are available.  If there is a network problem you want an interface to show the scope of the problem at a glance.

Even when all servers and routers are available, sooner or later you will be curious to know who, or what, is hogging your precious network’s bandwidth.  A GUI showing the top 10 users makes interesting reading.

Another reason to monitor network traffic is to learn more about your server’s response times and the use of resources.  To take the pain out of capturing frames and analysing the raw data, Guy recommends that you download a copy of the SolarWindsfree Real-time NetFlow Analyzer.

Summary of Configure the Vista Registry with VBScript

Because I love VBScript, I felt it only fair to stress that your registry task really is a job for VBScript.  For example is there a group policy to achieve your aim?  Another alternative would be merging a .Reg file into your registry.  If you are sure that this is indeed a job for a VBScript, then take the time to familiarise yourself with the syntax of the .RegWrite, .RegRead and especially, .RegDelete.

If you like this page then please share it with your friends

 


Windows Vista Registry Tweaks: