WMI Tasks: Registry by MSFT

WMI tasks for the registry create and modify registry keys and values. For other examples, see the TechNet ScriptCenter at http://www.microsoft.com/technet.

The script examples shown in this topic obtain data only from the local computer. For more information about how to use the script to obtain data from remote computers, see Connecting to WMI on a Remote Computer.

The following procedure describes how to run a script.

clip_image001To run a script

  1. Copy the code and save it in a file with a .vbs extension. Ensure that your text editor does not add a .txt extension to the file.
  2. Open a command prompt window and navigate to the directory where you saved the file.
  3. Type cscript scriptfile.vbs at the command prompt.

Note  By default, cscript displays the output of a script in the command prompt window. Because WMI scripts can produce large amounts of output, you might want to redirect the output to a file. Type cscript scriptfile.vbs > outfile.txt at the command prompt to redirect the output of the filename.vbs script to outfile.txt.

The following table lists script examples that can be used to obtain various types of data from the local computer.

How do I…

WMI classes or methods

…read registry key values using WMI?

Use the StdRegProv class, located in rootdefault namespace. You cannot get any instances of this class because the System Registry Provider is a method and event provider only. However, you can get registry data through methods such as EnumKey or EnumValue. The Win32_Registry, located in rootcimv2 namespace, gets data about the registry as a whole, such as how large it is.

const HKEY_CURRENT_USER = &H80000001
strComputer = "."
Set oReg=GetObject( _
   "winmgmts:{impersonationLevel=impersonate}!\" &_
    strComputer & "rootdefault:StdRegProv")
strKeyPath = "Console"
strValueName = "HistoryBufferSize"
oReg.GetDWORDValue _
   HKEY_CURRENT_USER,strKeyPath,strValueName,dwValue
WScript.Echo "Current History Buffer Size: " & dwValue

…create a new registry key?

Use the StdRegProv class, located in rootdefault namespace, and the CreateKey method.

const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set objReg=GetObject( _
    "winmgmts:{impersonationLevel=impersonate}!\" & _
   strComputer & "rootdefault:StdRegProv")
 
strKeyPath = "SOFTWARENewKey"
objReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath
WScript.Echo "Created registry key HKEY_LOCAL_MACHINESOFTWARENewKey"

…create a new registry value under a key?

Use the StdRegProv class, located in the rootdefault namespace, and the CreateKey method. Then use one of the Set methods, depending on what registry datatype the value is, such as the SetDWORDValue. The Set methods create a value if it does not already exist. For more information, see Mapping a Registry Data Type to a WMI Data Type.

Const HKEY_LOCAL_MACHINE = &H80000002
strKeyPath = "SOFTWARENewKey"
strComputer = "."
Set objReg=GetObject( _
    "winmgmts:{impersonationLevel=impersonate}!\" & _ 
    strComputer & "rootdefault:StdRegProv")
strValueName = "Example_Expanded_String_Value"
strValue = "%PATHEXT%"
objReg.SetExpandedStringValue HKEY_LOCAL_MACHINE,_
    strKeyPath,strValueName,strValue
WScript.Echo "Example expanded_String_Value at " _
    & "HKEY_LOCAL_MACHINESOFTWARENewKey"

…avoid getting an Invalid Class error when trying to write a script to read the registry?

Use the rootdefault namespace when accessing the StdRegProv class. StdRegProv is not part of the cimv2 namespace, which is why an "Invalid Class" error is generated if you try to connect to rootcimv2:StdRegProv.

Const HKEY_CURRENT_USER = &H80000001
strComputer = "."
Set oReg=GetObject( _
   "winmgmts:{impersonationLevel=impersonate}!\" & _
   strComputer & "rootdefault:StdRegProv") 
strKeyPath = "Console"
strValueName = "HistoryBufferSize"
oReg.GetDWORDValue _
    HKEY_CURRENT_USER, strKeyPath, strValueName, dwValue
Wscript.Echo "Current History Buffer Size: " & dwValue

…check security on a specific registry key?

Use the StdRegProv class, located in rootdefault namespace and the CheckAccess method. You can only check the access rights for the current user that is running the script or application. You cannot check the access rights for another specified user.

…read and write binary registry values?

Use the StdRegProv class, located in rootdefault namespace and the GetBinaryValue and SetBinaryValue methods. Registry values that appear in the regedt32 utility as a series of byte hexadecimal values are in the REG_BINARY data format. For more information, see Mapping a Registry Data Type to a WMI Data Type. The following VBScript code example creates a new key with a binary value. The binary value is supplied in the iValues byte array specified in Hex.

const HKEY_LOCAL_MACHINE = &H80000002
strKeyPath = "SOFTWARENewKey"
strComputer = "."
iValues = Array(&H01,&Ha2,&H10)
Set oReg=GetObject( _ 
    "winmgmts:{impersonationLevel=impersonate}!\" & _
   strComputer & "rootdefault:StdRegProv")
oReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath
strKeyPath = "SOFTWARENewKey"
BinaryValueName = "Example Binary Value"
 
oReg.SetBinaryValue HKEY_LOCAL_MACHINE,strKeyPath,_
    BinaryValueName,iValues

The following script reads the binary value.

const HKEY_LOCAL_MACHINE = &H80000002 
strKeyPath = "SOFTWARENewKey"
strValueName = "Example Binary Value"
strComputer = "."
dim iValues(3)
Set oReg=GetObject( _
   "winmgmts:{impersonationLevel=impersonate}!\" & _ 
    strComputer & "rootdefault:StdRegProv")
oReg.GetBinaryValue HKEY_LOCAL_MACHINE,strKeyPath,_
    strValueName,iValues
For i = lBound(iValues) to uBound(iValues)
Wscript.Echo iValues(i)
Next

…read and write registry values that contain multiple strings?

Use the StdRegProv class, located in rootdefault namespace and the GetMultiStringValue and SetMultiStringValue methods. Registry keys that appear in the regedt32 utility as a series of strings separated by spaces are in the REG_MULTI_SZ data format. For more information, see Mapping a Registry Data Type to a WMI Data Type. The following VBScript code example creates a new key and a new multistring value.

const HKEY_LOCAL_MACHINE = &H80000002
strKeyPath = "SOFTWARENewKey"
MultValueName = "Example Multistring Value"
strComputer = "."
iValues = Array("string1", "string2")
Set oReg=GetObject( _
    "winmgmts:{impersonationLevel=impersonate}!\" & _
   strComputer & "rootdefault:StdRegProv")
oReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath
oReg.SetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath,_
    MultValueName,iValues

The following script reads the multistring value.

const HKEY_LOCAL_MACHINE = &H80000002
strKeyPath = "SOFTWARENewKey"
strComputer = "."
iValues = Array("string1", "string2")
Set oReg=GetObject( _
    "winmgmts:{impersonationLevel=impersonate}!\" & _
   strComputer & "rootdefault:StdRegProv")
MultValueName = "Example Multistring Value"
oReg.GetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath,_
    MultValueName,iValues
For Each strValue In iValues
WScript.echo strValue
Next
See Also

WMI Tasks for Scripts and Applications

WMI C++ Application Examples

TechNet ScriptCenter

Modifying the System Registry

StdRegProv

Leave a Reply

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