How to Work with Windows Registry with WSH?

0
105
Spread the love
  • 28
  • 34
  • 56
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    165
    Shares

WSH registry
WSH registry

How to Work with Windows Registry with WSH?

What is a registry?

The window registry is the built-in repository or database for windows configurational information(for all Microsoft Operating systems). By changing the contents of the registry we can manage different computer operations.

VbScript can work smoothly with the windows registry. We can perform the following tasks with VbScript and registry.

  1. User configuration settings and profile information settings.
  2. Windows operating system settings.
  3. Necessary software configuration settings.
  4. Required Hardware settings.
  5. Required settings for windows services and software device drivers.

How data is stored in the registry?

Data inside the registry are always stored as values. It also represents the name of an element against which data is associated. Values can be attached in many ways to files that keep data on the Windows file system. Values are organized in the keys that acts as a container. (aka folder).

A registry key is a container that stores values or other registry keys. Data is stored in the registry in the following manner:

key:keyType:value

Where

  • a key is the fully qualified name of the registry
  • a keyType is the type of data that the key stores. It may also store different types of data.
  • a value is an actual data that needs to be stored.

How Windows Registry is organized?

Windows registry is a logical organization just like a tree structure. It is having five root keys. They are as follows:

Key ShortName Details
HKEY_CLASSES_ROOT HKCR Keeps all information of the windows file association.
HKEY_CURRENT_USER HKCU Keeps all information of the currently logged-in user.
HKEY_LOCAL_MACHINE HKLM Keeps all global computer-related settings.
HKEY_USER HKEY_USERS Keeps information on all users.
HKEY_CURRENT_CONFIG HKEY_CURRENT_CONFIG Keeps information of computer current configuration

How Windows information is stored?

Windows registry stored different information in different files: It can be found in the below folder-

 %System-root%\System32\Config

It includes:

  • DEFAULT
  • SAM
  • SECURITY
  • SOFTWARE
  • SYSTEM
  • USERDIFF

Apart from these, there are user profiles. they can be found in the Document and Settings folder.

Registry data is made up of keys and values. A key can contain several other keys and values. Data stored in the registry is in the following format-

Key:key-type:value

For Desktop the hierarchy of the key is as follows:

HKCU\
Control Panel\
Desktop\
Wallpaper

Windows Registry Datatypes

 

Type Details
REG_BINARY Keeps binary value
REG_DWORD Keeps a hexadecimal DWORD value -a 32-bit integer
REG_EXPAND_SZ Keeps an expandable String like %COMPUTERNAME%
REG_MULTI_SZ Keeps multiple Strings.
REG_SZ Keeps a String-most of the cases
REG_NONE Keeps an undefined data type
REG_DWORD_BIG_ENDIAN Keeps a hexadecimal DWORD value -a 32-bit integer in Big Endian format
REG_LINK Preserved my Microsoft
REG_DWORD_LITTLE_ENDIAN Keeps a hexadecimal DWORD value -a 32-bit integer in Little Endian format
REG_QWORD Keeps a 64-bit integer
REG_QWORD_LITTLE_ENDIAN Keeps a 64-bit integer in Little Endian format
REG_RESOURCE_LIST Keeps a list of device drivers

Note:

  • Registry values can be named or unnamed.
  • A named value is an assigned one and can be referenced.
  • An unnamed value is a default one.

How to Read Registry key and Value in VbScript or UFT?

We need to use the RegRead() method for reading a registry. It enables us to read a key/value from the registry.

myValue=WSHObject.RegRead(key or Value)

for reading a key or value from the registry. Using this method we can retrieve the following types of data:

  • REG_SZ
  • REG_MULTI_SZ
  • REG_DWORD
  • REG_BINARY
  • REG_EXPAND_SZ
Type Input Returns
REG_SZ A String A String
REG_MULTI_SZ An array of String A VB array of String
REG_DWORD A Number An Integer
REG_BINARY A binary A VB array of integer
REG_EXPAND_SZ An expandable String like %windr% A String

For any other type this method returns DISP_E_TYPEMISMATCH error.

Example

Option Explicit
Dim myValue,WSHObject
Set WSHObject=WScript.CreateObject("Wscript.Shell")
myValue=WSHObject.RegRead("HKCU\TestKey\FileName")

Note on RegRead() method

While performing the RegRead() method, if we specify the name of the registry key instead of value, the method then returns the default value assigned to the registry key.

A value entry consists of three parts

  • Name
  • Data types
  • Value

How to write a Registry key and Value in VbScript or UFT?

We need to use RegWrite() method for this. Using RegWrite() ,we can write a registry key and value. RegWrite() is defined under WSHShell.

RegWrite() method provides us the ability to do the following tasks:

  • Create a new registry key.
  • Add value to the registry key and assign data to it.
  • Modify an existing value assignment.

Below is the datatype for which we can create a key and value:

  • REG_SZ
  • REG_DWORD
  • REG_BINARY
  • REG_EXPAND_SZ

Syntax:

WSHShell.RegWrite(keyorValue,Data,Datatype)
  • KeyorValue is the key or value that needs to be created or modified.
  • Data is the actual value. It is either the new key or the new value to be added to an existing key or the data to be assigned to an existing value.
  • The data type is an optional parameter that signifies a String value. It further signifies the data type associated with a value.

Example:

Dim WSHShell
Set WSHShell=WScript.CreateObject("Wscript.Shell")
WSHShell.RegWrite "HKCU\TestKey\FileName","debug.log","REG_SZ"

Note on RegWrite() method

While specifying a registry key, the value of the name should end with a backslash character. But while specifying a registry value the backslash is omitted.

RegWrite() method automatically converts the value parameter to a String or integer. And DataType determines it’s data type.

The conversion table is as follows:

Conversion Datatype
String REG_SZ
Integer REG_DWORD
Integer REG_BINARY
String REG_EXPAND_SZ
Note RegWrite() method does not support REG_MULTI_SZ type and RegWrite() method can write a maximum one DWORD to REG_BINARY value. However, RegWrite() method does not support larger values. As I mentioned earlier, fully qualified key names and value names need to be prefixed with a root key. We can also use the abbreviated value.
To modify an existing key:
Set WSHShell=WScript.CreateObject("Wscript.Shell")
WSHShell.RegWrite "HKCU\TestKey\" "Disabled"

How to Delete a Registry key in VBScript or UFT?

RegDelete() method enables us to delete a registry key or value. It is defined under the WSHShell object.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 28
  • 34
  • 56
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    165
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here