How to Determine if a Registry Key Exists in VBScript

Written by kevin lee
  • Share
  • Tweet
  • Share
  • Email

Peering into the system registry gives you a glimpse into your operating system's critical settings. These settings, called keys, control the way Windows works. Developers use VBScript and other tools to examine and modify the keys. If you need to verify that a specific key exists before performing a registry operation, you can create a short VBScript function that queries the registry.

Skill level:
Moderately Easy

Other People Are Reading


  1. 1

    Open Notepad or a text editor and paste the following VBScript code into a new document:

    Option Explicit

    Dim key Name

    key Name = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Wallpapers\"

    If KeyFound(keyName) = True Then

    Wscript.echo "Key exists"

    Else Wscript.echo "Key does not exist"

    End If

    The variable "keyName" holds the name of the registry key for which you need to search. Replace "Name" with a key of your choice. The next line of code passes that name to a function called "KeyFound." That function checks to see if the key exists.

  2. 2

    Add this code below the code shown in the last step:

    function KeyFound(byval key Name)

    key Found = false

    Dim right Character

    right Character = right(key Name, 1)

    if right Character <> "\" Then key Name = key Name & "\"

    on error resume next

    createobject("").reg Read trim(keyName)

    select case err

    case 0: key Found = true

    end select

    end function

    This is the "KeyFound" function. It adds a slash to the end of the key if none exists and executes the "regRead" method. The "on error" statement allows the program to continue running even if the "regRead" method fails to locate the desired registry key. If the method finds a key, the code sets the value of "keyFound" to true.

  3. 3

    Save the file with the VBS extension. For instance, if you want to name it "CheckKeys," save it as "CheckKeys.vbs."

  4. 4

    Launch Windows Explorer and locate the file. Double-click that file to run the program. It executes and displays "Key exists" if it finds the key or "Key does not exist" if it doesn't.

Tips and warnings

  • The essential part of this program is the "KeyFound" function. Simply call it from any other VBScript program, and pass the "KeyFound" function the name of any key. The function returns "True" or "False," depending on whether or not the key exists.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.