[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 21808
  • Last Modified:

VB Script to check if registry key exists

I'm modifying my login script and would like the vbscript to check if a registry key exists (looking for a particular version of office).  For example, I'd like to have it check if HKEY_CURRENT_USER\Software\Microsoft\Office\9.0\ exists.  If it does, then I want to perform some actions.
0
resourcepc
Asked:
resourcepc
1 Solution
 
basicinstinctCommented:
This works for me:

'START EXAMPLE CODE
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_CURRENT_USER = &H80000001


If keyExists("9.0", "Software\Microsoft\Office\", HKEY_CURRENT_USER) Then
    MsgBox("The key exists :)")
Else
      MsgBox("The key does not exist :(")
End If

Function keyExists(strKeyName, strKeyPath, regCategory)
     retVal = false
     strComputer = "."
     Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
     strComputer & "\root\default:StdRegProv")
   
     oReg.EnumKey regCategory, strKeyPath, arrSubKeys
     
     For Each subkey In arrSubKeys
          If strKeyName = subkey Then
               retVal = true
               Exit For
          End If
     Next
     keyExists = retVal
End Function

'END EXAMPLE CODE
0
 
MilanKMCommented:
Also see the following link
http://www.developerfusion.co.uk/forums/thread/107110/

Thanks
MilanKM
0
 
resourcepcAuthor Commented:
That worked!  Thanks.
0
 
apresenceCommented:
Be careful when using this - I'm not sure this really does what it proports to do... it returns False if a registry exists, but has no default value.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now