How to Work with Windows Registry with WSH?

WSH registry
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.

Ads code goes here
  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:

KeyShortNameDetails
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 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

 

TypeDetails
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

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
TypeInputReturns
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

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:

ConversionDatatype
StringREG_SZ
IntegerREG_DWORD
IntegerREG_BINARY
StringREG_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.

Syntax:

WSHShell.RegDelete "key or value"

Example

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:

KeyShortNameDetails
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”

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"

 

Share and Enjoy !

2 thoughts on “How to Work with Windows Registry with WSH?”

  1. Pingback: What Is Windows Script Host (WSH)? - Tech Travel Hub

  2. Pingback: Risks Of Using Operating Systems In Dual Boot And Solutions - Tech Travel Hub

Leave a Comment

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