VBScript – Working with the Windows Registry

The script below demonstrates the write, read and deletion for each type of value in the Windows registry.

The registry value types are:

  • String (REG_SZ): A fixed-length text string.
  • Binary (REG_BINARY): Raw binary data. Most hardware component information is stored as binary data and is displayed in Registry Editor in hexadecimal format.
  • DWORD (REG_DWORD): Data represented by a number that is 4 bytes long (a 32-bit integer). Many parameters for device drivers and services are this type and are displayed in Registry Editor in binary, hexadecimal, or decimal format.
  • Multi-String (REG_MULTI_SZ): A multiple string. Values that contain lists or multiple values in a form that people can read are generally this type. Entries are separated by spaces, commas, or other marks.
  • Expandable String (REG_EXPAND_SZ): A variable-length data string. This data type includes variables that are resolved when a program or service uses the data.

Option Explicit

Const HKEY_CLASSES_ROOT = &H80000000

Const HKEY_CURRENT_USER = &H80000001

Const HKEY_LOCAL_MACHINE = &H80000002

Const HKEY_USERS = &H80000003

Const HKEY_CURRENT_CONFIG = &H80000005

Dim strComputer

Dim objRegistry

Dim strKeyPath

Dim strValueName

Dim strValue

Dim arrValues

Dim intValue

strComputer = “.”

Set objRegistry=GetObject(“winmgmts:{impersonationLevel=impersonate}!” & strComputer & “rootdefault:StdRegProv”)

‘Create Key

strKeyPath = “Registry Test”

objRegistry.CreateKey HKEY_CURRENT_USER,strKeyPath

‘Set String value

strKeyPath = “Registry Test”

strValueName = “String Test”

strValue = “123”

objRegistry.SetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue

‘Get String value

strKeyPath = “Registry Test”

strValueName = “String Test”

objRegistry.GetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue

Wscript.Echo strValueName & ” = ” & strValue

‘Delete String value

strKeyPath = “Registry Test”

strValueName = “String Test”

objRegistry.DeleteValue HKEY_CURRENT_USER,strKeyPath,strValueName

‘Set Binary value

strKeyPath = “Registry Test”

strValueName = “Binary Test”

arrValues = Array(1,2,3,4,5,6,7,8,9,10)

objRegistry.SetBinaryValue HKEY_CURRENT_USER,strKeyPath,strValueName,arrValues

‘Get Binary value

strKeyPath = “Registry Test”

strValueName = “Binary Test”

objRegistry.GetBinaryValue HKEY_CURRENT_USER,strKeyPath,strValueName,arrValues

For Each strValue In arrValues

Wscript.Echo strValueName & ” = ” & strValue

Next

‘Delete Binary value

strKeyPath = “Registry Test”

strValueName = “Binary Test”

objRegistry.DeleteValue HKEY_CURRENT_USER,strKeyPath,strValueName

‘Set DWORD value

strKeyPath = “Registry Test”

strValueName = “DWORD Test”

intValue = 123

objRegistry.SetDWORDValue HKEY_CURRENT_USER,strKeyPath,strValueName,intValue

‘Get DWORD value

strKeyPath = “Registry Test”

strValueName = “DWORD Test”

objRegistry.GetDWORDValue HKEY_CURRENT_USER,strKeyPath,strValueName,intValue

Wscript.Echo strValueName & ” = ” & intValue

‘Delete DWORD value

strKeyPath = “Registry Test”

strValueName = “DWORD Test”

objRegistry.DeleteValue HKEY_CURRENT_USER,strKeyPath,strValueName

‘Set Multi-String value

strKeyPath = “Registry Test”

strValueName = “Multi-String Test”

arrValues = Array(“Test1″,”Test2″,”Test3”)

objRegistry.SetMultiStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,arrValues

‘Get Multi-String value

strKeyPath = “Registry Test”

strValueName = “Multi-String Test”

objRegistry.GetMultiStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,arrValues

For Each strValue In arrValues

Wscript.Echo strValueName & ” = ” & strValue

Next

‘Delete Multi-String value

strKeyPath = “Registry Test”

strValueName = “Multi-String Test”

objRegistry.DeleteValue HKEY_CURRENT_USER,strKeyPath,strValueName

‘Set Expandable String value

strKeyPath = “Registry Test”

strValueName = “Expandable String Test”

strValue = “123”

objRegistry.SetExpandedStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue

‘Get Expandable String value

strKeyPath = “Registry Test”

strValueName = “Expandable String Test”

objRegistry.GetExpandedStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue

Wscript.Echo strValueName & ” = ” & strValue

‘Delete Expandable String value

strKeyPath = “Registry Test”

strValueName = “Expandable String Test”

objRegistry.DeleteValue HKEY_CURRENT_USER,strKeyPath,strValueName

‘Delete Key

strKeyPath = “Registry Test”

objRegistry.DeleteKey HKEY_CURRENT_USER,strKeyPath

Set objRegistry = Nothing

Please note:

The above script write, read and delete values in the HKCU hive but can easily be modified to write to any of the registry hives.

The deletion function is the same no matter what value type is being deleted



Source by Shaun Vermaak

About the Author

admin

Leave a Reply

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