GuidesManaging Windows Registry with Scripting (Part 2)

Managing Windows Registry with Scripting (Part 2)

ServerWatch content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.




In the second article of this series, I will continue presenting ways of using scripting with Windows Management Instrumentation methods in order to manipulate Windows registry. Note, that these methods require WMI Standard Registry provider, which is installed by default on Windows 2000 and XP platforms.
In his latest article, Marcin Policht presents additional ways of using scripting with Windows Management Instrumentation (WMI) methods in order to manipulate the Windows registry.

The first article contained examples of implementing the following methods:

  • GetBinaryValue – reading registry value of BINARY type
  • GetDWORDValue – reading registry value of DWORD type
  • GetExpandedStringValue – reading registry value of EXPANDED STRING type
  • GetMultiStringValue – reading registry value of MULTI STRING type
  • GetStringValue – reading registry value of STRING type

I will continue, by providing code samples showing how to take advantage of the remaining methods:

  • CreateKey – creates registry key
  • SetBinaryValue – writing registry value of BINARY type
  • SetDWORDValue – writing registry value of DWORD type
  • SetExpandedStringValue – writing registry value of EXPANDED STRING type
  • SetMultiStringValue – writing registry value of MULTI STRING type
  • SetStringValue – writing registry value of STRING type
  • DeleteKey – deleting registry key
  • DeleteValue – deleting registry value
  • EnumKey – enumerating registry key
  • EnumValues – enumerating registry value

As before, the proper execution of the code relies on having available a number of constants:


Const HKEY_CLASSES_ROOT 	= &H80000000
Const HKEY_CURRENT_USER 	= &H80000001
Const HKEY_LOCAL_MACHINE 	= &H80000002
Const HKEY_USERS 		= &H80000003
Const HKEY_CURRENT_CONFIG 	= &H80000005

To save some space, we will assume that these five constants are defined in every one of our sample scripts (just make sure you copy them at the beginning of each).

CreateKey

The following code will create key named MyCompany in the HKEY_LOCAL_MACHINESOFTWARE area of the registry. The target computer name is stored in the variable sComputer. To use this script, set sComputer, hTree, and sKey variables to appropriate values (to use the local computer, you can set sComputer to a single dot (.)


sComputer	= "MyComputer"
sMethod		= "CreateKey"
hTree		= HKEY_LOCAL_MACHINE
sKey		= "SOFTWAREMyCompany"

Set oRegistry	= GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
		sComputer & "/root/default:StdRegProv")

Set oMethod	= oRegistry.Methods_(sMethod)
Set oInParam	= oMethod.inParameters.SpawnInstance_()

oInParam.hDefKey 	= hTree
oInParam.sSubKeyName	= sKey

Set oOutParam	= oRegistry.ExecMethod_(sMethod, oInParam)

SetBinaryValue

In this example, I create a binary value named BinValue (located in the previously created registry key) and set it to ff 01 01 01. Note that aValue is an array of strings, each representing an integer between 0 and 255 (representing a single byte).


sComputer	= "MyComputer"
sMethod		= "SetBinaryValue"
hTree		= HKEY_LOCAL_MACHINE
sKey		= "SOFTWAREMyCompany"
sValueName	= "BinValue"
aValue		= Array("255","1","1", "1")

Set oRegistry	= GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
		sComputer & "/root/default:StdRegProv")

Set oMethod	= oRegistry.Methods_(sMethod)
Set oInParam	= oMethod.inParameters.SpawnInstance_()

oInParam.hDefKey = hTree
oInParam.sSubKeyName = sKey
oInParam.sValueName = sValueName
oInParam.uValue = aValue

Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)

SetDWordValue

This time, the code creates a registry entry named DWordValue of type DWORD, located in the same key as in the previous two examples. The entry is set to decimal 11 (or 0x0000000b in hexadecimal). Note that the value is specified as an integer (but you can use any other radix that is recognized by VBScript).


sComputer	= "MyComputer"
sMethod		= "SetDWordValue"
hTree		= HKEY_LOCAL_MACHINE
sKey		= "SOFTWAREMyCompany"
sValueName	= "DWordValue"
uValue		= 11

Set oRegistry	= GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
		sComputer & "/root/default:StdRegProv")

Set oMethod	= oRegistry.Methods_(sMethod)
Set oInParam	= oMethod.inParameters.SpawnInstance_()

oInParam.hDefKey = hTree
oInParam.sSubKeyName = sKey
oInParam.sValueName = sValueName
oInParam.uValue = uValue

Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)

SetExpandedStringValue

Expanded string type contains system environment variables. Here is an example of a script creating such entry (using %SystemRoot% environment variable).


sComputer	= "MyComputer"
sMethod		= "SetExpandedStringValue"
hTree		= HKEY_LOCAL_MACHINE
sKey		= "SOFTWAREMyCompany"
sValueName	= "ExpandedStringValue"
sValue		= "%SystemRoot%MyCompany"

Set oRegistry	= GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
		sComputer & "/root/default:StdRegProv")

Set oMethod	= oRegistry.Methods_(sMethod)
Set oInParam	= oMethod.inParameters.SpawnInstance_()

oInParam.hDefKey = hTree
oInParam.sSubKeyName = sKey
oInParam.sValueName = sValueName
oInParam.sValue = sValue

Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)

SetMultiStringValue

Multisting type consists of multiple strings. Note that when viewed in the REGEDT32 on Windows 2000, each string will appear on a separate line. REGEDIT displays them as one continous sting of characters.


