Skip to content

$cs.Reg_GetExpandString

Reads a REG_EXPAND_SZ registry value. Environment variables in the stored value, such as %ProgramFiles% or %SystemRoot%, are expanded before the value is returned. Returns an empty string if the value isn’t found.

The variables are expanded with the environment of the account that runs the PowerPack, which is SYSTEM. User-specific variables therefore point to the SYSTEM profile — for example, %USERPROFILE% becomes C:\Windows\system32\config\systemprofile. If you need the raw, unexpanded string, read it with PowerShell instead: (Get-Item 'HKLM:\SOFTWARE\Contoso\AppSuite').GetValue('DisplayIcon', $null, 'DoNotExpandEnvironmentNames').

$cs.Reg_GetExpandString(string registryroot, string regkey, string regvalue)

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 containing the value, relative to registryroot.

Name of the value to retrieve. Pass "" for the key’s default value.

String — the value with environment variables expanded. Empty string "" if the value isn’t found.

Terminal window
$defaultPath = $cs.Reg_GetExpandString("HKU", ".DEFAULT\Environment", "Path")
$cs.Job_WriteLog("PATH of the default profile: $defaultPath")

Reading an uninstall string that contains a %ProgramFiles% reference. The returned value already contains the full path, so it can be used directly.

Terminal window
$uninstallString = $cs.Reg_GetExpandString('HKLM', 'SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Contoso AppSuite', 'UninstallString')
$cs.Job_WriteLog("Resolved uninstall command: $uninstallString")

Reading a DisplayIcon value that references %SystemRoot%, to confirm the target file actually exists on disk.

Terminal window
$iconPath = $cs.Reg_GetExpandString('HKLM', 'SOFTWARE\Contoso\AppSuite', 'DisplayIcon')
if (-not (Test-Path $iconPath))
{
$cs.Job_WriteLog("Warning: DisplayIcon target not found at $iconPath")
}

$cs.Reg_SetExpandString $cs.Reg_GetString