Windows Scripting
  Home arrow Windows Scripting arrow Working with the Windows Registry in WSH
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Working with the Windows Registry in WSH
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 14
    2006-08-29

    Table of Contents:
  • Working with the Windows Registry in WSH
  • Putting it Together
  • The WMI Way
  • Other Value Types

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Working with the Windows Registry in WSH


    (Page 1 of 4 )

    One of the most used procedures in Windows Scripting is reading from and writing to the Windows registry. Today we’re going to learn how WSH makes this a very simple task. I’m going to show you ways to do this natively in WSH by using COM objects and I’m also going to show you how to do this by implementing WMI. So hang on, and let’s see how powerful WSH can be.

    Introduction

    Whether you’re using WSH for system automation or to roll out security policies on several machines, editing the registry will inevitably come up. WSH provides several methods for doing this. We’re going to look in depth at the RegRead, RegWrite, and RegDelete methods and see how to properly implement them in your scripts.

    The RegRead, RegWrite, and RegDelete methods belong to the Wscript Shell object. The Shell object is used primarily for reading system information, working with environmental variables, editing the registry, and managing shortcuts. So our first task is to use our script to connect to the Shell object.

    Learning the Methods

    We create an instance of the shell object by using Wscript’s CreateObject() method. So our first line looks like this:

          Set WshShell = WScript.CreateObject("WScript.Shell")

    This creates an instance of the Shell object and assigns it to the WshShell variable.  ext, let’s learn a little about the methods we discussed earlier.

    The RegRead method is used to read values stored in the Windows registry. We provide the registry key and value and the RegRead method returns the data assigned to that value. Here is the syntax for the RegRead method:

                object.RegRead Name

    Object refers to the variable we’ve assigned to reference the Shell object. In our case, that's WshShell. Name refers to the key, value pair that we are looking for in the registry. When naming registry keys and values in WSH we refer to their hierarchy using the same conventions that we would use to name Windows directory structures. Each of the registry roots can be expressed in full or by using their abbreviated names as shown in the table below.

                Table 1:  Registry Root Naming Conventions

                Abbreviation                 Root Name

                HKCR                         HKEY_CLASSES_ROOT

                HKCU                         HKEY_CURRENT_USER

                HKLM                         HKEY_LOCAL_MACHINE

                HKU                            HKEY_USERS

                HKCC                         HKEY_CURRENT_CONFIG

    The RegWrite method is used to write data to a registry value or to create a registry key.  Its syntax is similar to the RegRead method.

                object.RegWrite Name, Value [, Type]

    Again, object refers to the variable that we’ve assigned to the Wscript Shell object. Name also refers to the registry key or value that we want to work with.  Now we have a few added parameters. Value refers to the data that we wish to write in to the specified key or value. The type attribute is optional and is used to define the type of value we wish to create or edit. If you do not specify, the default REG_SZ type is used. Valid options are REG_SZ, REG_EXPAND_SZ, REG_DWORD and REG_BINARY. Value is automatically converted to a string for REG_SZ and REG_EXPAND_SZ and to an integer for REG_DWORD or REG_BINARY.

    You can differentiate between registry keys and registry values by including the trailing forward slash (\) for key names.

    The RegDelete method is used to remove keys or values from the registry. Its syntax is much simpler than RegWrite.  You simply have to specify the key or value to be removed.

    object.RegDelete Name \

    The syntax for RegDelete is identical to that of RegRead.  Now that we have the tools to do the job, let’s see some examples.

    More Windows Scripting Articles
    More By Nilpo


       · Working with the registry is one of the most fundamental necessities in the Windows...
       · Nice, well written article. Great for beginners, because it starts at the...
       · Thank you very much. I appreciate your kind words. I've got a lot more of these in...
       · "object.SetDWORDStringValue Root, Path, Value, Integer"returns an errorIt should...
       · Thanks for spotting that typo. Much appreciated.However, it is not necessary to...
       · Thank you for sharing.
     

    WINDOWS SCRIPTING ARTICLES

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT