EnumKey method of the StdRegProv class

The EnumKey method enumerates the subkeys for a path. See Obtaining Registry Data for general information on accessing the registry through WMI.

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


uint32 EnumKey(
  [in]  uint32 hDefKey = HKEY_LOCAL_MACHINE,
  [in]  string sSubKeyName,
  [out] string sNames[]


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 path that contains the subkeys to be enumerated.

sNames [out]

An array of subkey strings.

Return value

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.


In some instances, useful configuration information is stored as the names of a set of subkeys. In the following sample script, for instance, the subkey names represent the services on a computer. In a case such as this, simply listing the names of the subkeys provides useful information.

The EnumKey method enables you to return the subkeys of a registry key or subkey. Note that the EnumKey method returns only the immediate subkeys of a key or subkey; it does not return any subkeys that might be contained within those top-level subkeys. To return all subkeys, you need to use a recursive function.


The following PowerShell code example shows how to use the EnumKey method to enumerate the services listed as subkeys in the registry key:


$HKEY_LOCAL_MACHINE = 2147483650
$strKeyPath = "SYSTEM\CurrentControlSet\Services"

$objReg = [WMIClass]"root\default:StdRegProv"

$arrSubKeys = $objReg.EnumKey($HKEY_LOCAL_MACHINE, $strKeyPath)
foreach ($subKey in ($arrSubKeys.sNames))


Minimum supported client
Windows Vista
Minimum supported server
Windows Server 2008

See also


Modifying the System Registry

WMI Tasks: Registry