Public/Get-ProductCode.ps1

function Get-ProductCode
{
    <#
        .DESCRIPTION
            Search the registry for a product code
    
        .EXAMPLE
            $ProductCode = Get-ProductCode -Name "Google Chrome" -RegistryKey 64
    
        .NOTES
            Created by: Jon Anderson
            Modified: 2023-07-10
    #>

    [CmdletBinding()]
    param(
        [parameter(Mandatory = $true)][ValidateNotNullOrEmpty()]
        [String]$Name,
        [parameter(Mandatory = $true)][ValidateSet("64","32")]
        [String]$UninstallKey
    )
    
    Write-LogEntry -Value "Search for a product code for application $($Name)" -Severity 1
    switch ($UninstallKey)
    {
        '64' {$RegistryKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"}
        '32' {$RegistryKey = "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"}
    }
    Write-LogEntry -Value "Searching in $RegistryKey" -Severity 1
    $Keys = Get-ChildItem -Path $RegistryKey | Select-Object -ExpandProperty Name
    foreach($Key in $Keys)
    {
        $TempPath = $Key -replace "HKEY_LOCAL_MACHINE","HKLM:"
        $TempKey = Get-ItemProperty -Path $TempPath
        if(($TempKey).DisplayName -match $Name)
        {
            if($TempKey.PSChildName -Match ("^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$"))
            {
                Write-LogEntry -Value "Product code found for application $($Name): $($TempKey.PSChildName)" -Severity 1
                return $TempKey.PSChildName.ToString()
            }
            else
            {
                Write-LogEntry -Value "No product code found for application: $($Name). UninstallString is: $($TempKey.UninstallString)" -Severity 2
                return $NULL
            }
        }
    }
}