SetStringValue method of the StdRegProv class

The SetStringValue method sets the data value for a named value whose data type is REG_SZ.

This topic uses Managed Object Format (MOF) syntax. For more information about using this method, see Calling a Method.


uint32 SetStringValue(
  [in] uint32 hDefKey = HKEY_LOCAL_MACHINE,
  [in] string sSubKeyName,
  [in] string sValueName,
  [in] string sValue = 


hDefKey [in]

A registry tree, also known as a hive, that contains the sSubKeyName path. The default value is HKEY_LOCAL_MACHINE.

The following trees are defined in Winreg.h.

HKEY_CLASSES_ROOT (2147483648)

HKEY_CURRENT_USER (2147483649)


HKEY_USERS (2147483651)


sSubKeyName [in]

A key that contains the named value to be set.

sValueName [in]

A named value whose data value you are setting. You can specify an existing named value (update) or a new named value (create). Specify an empty string to set the data value for the default named value.

sValue [in]

A data value.

Return value

If the key does not exist, then the call to the SetStringValue method fails.

In C++, the method returns a uint32 value that is 0 (zero) if successful. If the function fails, the return value is a nonzero error code that is defined in WinError.h. In C++, use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag to get a generic description of the error. You can also look up return values under the WMI Error Constants.

In scripting or Visual Basic, the method returns an integer value that is 0 (zero) if successful. If the function fails, the return value is a nonzero error code that you can look up in WbemErrorEnum.


Changing the values of registry entries is a common registry management task. The most challenging aspect of that task is determining which entries you need to change, and to which values they must be changed. After you have this information, you must choose the appropriate Registry Provider methods for making the change.

To change a string value (REG_SZ), you use the SetStringValue method; to change a numeric value (REG_DWORD), you use the SetDWORDValue method. Each of these methods takes four parameters:

  • A constant that specifies the subtree in which the value being changed is located.
  • The path to the subkey in which the value is located.
  • The name of the entry with the value to be changed.
  • The new value.

These parameters are similar to the parameters used to read registry entries. The only difference is that when registry entries are read, the fourth parameter represents the value read from the registry; when registry entries are configured, the fourth parameter represents the new value being written to the registry.

Be certain to investigate whether other methods of configuration are available before deciding to script direct changes to the registry.

Regardless of where you obtain your information, always back up the registry before modifying it in any way.


The Change Computer Name VBScript sample changes the local computer name.

The following VBScript code example shows how to call the SetStringValue method to write a string value to a key. The script first creates the key.

const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"&_ 
    strComputer & "\root\default:StdRegProv")

strKeyPath = "SOFTWARE\MyKey\"
KeyPath = "Software\MyKey"
strValueName = "String Value Name"
strValue = "string value"

' Create key to use
Return = objReg.CreateKey(HKEY_LOCAL_MACHINE, KeyPath)
If (Return = 0) And (Err.Number = 0) Then   
    Wscript.Echo "HKEY_LOCAL_MACHINE\Software\MyKey created"

    ' write string value to key    
    Return = objReg.SetStringValue( _
    If (Return = 0) And (Err.Number = 0) Then 
        Wscript.Echo "HKEY_LOCAL_MACHINE\Software\MyKey" & _ 
            " contains 'string value'"
        Wscript.Echo "SetStringValue failed. Error = " & Err.Number
    End If
    Wscript.Echo "CreateKey failed. Error = " & Err.Number
End If


Minimum supported client
Windows Vista
Minimum supported server
Windows Server 2008

See also


Modifying the System Registry

WMI Tasks: Registry