functions/dynamicsnav/Get-DynamicsPlatformVersions.ps1

function Get-DynamicsPlatformVersions {
    <#
    .SYNOPSIS
    Retrieves all installed Dynamics NAV or Business Central platform versions from the system registry.
 
    .DESCRIPTION
    This function scans the local machine registry under:
    HKLM:\SOFTWARE\Microsoft\Microsoft Dynamics NAV\
    and returns all subkeys representing installed platform versions (e.g., 80, 90, 100, 110, 210, etc.)
    that contain a '\Service' subkey, indicating that the version is actually installed.
 
    .OUTPUTS
    [string[]] A list of platform version strings that have a valid 'Service' subkey.
 
    .EXAMPLE
    $versions = Get-DynamicsPlatformVersions
    foreach ($version in $versions) {
        Write-Host "Found Dynamics Platform Version: $version"
    }
 
    .NOTES
    Requires administrative rights to access the 64-bit local machine registry.
    #>


    [CmdletBinding()]
    param ()

    $basePath = "HKLM:\SOFTWARE\Microsoft\Microsoft Dynamics NAV"

    if (-not (Test-Path $basePath)) {
        Write-Verbose "Base registry path not found: $basePath"
        return @()
    }

    $platformVersions = @()

    try {
        $subKeys = Get-ChildItem -Path $basePath -ErrorAction Stop | Where-Object {
            $_.Name -match '\\\d+$' # only numeric version folders
        }

        foreach ($key in $subKeys) {
            $version = ($key.Name -split '\\')[-1]
            $servicePath = Join-Path -Path $key.PSPath -ChildPath "Service"
            if (Test-Path $servicePath) {
                # TODO check Service\Installed = 1
                $platformVersions += $version
            }
        }
    }
    catch {
        Write-Warning "Failed to access registry: $_"
    }

    return $platformVersions
}