private/Test-Install7Zip.ps1

function Test-Install7Zip {
    <#
    .SYNOPSIS
        Tests whether 7-Zip is installed
 
    .DESCRIPTION
        Checks for 7z.exe in the default 64-bit and 32-bit installation paths.
        If neither file exists, searches the machine uninstall registry for a
        display name beginning with 7-Zip. Returns $true when any check finds
        an installation; otherwise, returns $false.
 
    .EXAMPLE
        PS> Test-Install7Zip
 
        Tests the default paths and uninstall registry for 7-Zip.
 
    .INPUTS
        None. This function does not accept pipeline input.
 
    .OUTPUTS
        System.Boolean. Returns $true when 7-Zip is detected; otherwise,
        returns $false.
 
    .NOTES
        Author: David Segura
        Company: Recast Software
        Version: 1.0.0
        Date: 2026-08-28
    #>

    [OutputType([bool])]
    param ()

    # Fast path — default install location
    $defaultPaths = @(
        (Join-Path $env:ProgramFiles   '7-Zip\7z.exe'),
        (Join-Path ${env:ProgramFiles(x86)} '7-Zip\7z.exe')
    )
    foreach ($path in $defaultPaths) {
        if (Test-Path -Path $path) { return $true }
    }

    # Registry fallback
    $regPaths = @(
        'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*',
        'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
    )

    foreach ($regPath in $regPaths) {
        $entry = Get-ItemProperty -Path $regPath -ErrorAction SilentlyContinue |
            Where-Object { $_.PSObject.Properties['DisplayName'] -and $_.DisplayName -like '7-Zip*' } |
            Select-Object -First 1
        if ($entry) { return $true }
    }

    return $false
}