How to use the Windows Script Host to read, write, and delete registry keys

Article translations Article translations
Article ID: 244675 - View products that this article applies to.
This article was previously published under Q244675
Expand all | Collapse all

SUMMARY

The Microsoft Windows Script Host (WSH) is a tool that allows you to run Microsoft Visual Basic Scripting Edition and Microsoft JScript natively within the base operating system, either on Microsoft Windows 95 or Microsoft Windows NT 4.0. It also includes several COM automation methods that allow you to do several handy tasks easily through the Windows Script Host Object Model.

The Microsoft Windows Script Host is integrated in Windows 98. It is available for Windows NT 4.0 from within the Windows NT 4.0 Option Pack. It is also available for download from the following site:
http://msdn2.microsoft.com/en-us/library/ms950396.aspx
The Windows Script Host provides several useful methods to read, write, and delete registry keys easily. The RegWrite function's third parameter allows the programmer to specify the type of data added to the registry. RegWrite supports strType as REG_SZ, REG_EXPAND_SZ, REG_DWORD and REG_BINARY. If another data type is passed as strType, RegWrite returns E_INVALIDARG.

MORE INFORMATION

Important This section, method, or task contains steps that tell you how to modify the registry. However, serious problems might occur if you modify the registry incorrectly. Therefore, make sure that you follow these steps carefully. For added protection, back up the registry before you modify it. Then, you can restore the registry if a problem occurs. For more information about how to back up and restore the registry, click the following article number to view the article in the Microsoft Knowledge Base:
322756 How to back up and restore the registry in Windows
The Microsoft Visual FoxPro code below demonstrates several examples of reading, writing, and deleting registry keys and values.
WSHShell = CreateObject("WScript.Shell")
 
*!* Create Registry Keys
WSHShell.Popup( "Create key HKCU\MyRegKey with value 'Top level key'")
WSHShell.RegWrite( "HKCU\MyRegKey\", "Top level key")
 
WSHShell.Popup( "Create key HKCU\MyRegKey\Entry with value 'Second level key'")
WSHShell.RegWrite( "HKCU\MyRegKey\Entry\", "Second level key")
 
WSHShell.Popup( "Set value HKCU\MyRegKey\Value to REG_SZ 1")
WSHShell.RegWrite ("HKCU\MyRegKey\Value", 1)
 
WSHShell.Popup( "Set value HKCU\MyRegKey\Entry to REG_DWORD 2")
WSHShell.RegWrite( "HKCU\MyRegKey\Entry", 2, "REG_DWORD")
 
WSHShell.Popup( "Set value HKCU\MyRegKey\Entry\Value1 to REG_BINARY 3")
WSHShell.RegWrite( "HKCU\MyRegKey\Entry\Value1", 3, "REG_BINARY")
 
 
*!* Read Registry Keys
lcValue1 = WSHShell.RegRead("HKCU\MyRegKey\")
WSHShell.Popup("Value of HKCU\MyRegKey: " + lcValue1)
 
lcValue2 = WSHShell.RegRead("HKCU\MyRegKey\Entry\")
WSHShell.Popup("Value of HKCU\MyRegKey\Entry\: " + lcValue2)
 
lcValue3 = WSHShell.RegRead("HKCU\MyRegKey\Value")
WSHShell.Popup("Value of HKCU\MyRegKey\Value: " + lcValue3)
 
lnValue1 = WSHShell.RegRead("HKCU\MyRegKey\Entry")
WSHShell.Popup("Value of HKCU\MyRegKey\Entry: " + ALLTRIM(STR(lnValue1)))
 
lnValue3 = WSHShell.RegRead("HKCU\MyRegKey\Entry\Value1")
WSHShell.Popup("Value of HKCU\MyRegKey\Entry\Value1: " + ALLTRIM(STR(lnValue3(1))))
 
 
*!* Delete Registry Keys
WSHShell.Popup( "Delete value HKCU\MyRegKey\Entry\Value1")
WSHShell.RegDelete( "HKCU\MyRegKey\Entry\Value1")
 
WSHShell.Popup ("Delete key HKCU\MyRegKey\Entry")
WSHShell.RegDelete( "HKCU\MyRegKey\Entry\") 
 
WSHShell.Popup ("Delete key HKCU\MyRegKey")
WSHShell.RegDelete( "HKCU\MyRegKey\") 

REFERENCES

To see the following white papers:
  • White paper: Windows Script Host: A Universal Scripting Host for scripting languages
  • Technical paper: Windows Script Host programmer's reference
visit the following Microsoft Developer Network (MSDN) Web site:
http://msdn2.microsoft.com/en-us/library/ms950396.aspx
For additional information about using Visual FoxPro and the Windows Script Host, click the following article number to view the article in the Microsoft Knowledge Base:
244677 How to create a Desktop shortcut using the Windows Script Host
244676 How to map, delete and enumerate network drives using the Windows Script Host

Properties

Article ID: 244675 - Last Review: August 27, 2007 - Revision: 3.7
APPLIES TO
  • Microsoft Visual FoxPro 3.0 Standard Edition
  • Microsoft Visual FoxPro 3.0b Standard Edition
  • Microsoft Visual FoxPro 5.0 Standard Edition
  • Microsoft Visual FoxPro 5.0a
  • Microsoft Visual FoxPro 6.0 Professional Edition
  • Microsoft Visual FoxPro 7.0 Professional Edition
  • Microsoft Visual FoxPro 8.0 Professional Edition
  • Microsoft Visual FoxPro 9.0 Professional Edition
Keywords: 
kbcodesnippet kbhowto KB244675

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com