Get-WingetInstallerHash.ps1


<#PSScriptInfo
 
.VERSION 1.0.1
 
.GUID 9b52c48a-8a4f-4be8-981b-b702ced6cb0c
 
.AUTHOR Kalichuza
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS PowerShell, Winget, Installer, Hash
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
.PRIVATEDATA
 
#>


<#
 
.DESCRIPTION
Pulls the installer hash from winget packages
 
#>
 

[CmdletBinding()]
param (
    [string]$WingetApp 
)

function Get-WingetInstallerHash {
    param (
        [string]$WingetApp
    )

    if (-not $WingetApp) {
        Write-Host "Please provide the application name as a parameter." -ForegroundColor Red
        return
    }

    try {
        # Run Winget show command and store output
        $wingetOutput = winget show $WingetApp 2>&1
        
        # Check if Winget returned an error
        if ($wingetOutput -match "No installed package found") {
            Write-Host "Error: Unable to find the application '$WingetApp'." -ForegroundColor Red
            return
        }

        # Extract the SHA256 hash
        $WingetAppHash = $wingetOutput | ForEach-Object { 
            if ($_ -match "Installer SHA256:\s*([a-fA-F0-9]{64})") {
                $matches[1]
            }
        }

        if ($WingetAppHash) {
            Write-Output "$WingetAppHash"
        } else {
            Write-Host "No SHA256 hash found for '$WingetApp'. It may be a Microsoft Store app or missing an installer hash." -ForegroundColor Yellow
        }
    } catch {
        Write-Host "An error occurred: $_" -ForegroundColor Red
    }
}

# Call function if running interactively
if ($WingetApp) {
    Get-WingetInstallerHash -WingetApp $WingetApp
}