(view source code of readreg.vbs as plain text)
' Constants used in this scriptConst HKCR = &H80000000 'HKEY_CLASSES_ROOT
Const HKCU = &H80000001 'HKEY_CURRENT_USER
Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
Const HKU = &H80000003 'HKEY_USERS
Const HKCC = &H80000005 'HKEY_CURRENT_CONFIG
Const REG_SZ =1
Const REG_EXPAND_SZ =2
Const REG_BINARY =3
Const REG_DWORD =4
Const REG_MULTI_SZ =7
' Check and parse command line argumentsIf WScript.Arguments.Count <> 2 Then Syntax
section = WScript.Arguments(0)
key = UCase( WScript.Arguments(1) )
sectArray = Split( section, "\", 2 )
If UBound( sectArray ) < 1 Then Syntax
hive = UCase( sectArray(0) )
tree = sectArray(1)
' Convert specified hive to associated constantSelect Case hive
Case "HKEY_CLASSES_ROOT"
hkey = HKCRCase "HKEY_CURRENT_CONFIG"
hkey = HKCCCase "HKEY_CURRENT_USER"
hkey = HKCUCase "HKEY_LOCAL_MACHINE"
hkey = HKLMCase "HKEY_USERS"
hkey = HKUCase Else
Syntax
End Select
' Connect to the registry'Set WshShell = WScript.CreateObject( "WScript.Shell" )Set oReg = GetObject( "winmgmts:!root/default:StdRegProv" )
' Read and display the requested valueWScript.Echo vbCrLf & GetVal( hkey, tree, key )
'***********************************************************************'* Function Name: GetVal *'* Inputs: hive, tree, key *'* Example WScript.Echo GetVal( HKCU, "Environment", "Path" ) *'* Returns: Value in the format NAME=VALUE *'***********************************************************************Function GetVal( strHive, strTree, strKey )
' Read all keys and their values for the entire section into an arrayIf oReg.EnumValues( strHive, strTree, sKeys, iKeyType ) Then
GetVal = "-None-"
ElseFor Count = 0 to UBound( sKeys )
' Select the requested keyIf strKey = UCase( sKeys( Count ) ) Then
' Format the outputSelect Case iKeyType( Count )
Case REG_SZoReg.GetStringValue strHive, strTree, sKeys( Count ), sValue
GetVal = sKeys( Count ) & "=" & sValue
Case REG_EXPAND_SZoReg.GetExpandedStringValue strHive, strTree, sKeys( Count ), sValue
GetVal = sKeys( Count ) & "=" & sValue
Case REG_BINARYoReg.GetBinaryValue strHive, strTree, sKeys( Count ), aValue
GetVal = sKeys( Count ) & "=" & Join( aValue,"" )
Case REG_DWORDoReg.GetDWORDValue strHive, strTree, sKeys( Count ), lValue
GetVal = sKeys( Count ) & "=" & lValue
Case REG_MULTI_SZoReg.GetMultiStringValue strHive, strTree, sKeys( Count ), sValue
GetVal = sKeys( Count ) & "=" & Join( sValue,"" )
End Select
End If
NextGetVal = "[" & section & "]" & vbCrLf & GetVal
End If
End Function
Sub SyntaxstrMsg = vbCrLf & "ReadReg.vbs, Version 1.00" _
& vbCrLf & "Read a value from the registry" _
& vbCrLf _& vbCrLf & "Usage: READREG.VBS section key" _
& vbCrLf _& vbCrLf & "Where: " & Chr(34) & "section" _
& Chr(34) & " is the section name, without brackets" _
& vbCrLf & " " & Chr(34) & "key" _
& Chr(34) & " is the key whose value must be read" _
& vbCrLf & vbCrLf _
& "Arguments should be enclosed in quotes if they contain spaces" _
& vbCrLf _& vbCrLf & "Example:" & vbCrLf _
& "READREG.VBS " & Chr(34) & "HKEY_CURRENT_USER\Environment" _
& Chr(34) & " " & Chr(34) & "path" & Chr(34) _
& vbCrLf & "should return the user part of NT's PATH variable" _
& vbCrLf & vbCrLf & "Based on " & Chr(34) _
& "Registry functions Provided by the WMI StdRegProv class" _
& Chr(34) & vbCrLf & "by Andrew Mayberry" & vbCrLf _
& "http://cwashington.netreach.net/depo/view.asp?Index=560&ScriptType=vbscript" _
& vbCrLf _& vbCrLf & "Written by Rob van der Woude" _
& vbCrLf & "http://www.robvanderwoude.com"
WScript.Echo strMsg
WScript.Quit(1)
End Sub
page last modified: 2025-10-11; loaded in 0.0088 seconds