Windows Scripting
  Home arrow Windows Scripting arrow Page 2 - 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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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 - Putting it Together


    (Page 2 of 4 )

    Let’s put all of this to some real world use. We’re going to build a script that reads some information from the registry, displays it to us, then deletes the value and writes it back. Of course, we wouldn’t perform all of these actions together in a real script. This is just to demonstrate the functionality.

    Let’s use our script to read the currently installed product ID. This information can be found in the ProductID value under the following registry key:

            HKEY_LOCAL_MACHINE\Software\Windows\CurrentVersion\

    We start our script by establishing some variables and creating a call to the Shell object. Then we use our RegRead method to get this information. Our code begins as follows:

          strRegValue = “HKLM\Software\Microsoft\Windows\CurrentVersion\
    ProductID”
          Set WshShell = WScript.CreateObject("WScript.Shell")
          strPID = WshShell.RegRead (strRegValue)
          Wscript.Echo strPID

    Okay, here’s what we’ve done so far. We’ve assigned our registry value to the strRegValue variable. Then we use the CreateObject method to create an instance of the Shell object and assign it to the WshShell variable. Our next line uses the RegRead method to pull the information from the registry and assign it to the strPID variable. As documented, you should be able to use this line without the parenthesis, however, since it returns a value it technically is a function and requires the use of parenthesis. We finish by using the Echo method to return the contents of strPID to the user. Now for our next piece of code.

            WshShell.RegDelete strRegValue
            Wscript.Echo “The registry value has been deleted.”

    This piece of code is simpler. It just deletes our registry value and then tells the user what we’ve done. Next, we have to write it back.

            WshShell.RegWrite strRegValue, strPID
            Wscript.Echo “The registry value has been written back.”

    This part of the code uses the RegWrite method to write our value back into the registry and then uses the Echo method to tell the user when it has been written. The complete code for this example looks like this:

    strRegValue = "HKLM\Software\Microsoft\Windows\CurrentVersion\ProductID"
    Set WshShell = WScript.CreateObject("WScript.Shell")
    strPID = WshShell.RegRead strRegValue
    Wscript.Echo strPID
    WshShell.RegDelete strRegValue
    Wscript.Echo "The registry value has been deleted."
    WshShell.RegWrite strRegValue, strPID
    Wscript.Echo "The registry value has been written back."

    Well, that’s the WSH way, but why stop there? I want to demonstrate all of the ways that WSH can work with the registry. The next way uses WMI, or Windows Management Instrumentation. In short, WMI is a set of features that is used to manage Windows.

    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...
     

    WINDOWS SCRIPTING ARTICLES

    - Introducing Two-Way Data Binding using Silve...
    - Silverlight 2.0 Application Development with...
    - Burning Multisession CDs with IMAPI2 in WSH
    - Creating a Silverlight 2.0 Application that ...
    - Burning CDs with the IMAPI2 Control
    - Burning CDs in Windows XP with WSH
    - Advanced Word Object Scripting
    - Reading and Printing Word Documents in WSH
    - Scripting Microsoft Word
    - Using WSH to Catalog MP3 Files
    - Reading MP3 ID3 Tags in WSH
    - A Brief Look at Menus in WPF
    - More Examples of Simplified Image Processing...
    - Completing a WPF To-Do List Application
    - Simplified Image Processing in GDI+





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT