Working with Registry keys using PSADT 4.1.x

I have an Intune deployment I'm trying to build for Microsoft SSMS 21 and due to Microsoft's decision not to put the same Product version number on the SSMS.exe as the release version - (they had done for a few versions previously but not v21.x :exploding_head:), and sadly the Uninstall path to the product in the Registry doesn't look much better, it looks like it 'may' change with each subsequent version (currently HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\9349aeac) - I'd just like some consistency Microsoft :man_shrugging:
As a result i'm struggling with my deployment detection rule I'd like a consistent and reliable Detection rule to determine if the install has landed successfully.

I think I'd like to create a 'tag' in the Registry after the install has succeeded by way of a consistent Registry value that can be checked with my Detection rule.

So I have been investigating the PSADT Registry functions

I'm feeling slightly confused with how to use these 4 *-ADTRegistry* functions

I'm thinking creating HKLM:\SOFTWARE\CompanyName\SSMS 21\CurrentVersion with a value for the CurrentVersion of 21.5.14
N.B. I realise I'll need to perform this within the Post-Install section of my Invoke-AppDeployToolkit.ps1 script

And after the product has been successfully uninstalled, remove the Registry Key
e.g. HKLM:\SOFTWARE\CompanyName\SSMS 21
N.B. I realise I'll need to perform this within the Post-Uninstall section of my Invoke-AppDeployToolkit.ps1 script

Where should I start? :thinking:

I have an idea that it's something like:

Post-Install:

Set-ADTRegistryKey -Key 'HKEY_LOCAL_MACHINE\SOFTWARE\CompanyName\SSMS 21' -Name 'CurrentVersion' -Type 'DWord' -Value '21.5.14'

Do I need to check if the path exists before assuming it will just create the Key?
and if I do how to check? Is it something like this?

if (!(Test-ADTRegistryValue -Key 'HKLM:\SOFTWARE\CompanyName\SSMS 21' -Name 'CurrentVersion')) {
    # Set (create) Registry value for new install of SSMS 21.x
    Set-ADTRegistryKey -Key 'HKEY_LOCAL_MACHINE\SOFTWARE\CompanyName\SSMS 21' -Name 'CurrentVersion' -Type 'DWord' -Value '21.5.14'
} else {
    # Update Registry value if a newer version has been just been installed
    if ((Get-ADTRegistryKey -Key 'HKEY_LOCAL_MACHINE\SOFTWARE\Company\SSMS 21' -Name 'CurrentVersion') -lt '21.5.14') {
        Set-ADTRegistryKey -Key 'HKEY_LOCAL_MACHINE\SOFTWARE\CompanyName\SSMS 21' -Name 'CurrentVersion' -Type 'DWord' -Value '21.5.14'
    }
}

What isn't covered by the above logic is if the Registry path HKEY_LOCAL_MACHINE\SOFTWARE\CompanyName\SSMS 21 doesn't exist what would happen?, but I can't seem to work out how to test for this :thinking:

Post-Uninstall:

Remove-ADTRegistryKey -Key 'HKEY_LOCAL_MACHINE\Software\CompanyName\SSMS 21' -Recurse

Do I need the -Recurse switch?

These functions should work almost identically to how they do in 3.x.

You will want this to be a string as your version number can't be represented as a DWORD.

This is mostly correct, however you'll want if ([System.Version](Get-ADTRegistryKey -Key 'HKEY_LOCAL_MACHINE\SOFTWARE\Company\SSMS 21' -Name 'CurrentVersion') -lt '21.5.14') { so the string is cast into a proper version object so the lt comparison operator works right.

If the key doesn't exist, it'll get created as required.

No, because there's no subkeys to do purge also. It doesn't hurt to add it though.

@mjr4077au Perfect, helpful answers as usual :star_struck:
Thank you for you time and effort

@mjr4077au FYI: I think the documentation showing the use of String in the Set-ADTRegistryKey may need updating for example 3 here:

All other examples single quote the -Type
e.g. -Type 'DWord', -Type 'Binary'
but this example does not: -Type String
Unsure if it's important?

Single word parameter values don't need to be quoted, so in some regards it's better to leave it as it is to demonstrate that.

I use double detection for intune, so I have file, path, exe = greater than or = to file version 1.234.2345 . So I have the the file version to be sure and psappdeploy writes a key as I'm using master wrapper for speed so it writes in its own area, which can be used for detection.

Set-ADTRegistryKey -Key "HKLM\SOFTWARE\InstalledApps\AppsAnywhere_AppsAnywhere_2.5.0"