Skip to content

$cs.Reg_ExistVariable

Checks whether a named value exists under a registry key. Returns $false (not an exception) if either the key or the value is missing.

$cs.Reg_ExistVariable(string registryroot, string regkey, string regvariable)

Root hive: HKLM/HKEY_LOCAL_MACHINE, HKCU/HKEY_CURRENT_USER, or HKU/HKEY_USERS.

Registry key path containing the value, relative to registryroot.

Name of the value to check for.

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

Terminal window
if ($cs.Reg_ExistVariable('HKLM', 'SOFTWARE\Microsoft\Windows\CurrentVersion\Run', 'Everything'))
{
$cs.Reg_DeleteVariable('HKLM', 'SOFTWARE\Microsoft\Windows\CurrentVersion\Run', 'Everything')
}

Checking that a specific setting exists before reading it with Reg_GetString, to distinguish “not configured” from “configured as empty string”.

Terminal window
if ($cs.Reg_ExistVariable('HKLM', 'SOFTWARE\Contoso\AppSuite', 'InstallPath'))
{
$installPath = $cs.Reg_GetString('HKLM', 'SOFTWARE\Contoso\AppSuite', 'InstallPath')
$cs.Job_WriteLog("AppSuite installed at: $installPath")
}
else
{
$cs.Job_WriteLog("InstallPath setting not found")
}

Verifying a licensing value is present before proceeding with a license-dependent install step.

Terminal window
if (-not $cs.Reg_ExistVariable('HKLM', 'SOFTWARE\Contoso\AppSuite\Licensing', 'LicenseKey'))
{
Exit-PSScript -exitcode 1 -exitmessage "No license key configured, aborting install"
}

$cs.Reg_DeleteVariable $cs.Reg_GetString $cs.Reg_ExistKey