Set-RegistryTag and Remove-RegistryTag

Topics: Archive - Toolkit Extensions
May 2, 2014 at 3:18 PM
Edited May 2, 2014 at 3:24 PM
Here's a pair of functions I developed recently to add a "tag" to the Windows Registry after installation. This can be used for easy application detection and version control using SCCM 2012's Application model.

Inspiration for this idea came from Jerre's Java installation script. I haven't copied any of his code, but upon reflection, they look awfully similar since they perform the same task using the same toolkit. Jerre, if you see this and you'd prefer that I take it down, let me know and I will do so.

Under the variable declaration section of the Extensions script, define a variable $regTagRoot, and set it equal to the "root" key you'd like all your tags to fall under. For example:
$regTagRoot = "HKEY_LOCAL_MACHINE\SOFTWARE\MyOrganization\Applications"
Here are the functions themselves.
function Set-RegistryTag
{
    $regKey = "$regTagRoot\$appVendor\$appName"
    Write-Log "Writing application registry tag"
    Set-RegistryKey -Key $regKey -Name "AppVersion" -Value "$appVersion" -Type String -ContinueOnError $true
    Set-RegistryKey -Key $regKey -Name "InstallDate" -Value (Get-Date) -Type String -ContinueOnError $true
    Set-RegistryKey -Key $regKey -Name "DeploymentScriptVersion" -Value "$appScriptVersion" -Type String -ContinueOnError $true
    Set-RegistryKey -Key $regKey -Name "DeploymentEngineVersion" -Value "$deployAppScriptVersion" -Type String -ContinueOnError $true
    Set-RegistryKey -Key $regKey -Name "DeploymentExtensionVersion" -Value "$appDeployExtScriptVersion" -Type String -ContinueOnError $true
}

function Remove-RegistryTag
{
    $regKeyParent = "$regTagRoot\$appVendor"
    
    if (Test-Path -Path $regKeyParent)
    {
        $regKey = "$regKeyParent\$appName"
        if (Test-Path -Path $regKey)
        {
            Write-Log "Removing application registry tag"
            Remove-RegistryKey -Key $regKey -Recurse -ContinueOnError $true
        }

        if (Get-ChildItem -Path $regKeyParent)
        {
            Write-Log "Other registry entries still exist under the parent key ('$regKeyParent')"
        } else {
            Write-Log "Parent registry key ('$regKeyParent') was detected as empty and will be removed"
            Remove-RegistryKey -Key $regKeyParent -Recurse -ContinueOnError $true
        }
    }
}