Skip to content

$cs.Reg_EnumKey

Returns the names of the subkeys found directly under a registry key. If the path doesn’t exist, $false is returned rather than throwing. Check the key with $cs.Reg_ExistKey first, so you don’t loop over $false by mistake.

$cs.Reg_EnumKey(string regroot, string regpath, bool mustexist)

Root hive: HKLM/HKEY_LOCAL_MACHINE, HKCU/HKEY_CURRENT_USER, or HKU/HKEY_USERS. PowerPacks run as SYSTEM, so HKCU is the SYSTEM account’s hive, not the logged-on user’s. To change user settings, see Invoke-RunAsLoggedOnUser.

Registry key path to enumerate, relative to regroot.

Accepted for signature compatibility but currently has no effect on behavior — the function does not throw or change behavior based on this value.

Array of subkey names (string[]). Empty array if the key exists but has no subkeys. $false if the key doesn’t exist or can’t be opened. Empty string "" if regroot isn’t HKLM, HKCU or HKU.

Terminal window
$regkeys = $cs.Reg_EnumKey('HKLM', 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall', $true)
foreach ($item in $regkeys) {
$cs.Job_WriteLog("Regkey found: $item")
}

Enumerating installed version subkeys under a vendor key during Uninstall, so old versions can be identified and removed one by one.

Terminal window
if ($cs.Reg_ExistKey('HKLM', 'SOFTWARE\Contoso\AppSuite\Versions')) {
$versions = $cs.Reg_EnumKey('HKLM', 'SOFTWARE\Contoso\AppSuite\Versions', $true)
foreach ($version in $versions) {
if ($version -ne '5.2.0') {
$cs.Job_WriteLog("Removing outdated version key: $version")
$cs.Reg_DelTree('HKLM', "SOFTWARE\Contoso\AppSuite\Versions\$version")
}
}
}

Checking how many components are registered under a key before deciding whether the whole feature tree should be torn down.

Terminal window
$components = $cs.Reg_EnumKey('HKLM', 'SOFTWARE\Contoso\AppSuite\Components', $true)
if ($components -is [array] -and $components.Count -eq 0) {
$cs.Job_WriteLog("No components left, removing AppSuite root key")
$cs.Reg_DelTree('HKLM', 'SOFTWARE\Contoso\AppSuite')
}

$cs.Reg_ExistKey $cs.Reg_GetString