Misc/Expand-7zipArchive.ps1

<#
.synopsis
    Extracts a zip file via 7zip, if installed
.description
    Extract a zip file via 7zip, if it is installed. If not, it tries to install it
.parameter Path
    Url to the zip
.parameter DestinationPath
    path to etract to
.example
    Expand-7zipArchive -Path C:\temp\temp.zip -DestinationPath C:\temp\
#>

function Expand-7zipArchive {
    Param (
        [Parameter(Mandatory=$true)]
        [string] $Path,
        [string] $DestinationPath
    )

    $7zipPath = "$env:ProgramFiles\7-Zip\7z.exe"

    $use7zip = $false
    if (!(Test-Path -Path $7zipPath -PathType Leaf)) {
        $dlurl = 'https://7-zip.org/' + (Invoke-WebRequest -Uri 'https://7-zip.org/' | Select-Object -ExpandProperty Links | Where-Object {($_.innerHTML -eq 'Download') -and ($_.href -like "a/*") -and ($_.href -like "*-x64.exe")} | Select-Object -First 1 | Select-Object -ExpandProperty href)
        # above code from: https://perplexity.nl/windows-powershell/installing-or-updating-7-zip-using-powershell/
        $installerPath = Join-Path $env:TEMP (Split-Path $dlurl -Leaf)
        Invoke-WebRequest $dlurl -OutFile $installerPath
        Start-Process -FilePath $installerPath -Args "/S" -Verb RunAs -Wait
        Remove-Item $installerPath
    }

    if (Test-Path -Path $7zipPath -PathType Leaf) {
        try {
            $use7zip = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($7zipPath).FileMajorPart -ge 19
        }
        catch {
            $use7zip = $false
        }
    }

    if ($use7zip) {
        Set-Alias -Name 7z -Value $7zipPath
        $command = '7z x "{0}" -o"{1}" -aoa -r' -f $Path,$DestinationPath
        Invoke-Expression -Command $command | Out-Null
    } else {
        Expand-Archive -Path $Path -DestinationPath "$DestinationPath" -Force
    }
}