Accessing the Registry from Scripts

The following code is used to write, read, and delete information in the Registry. Include the following three routines in your script:

Sub RegistryWrite(KeyName, ValueName, ValueData, ValueType)
  ValueType = UCase(ValueType)
  If ValueType <> "REG_DWORD" and ValueType <> "REG_BINARY" Then _
                                                   ValueType = "REG_SZ"
  Set WshShell = WScript.CreateObject("WScript.Shell")
  WshShell.RegWrite KeyName & "\" & ValueName, ValueData, ValueType
End Sub

Function RegistryRead(KeyName, ValueName)
  Set WshShell = WScript.CreateObject("WScript.Shell")
  RegistryRead = WSHShell.RegRead(KeyName & "\" & ValueName)
End Function

Sub RegistryDelete(KeyName, ValueName)
  Set WshShell = WScript.CreateObject("WScript.Shell")
  WshShell.RegWrite KeyName & "\" & ValueName, ""
  WshShell.RegDelete KeyName & "\" & ValueName
End Sub

Using these three routines, you can accomplish nearly all Registry tasks. To create a Registry key, type this (note that all HKEY... roots must appear in uppercase):

Call RegistryWrite("HKEY_LOCAL_MACHINE\New Key", "", "", "")

To assign data to a Registry value:

Call RegistryWrite("HKEY_LOCAL_MACHINE\My Key", "Some Value", _ 
                                                   "Some Data", "")

Leave " Some Value " blank to set the (default) value. To read the data stored in a given value:

Variable = RegistryRead("HKEY_LOCAL_MACHINE\My Key", "Some Value")

Leave " Some Value " blank to read the (default) value. To delete a key:

Call RegistryDelete("HKEY_LOCAL_MACHINE\My Key", "")

To delete a value:

Call RegistryDelete("HKEY_LOCAL_MACHINE\My ...

Get Windows Me Annoyances now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.