Recipes by topic

Installed programs, read from the registry

The Windows “Installed apps” list comes from the Uninstall registry keys. The script reads them with the StdRegProv class of the root/default namespace, which also works remotely. Win32_Product is avoided: enumerating it triggers a consistency check of every MSI package (with possible repair), it is slow and it writes events to the event log.

function Get-RegistryString {
    [CmdletBinding()]
    param (
        [Microsoft.Management.Infrastructure.CimSession]
        $CimSession,

        [string]
        $SubKey,

        [string]
        $ValueName
    )

    $parameters = @{
        Namespace  = 'root/default'
        ClassName  = 'StdRegProv'
        MethodName = 'GetStringValue'
        Arguments  = @{
            hDefKey     = [uint32]2147483650
            sSubKeyName = $SubKey
            sValueName  = $ValueName
        }
    }
    if ($CimSession) {
        $parameters['CimSession'] = $CimSession
    }
    (Invoke-CimMethod @parameters).sValue
}

function Get-InstalledProgram {
    [CmdletBinding()]
    param (
        [Microsoft.Management.Infrastructure.CimSession]
        $CimSession
    )

    $uninstallPath = @(
        'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'
        'SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
    )

    foreach ($path in $uninstallPath) {
        $enumParameters = @{
            Namespace  = 'root/default'
            ClassName  = 'StdRegProv'
            MethodName = 'EnumKey'
            Arguments  = @{
                hDefKey     = [uint32]2147483650
                sSubKeyName = $path
            }
        }
        if ($CimSession) {
            $enumParameters['CimSession'] = $CimSession
        }
        $keyName = (Invoke-CimMethod @enumParameters).sNames

        foreach ($name in $keyName) {
            $valueParameters = @{
                SubKey = '{0}\{1}' -f $path, $name
            }
            if ($CimSession) {
                $valueParameters['CimSession'] = $CimSession
            }
            $displayName = Get-RegistryString @valueParameters -ValueName 'DisplayName'
            if ($displayName) {
                [PSCustomObject]@{
                    DisplayName    = $displayName
                    DisplayVersion = Get-RegistryString @valueParameters -ValueName 'DisplayVersion'
                    Publisher      = Get-RegistryString @valueParameters -ValueName 'Publisher'
                }
            }
        }
    }
}

Get-InstalledProgram |
    Sort-Object -Property DisplayName

hDefKey is 2147483650 for HKEY_LOCAL_MACHINE. The two paths cover 64-bit and 32-bit programs. Programs installed for a single user are under HKEY_USERS and are not read here. Entries without a DisplayName (components, updates) are skipped.

Variant: remote

The functions in the script accept -CimSession: define them first, then call them with the session.

$cimSession = New-CimSession -ComputerName 'PC01'
try {
    Get-InstalledProgram -CimSession $cimSession |
        Sort-Object -Property DisplayName
} finally {
    Remove-CimSession -CimSession $cimSession
}

← All recipes