Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


March 2003

Editing the Registry

Use the WshShell object to make and record registry changes
RSS
Subscribe to Windows IT Pro | See More Registry Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

You probably use Control Panel or other tools for most server-tuning tasks, but some recommended fixes require you to edit the registry by hand. Manually editing the registry can easily mess up your OS to the point of no return, so if a registry modification is important enough to make by hand, it's important enough to make consistently on all applicable computers. You probably also want to document the registry edit and its purpose, as well as confirm the changes.

If you've ever manually edited the registry on more than a few computers, you probably know one way to make changes without navigating through the HKEY maze more than once. You can save the edited key as a .reg file, then import that file to all the computers that need the same fix. However, that approach doesn't log the change and supports only a limited amount of granularity (you can import a key, but you can't import one edited value). A better approach is to use VBScript to write your change to the registry and record changes in the computer's Application log so that you—and others—can tell what edits you've made and why. You can modify the same script to read the registry and thus confirm your changes.

A Good Example
You can edit any part of the registry through VBScript, but for an example, let's suppose that you're editing the registry on your application servers so that a particular application will refer to usernames instead of computer names. The edit is application-specific, so you must be sure to edit the correct application's subkey. The subkey's final edited value is the sum of two hexadecimal numbers (rather than something easy, such as 0 or 1), so you must be sure to enter the right number on all application servers. The edit's purpose isn't obvious, so you'll want a record explaining the edit.

To accomplish these goals, you need a script that performs several tasks. The script must create a WshShell object (which represents the Windows Shell and thus gives you programmatic access to the registry-editing tools and Event Viewer), write the change to the registry, and record the change in the Application log.

Writing the Change
I discuss WshNetwork, a Windows Script Host (WSH) 2.0 object representing network-accessible printers and drives, in "Connecting to Printers," August 2002, http://www.winnetmag.com, InstantDoc ID 25652, and "Connecting Users to Network Resources," June 2002, http://www.winnetmag.com, InstantDoc ID 24893. WshShell, another WSH object, deals with certain Windows Shell­related objects, including the registry editor and Event Viewer.

WshShell supports several methods for reading or editing the registry. Generally, using the registry-editing methods is simple as long as you're exact; putting even an extra space in the string will return an error. For this example, you're going to work with the RegRead method, which reads a subkey or value, and the RegWrite method, which writes a subkey or value. These methods use a simple syntax:

WshShell.RegRead strName

and

WshShell.RegWrite strName, varValue, [strtype]
   Previous  [1]  2  Next 


Reader Comments
The code listed in Listing 1 will not work. You cannot use () marks around a method unless it is used as part of an assignment.

oWshShell.RegWrite ("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Terminal
Server\Compatibility\Applications\AppName\Flags", 18)

This Should read:

oWshShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Terminal
Server\Compatibility\Applications\AppName\Flags", 18

Otherwise it will error out with a syntax error.



David Figueroa August 27, 2003


You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

New Microsoft/Yahoo! Deal? No

On Sunday, the Times of London reported that Microsoft had renewed talks with failing Internet giant Yahoo! and would manage its search engine for 10 years, while Yahoo! would retain control of its email, messaging, and content services. This report ...

How can I stop and start services from the command line?

...


Windows OSs Whitepapers Why SaaS is the Right Solution for Log Management

Related Events Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing