Spread the love

How to Work with Windows Registry with WSH?

Spread the love

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:



  • 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.
READ  What is Windows Script Host (WSH)?

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:

HKEY_CLASSES_ROOTHKCRKeeps all information of the windows file association.
HKEY_CURRENT_USERHKCUKeeps all information of the currently logged-in user.
HKEY_LOCAL_MACHINEHKLMKeeps all global computer-related settings.
HKEY_USERHKEY_USERSKeeps information on all users.
HKEY_CURRENT_CONFIGHKEY_CURRENT_CONFIGKeeps information of computer current configuration
READ  Difference between Dim and Redim Statement In VBScript,UFT,QTP

How Windows information is stored?

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


It includes:

  • SAM

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-


For Desktop the hierarchy of the key is as follows:

Control Panel\

Windows Registry Datatypes


REG_BINARYKeeps binary value
REG_DWORDKeeps a hexadecimal DWORD value -a 32-bit integer
REG_EXPAND_SZKeeps an expandable String like %COMPUTERNAME%
REG_MULTI_SZKeeps multiple Strings.
REG_SZKeeps a String-most of the cases
REG_NONEKeeps an undefined data type
REG_DWORD_BIG_ENDIANKeeps a hexadecimal DWORD value -a 32-bit integer in Big Endian format
REG_LINKPreserved my Microsoft
REG_DWORD_LITTLE_ENDIANKeeps a hexadecimal DWORD value -a 32-bit integer in Little Endian format
REG_QWORDKeeps a 64-bit integer
REG_QWORD_LITTLE_ENDIANKeeps a 64-bit integer in Little Endian format
REG_RESOURCE_LISTKeeps a list of device drivers


  • 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_SZA StringA String
REG_MULTI_SZAn array of StringA VB array of String
REG_DWORDA NumberAn Integer
REG_BINARYA binaryA VB array of integer
REG_EXPAND_SZAn expandable String like %windr%A String
READ  Signed and Unsigned ActiveX Controls

For any other type this method returns DISP_E_TYPEMISMATCH error.


Option Explicit
Dim myValue,WSHObject
Set WSHObject=WScript.CreateObject("Wscript.Shell")

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
READ  How To Convert JSON File To XML format In UFT in VBScript way


  • 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.


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:

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.

READ  Tips and Tricks of Error Handling in VBScript or in UFT


WSHShell.RegDelete "key or value"


Set WSHShell=WScript.CreateObject("Wscript.Shell")
WSHShell.RegDelete "HKCU\TestKey\"

By providing a fully qualified key name followed by a backslash, we can delete an existing key. It will delete all subkeys and values. So we can specify a key name by ending with a final backslash or we can leave it out to specify a value name.

Fully qualified key names and values are prefixed with a root key. We can use abbreviated versions of the root ky names with the RegDelete() method.

Five possible root keys are as follows:

HKEY_CLASSES_ROOTHKCRKeeps all information of the windows file association.
HKEY_CURRENT_USERHKCUKeeps all information of the currently logged-in user.
HKEY_LOCAL_MACHINEHKLMKeeps all global computer-related settings.
HKEY_USERHKEY_USERSKeeps information on all users.
HKEY_CURRENT_CONFIGHKEY_CURRENT_CONFIGKeeps information of computer current configuration


How to Handle errors while working with Registry?

There could be a scenario, if we specify a registry key or value that does not exist, we may get error prompt. To prevent such error prompt and continue our script execution, we can use On Error Resume Next statement.

How to Set Background using VBScript?

We can take the help of the registry to set background.

WSHShell.RegWrite "HKCU\Control Panel\Desktop\wallpaper","path_of_the_new_image_file"

to disable tile option

WSHShell.RegWrite "HKCU\Control Panel\Desktop\TileWallpaper",0

To set a background color

WSHshell.RegWrite "HKCU\Control Panel\Colors\Background","255 255 255"

How to set Screensaver in VbScript?

For Screensaver, we need to alter four screensavers related registry values. They are situated at “HKCU\Control Panel\Desktop subkey”

READ  Control Flow in VBScript or UFT

The alternation will be as follows

  • Screensaveractive- we need to set to 1
  • ScreensaverIsSecure- We need to set to 1
  • SreensaverTimeout- we need to set to a value in seconds
  • SetupScreensaver – by setting SCRNSAVE.EXE to %SystemRoot%&System32\ssstart.scr
Activate Windows Screensaver
WSHShell.RegWrite "HKCU\Control Panel\Desktop\ScreensaverActive",1
Enable Password protection
WSHShell.RegWrite "HKCU\Control Panel\Desktop\ScreensaverIsSecure",1
Setup a delay in activating the Screensaver
WSHShell.RegWrite "HKCU\Control Panel\Desktop\ScreensaverTimeout",timeout_in_Seconds
Enable specific Screensaver
WSHShell.RegWrite "HKCU\Control Panel\Desktop\SCRNSAVE.EXE",%SystemRoot%&"System32\ssstart.scr"

How to force logoff a user?

The screensaver, desktop background, color when changed from the code, the user needs to log off and log back in. After all, changes have done via coding admin script must log off the active user.

The code to force logoff is as below:

WSHShell.Run %SystemRoot%&"System32\logoff.exe"


Spread the love
Animesh Chatterjeehttps://techtravelhub.com/
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

Related Articles

Learn File System Object(FSO) in UFT or QTP or VBScript Free

This post will cover the File System Object(FSO) in UFT. File...



Please enter your comment!
Please enter your name here

Recent Posts

Super Keyword in Java Simplified

Super Keyword Super Keyword in Java Simplified: Java defines a special...

Learn Object Repository in UFT in An Efficient and Fast Manner

Object Repository in UFT The post will cover the following...

Learn What Primary Tools For Test Automation

Primary Tools For Test Automation According to the Gartner report...


Spread the love