How to create Registry key if it does not exists?

In one of my previous post, I posted about how to read and modify the Registry Settings using VBScript/QTP. But, this only works if Registry Key/SubKey (that you intends to read/modify) already exists.

In this post, first we will look at the small overview of what a Registry is and then we will look into, how to create Registry Key/SubKey and set the value if it does not exists.

Registry Overview:

The Windows Registry is a hierarchical database that stores configuration settings and options on Microsoft Windows operating systems. It contains settings for low-level operating system components and for applications running on the platform that have opted to use the registry. The registry contains two basic elements: keys and values. Registry keys are container objects similar to folders. Registry values are non-container objects similar to files. Keys may contain values or further keys (aka subkey). The hierarchy of registry keys can only be accessed from a known root key handle. There are seven predefined root keys, traditionally named according to their constant handles defined in the Win32 API:

  •     HKEY_CURRENT_CONFIG (only in Windows 9x/Me and NT-based Windows versions)
  •     HKEY_USERS
  •     HKEY_PERFORMANCE_DATA (only in NT-based Windows versions (but invisible))
  •     HKEY_DYN_DATA (only in Windows 9x/Me, and visible in Windows Registry Editor)

How to create Registry Key/SubKey:

There are 2 methods to do this that I am going to post it here:

1) Using VBScript:

Dim objShell, RegLocate

'create windows shell object

Set objshell =CreateObject("WScript.Shell")

'Suppose New Windows and PopupMgr SubKey does not exists in the below Registry Path
RegLocate ="HKCU\Software\Microsoft\Internet Explorer\New Windows\PopupMgr"

'Reading Registry will throw an error if path doesn't exists

On Error Resume Next
reg_value=objshell.RegRead (RegLocate)
On Error Goto 0

'check for the error and then write the value to Registry
If err_number <> 0 Then
objShell.RegWrite RegLocate,"No","REG_SZ"
End If

Set objshell = Nothing

2) Using WMI:

Using methods of StdRegProv Class. You can find all the methods on the following link:

Here, I am using GetDWordValue, CreateKey and SetDWORDValue methods of StdRegProv class

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

strComputer = "."

Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

strKeyPath = "Software\Microsoft\Internet Explorer\New Windows"
strValueName = "PopupMgr"

'storing the value of strValueName from strKeyPath in dwValue
objRegistry.GetDWORDValue HKEY_CURRENT_USER,strKeyPath,strValueName,dwValue

'checking if Registry Key Exists
If cstr(IsNull(dwValue))="True" or cstr(objRegistry.EnumKey(HKEY_CURRENT_USER,strKeyPath,arrSubKeys)) <> "0" Then

msgbox "The registry key does not exist."

'setting 1 for strValueName in strKeyPath
objRegistry.SetDWORDValue HKEY_CURRENT_USER,strKeyPath,strValueName,1

objRegistry.GetDWORDValue HKEY_CURRENT_USER,strKeyPath,strValueName,dwValue

'checking if Registry Key still exists
If cstr(IsNull(dwValue))="True" Then

'If Registry Key still doesn't exists, then it means strKeyPath does not exist, need to create key first
objRegistry.CreateKey HKEY_CURRENT_USER,strKeyPath
objRegistry.SetDWORDValue HKEY_CURRENT_USER,strKeyPath,strValueName,1
objRegistry.GetDWORDValue HKEY_CURRENT_USER,strKeyPath,strValueName,dwValue
msgbox dwValue
msgbox "The registry key exists."
End If
msgbox "The registry key exists."
End If

Tip: If RegLocate terminates with “\”, then it is treated as Key, otherwise it is treated as Value

Tagged with: , , , , , , ,
Posted in Awesum Stuff
2 comments on “How to create Registry key if it does not exists?
  1. […] I have already talked about How to read/modify and create Registry Settings in previous posts, today I will be talking about how to delete Registry Keys. Again, there are 2 […]

  2. […] Try replacing the quotation marks – the ones on the page don't look quite right Found this link : How to create Registry key if it does not exists? | Learn2Automate Basically replaced the constants with the ones from the above article so : […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog Stats
  • 168,130 hits

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 796 other followers

%d bloggers like this: