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

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.

 

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 HKCUMyRegKey with value 'Top level key'")
WSHShell.RegWrite( "HKCUMyRegKey", "Top level key")

WSHShell.Popup( "Create key HKCUMyRegKeyEntry with value 'Second level key'")
WSHShell.RegWrite( "HKCUMyRegKeyEntry", "Second level key")

WSHShell.Popup( "Set value HKCUMyRegKeyValue to REG_SZ 1")
WSHShell.RegWrite ("HKCUMyRegKeyValue", 1)

WSHShell.Popup( "Set value HKCUMyRegKeyEntry to REG_DWORD 2")
WSHShell.RegWrite( "HKCUMyRegKeyEntry", 2, "REG_DWORD")

WSHShell.Popup( "Set value HKCUMyRegKeyEntryValue1 to REG_BINARY 3")
WSHShell.RegWrite( "HKCUMyRegKeyEntryValue1", 3, "REG_BINARY")


*!* Read Registry Keys
lcValue1 = WSHShell.RegRead("HKCUMyRegKey")
WSHShell.Popup("Value of HKCUMyRegKey: " + lcValue1)

lcValue2 = WSHShell.RegRead("HKCUMyRegKeyEntry")
WSHShell.Popup("Value of HKCUMyRegKeyEntry: " + lcValue2)

lcValue3 = WSHShell.RegRead("HKCUMyRegKeyValue")
WSHShell.Popup("Value of HKCUMyRegKeyValue: " + lcValue3)

lnValue1 = WSHShell.RegRead("HKCUMyRegKeyEntry")
WSHShell.Popup("Value of HKCUMyRegKeyEntry: " + ALLTRIM(STR(lnValue1)))

lnValue3 = WSHShell.RegRead("HKCUMyRegKeyEntryValue1")
WSHShell.Popup("Value of HKCUMyRegKeyEntryValue1: " + ALLTRIM(STR(lnValue3(1))))


*!* Delete Registry Keys
WSHShell.Popup( "Delete value HKCUMyRegKeyEntryValue1")
WSHShell.RegDelete( "HKCUMyRegKeyEntryValue1")

WSHShell.Popup ("Delete key HKCUMyRegKeyEntry")
WSHShell.RegDelete( "HKCUMyRegKeyEntry")

WSHShell.Popup ("Delete key HKCUMyRegKey")
WSHShell.RegDelete( "HKCUMyRegKey") 

 

Reference: http://support.microsoft.com/kb/244675

Leave a Reply

Your email address will not be published. Required fields are marked *