sComputer	= "MyComputer"
sMethod		= "SetMultiStringValue"
hTree		= HKEY_LOCAL_MACHINE
sKey		= "SOFTWAREMyCompany"
sValueName	= "MultiStringValue"
aValue		= Array("www","swynk","com")

Set oRegistry	= GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
		sComputer & "/root/default:StdRegProv")

Set oMethod	= oRegistry.Methods_(sMethod)
Set oInParam	= oMethod.inParameters.SpawnInstance_()

oInParam.hDefKey = hTree
oInParam.sSubKeyName = sKey
oInParam.sValueName = sValueName
oInParam.sValue = aValue

Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)

SetStringValue

This method is the most straightforward and most commonly used.


sComputer	= "MyComputer"
sMethod		= "SetStringValue"
hTree		= HKEY_LOCAL_MACHINE
sKey		= "SOFTWAREMyCompany"
sValueName	= "StringValue"
sValue		= "www.swynk.com"

Set oRegistry	= GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
		sComputer & "/root/default:StdRegProv")

Set oMethod	= oRegistry.Methods_(sMethod)
Set oInParam	= oMethod.inParameters.SpawnInstance_()

oInParam.hDefKey = hTree
oInParam.sSubKeyName = sKey
oInParam.sValueName = sValueName
oInParam.sValue = sValue

Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)

DeleteKey

As the name indicates, this method deletes existing key. Here is the sample code:


sComputer	= "MyComputer"
sMethod		= "DeleteKey"
hTree		= HKEY_LOCAL_MACHINE
sKey		= "SOFTWAREMyCompany"

Set oRegistry	= GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
		sComputer & "/root/default:StdRegProv")

Set oMethod	= oRegistry.Methods_(sMethod)
Set oInParam	= oMethod.inParameters.SpawnInstance_()

oInParam.hDefKey = hTree
oInParam.sSubKeyName = sKey

Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)

DeleteValue

As before, the name of the method is self-explanatory and no additional comments are necessary.


sComputer	= "MyComputer"
sMethod		= "DeleteValue"
hTree		= HKEY_LOCAL_MACHINE
sKey		= "SOFTWAREMyCompany"
sValueName	= "ToBeDeleted"

Set oRegistry	= GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
		sComputer & "/root/default:StdRegProv")

Set oMethod	= oRegistry.Methods_(sMethod)
Set oInParam	= oMethod.inParameters.SpawnInstance_()

oInParam.hDefKey = hTree
oInParam.sSubKeyName = sKey
oInParam.sValueName = sValueName

Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)

EnumKey

EnumKey method enumerates all subkeys for the key, which name you specify. This sample script displays each subkey separately. Note, that the results are obtained by listing entries of the array stored in the sNames property (part of output parameter object).


sComputer	= "MyComputer"
sMethod		= "EnumKey"
hTree		= HKEY_LOCAL_MACHINE
sKey		= "SOFTWAREMicrosoftWindows NTCurrentVersion"

Set oRegistry	= GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
		sComputer & "/root/default:StdRegProv")

Set oMethod	= oRegistry.Methods_(sMethod)
Set oInParam	= oMethod.inParameters.SpawnInstance_()

oInParam.hDefKey = hTree
oInParam.sSubKeyName = sKey

Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)

For i=0 To UBound(oOutParam.Properties_("sNames"))
	WScript.Echo oOutParam.Properties_("sNames")(i)
Next

EnumValues

This method works similarly to the previous one, but instead of enumerating subkeys of a key, it performs the same for all values contained in it. The output contains also the type of each value. The types are represented as integers. In order to make the output more meaningful, I assigned appropriate name to each value and defined them as constants. The output displays each value and its corresponding data type as a single line.


Const REG_SZ		= 1
Const REG_EXPAND_SZ	= 2
Const REG_BINARY	= 3
Const REG_DWORD		= 4
Const REG_MULTI_SZ	= 7

sComputer	= "MyComputer"
sMethod		= "EnumValues"
hTree		= HKEY_LOCAL_MACHINE
sKey		= "SOFTWAREMicrosoftWindows NTCurrentVersionWinlogon"

Set oRegistry	= GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
		sComputer & "/root/default:StdRegProv")

Set oMethod	= oRegistry.Methods_(sMethod)
Set oInParam	= oMethod.inParameters.SpawnInstance_()

oInParam.hDefKey = hTree
oInParam.sSubKeyName = sKey

Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)

For i=0 To UBound(oOutParam.Properties_("sNames"))
	sMessage = oOutParam.Properties_("sNames")(i)
	Select Case oOutParam.Properties_("Types")(i)
		Case REG_SZ		sMessage = sMessage & " :REG_SZ"
		Case REG_EXPAND_SZ	sMessage = sMessage & " :REG_EXPAND_SZ"
		Case REG_BINARY		sMessage = sMessage & " :REG_BINARY"
		Case REG_DWORD		sMessage = sMessage & " :REG_DWORD"
		Case REG_MULTI_SZ	sMessage = sMessage & " :REG_MULTI_SZ"
	End Select
	WScript.Echo sMessage
Next


This article was originally published on Aug 13, 2002

Page
1 of
1

Thanks for your registration, follow us on our social networks to keep up-to-date

Get the Free Newsletter!

Subscribe to Daily Tech Insider for top news, trends & analysis

Latest Posts

Related Stories