Search code examples
powershellregistry

Get upgrade code for product code from registry


I need a hint how to get the upgrade code from an installed MSI out of the registry. Actually I'm having the product code, which can be retrieved from HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\.

Now I want to retrieve the upgrade code (based on the product code) from HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UpgradeCodes.

My problem is that the product code is used as value-name, which means I've a REG_SZ where the name is the product code guid and the value is empty.

One way to retrieve the product code might be:

PS HKLM:\SOFTW...Codes> Get-ItemProperty * | select -First 1 | gm

    TypeName: System.Management.Automation.PSCustomObject

Name                             MemberType   Definition
----                             ----------   ----------
Equals                           Method       bool Equals(System.Object obj)
GetHashCode                      Method       int GetHashCode()
GetType                          Method       type GetType()
ToString                         Method       string ToString()
42F79228D77BA4A4EB5150F3DC090CE3 NoteProperty System.String 42F79228D77BA4A4EB5150F3DC090CE3=
...

How can I check if a PSCustomObject has the property 42F79228D77BA4A4EB5150F3DC090CE3?

Does anybody knows if there is a more elegant way?


Solution

  • This is how you can check. Working on that elegant solution...

    $properties = Get-ItemProperty * | select -first 1 | Get-Member | Where-object {$_.MemberType -eq "NoteProperty"}
    
    if("42F79228D77BA4A4EB5150F3DC090CE3" -in $properties.Name){
        Write-Output "It's in there!"
    }
    

    Edit

    This is a bit more elegant. It goes to the HKLM path, and checks for a PSChildName (Registry Key) that is the same as the code.

    If found, it will return the Name and property. If not found, the variable $codeExists will be $null.

    $code       = "42F79228D77BA4A4EB5150F3DC090CE3" 
    $codeExists = Get-ChildItem "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UpgradeCodes" | Where-Object {$_.PSChildName -eq $code}
    
    if($codeExists){
         Write-Output "It's in there!"
    }