Introduction

In this blog post, we will be exploring the Get-InstalledSoftware PowerShell script. This script is designed to retrieve a list of installed software from the Windows Registry.

Prerequisites

The Script

The script is written in a function-based approach and uses various cmdlets to scan the registry for installed software. Here's a breakdown of the code:

Function Get-InstalledSoftware {
<#
.SYNOPSIS
    Retrieves a list of installed software from the Registry.
.DESCRIPTION
    Scans HKLM (64-bit), HKLM (32-bit WOW64), and HKCU hives for installed software.
.PARAMETER Name
    The name (or partial name) of the software to find. Accepts wildcards.
.EXAMPLE
    Get-InstalledSoftware -Name "Google"
#>
[CmdletBinding()]
Param (
    [Parameter(Position=0, ValueFromPipeline=$true)]
    [string]$Name = "*"
)

Process {
    # Define all 3 registry locations to ensure we don't miss 32-bit or User-level apps
    $RegistryPaths = @(
        "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*",           # 64-bit System
        "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*", # 32-bit System
        "HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"            # Current User
    )

    # Process all paths and output objects immediately
    $Results = foreach ($Path in $RegistryPaths) {
        Get-ItemProperty -Path $Path -ErrorAction SilentlyContinue | 
            Where-Object { 
                $_.DisplayName -and ($_.DisplayName -like "*$Name*") 
            } | 
            Select-Object @{N='Name';E={$_.DisplayName}}, @{N='Version';E={$_.DisplayVersion}}
    }

    # Sort and remove duplicates (common when scanning multiple hives)
    $Results | Sort-Object Name | Select-Object -Unique Name, Version
}
}

How It Works

Registry Scanning

The script defines three registry paths to scan for installed software: HKLM (64-bit and 32-bit), and HKCU. These paths are used as input for the Get-ItemProperty cmdlet, which retrieves the necessary information about each installed software.

Finding Software by Name

The Where-Object cmdlet is used to filter the results based on the DisplayName property of each registry entry. The script accepts wildcards in the $Name parameter, allowing for partial matches when searching for specific software.

Key Code Snippets


$RegistryPaths = @(
    "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*",           # 64-bit System
    "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*", # 32-bit System
    "HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"            # Current User
)

$Results = foreach ($Path in $RegistryPaths) {
    Get-ItemProperty -Path $Path -ErrorAction SilentlyContinue | 
        Where-Object { 
            $_.DisplayName -and ($_.DisplayName -like "*$Name*") 
        } | 
        Select-Object @{N='Name';E={$_.DisplayName}}, @{N='Version';E={$_.DisplayVersion}}
}

$Results | Sort-Object Name | Select-Object -Unique Name, Version

Usage Examples

To use this script, simply call the Get-InstalledSoftware function and provide a name or partial name of the software you're looking for. For example:


Get-InstalledSoftware -Name "Google"

Conclusion

This PowerShell script provides an effective way to retrieve a list of installed software from the Windows Registry. By using the Get-ItemProperty and Where-Object cmdlets, we can scan multiple registry paths for installed software and filter results based on specific criteria.