Skip to content

$cs.Reg_ExistKey

Checks whether a registry key exists. Never throws for a missing key — it simply returns $false.

$cs.Reg_ExistKey(string registryroot, string regkey)

Root hive: HKLM/HKEY_LOCAL_MACHINE, HKCU/HKEY_CURRENT_USER, or HKU/HKEY_USERS. Any other root, such as HKCR or HKCC, always returns $false. 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 check for, relative to registryroot.

Boolean — $true if the key exists, otherwise $false.

Under HKU, the path starts with the SID of a loaded user profile:

Terminal window
$userKey = "S-1-5-21-1004336348-1177238915-682003330-1001\Software\Adobe\Acrobat Reader\DC\AVGeneral"
if ($cs.Reg_ExistKey("HKU", $userKey))
{
$cs.Reg_SetDword("HKU", $userKey, "bisFirstLaunch", 0)
}

A PreInstall check to see whether the application is already installed, based on the presence of its registry footprint, and skip a redundant install.

Terminal window
if ($cs.Reg_ExistKey('HKLM', 'SOFTWARE\Contoso\AppSuite'))
{
Exit-PSScript -exitcode 3330 -exitmessage "AppSuite is already installed, skipping installation"
}

Skipping cleanup work during Uninstall when the key was never created in the first place, avoiding unnecessary log noise.

Terminal window
if ($cs.Reg_ExistKey('HKLM', 'SOFTWARE\Contoso\AppSuite\Features\Reporting'))
{
$cs.Reg_DelTree('HKLM', 'SOFTWARE\Contoso\AppSuite\Features\Reporting')
}
else
{
$cs.Job_WriteLog("Reporting feature key not present, nothing to clean up")
}

$cs.Reg_CreateKey $cs.Reg_DelTree $cs.Reg_ExistVariable