private/MDT/pwsh/Step-WinPEAppZip.ps1

function Step-WinPEAppZip {
    <#
    .SYNOPSIS
        Downloads and adds 7-Zip tools to a mounted WinPE image.
 
    .DESCRIPTION
        Downloads 7zr.exe (standalone) and the 7-Zip extra package from the
        official 7-Zip GitHub releases and caches them under
        WinPEAppsPath\7zip\<version>. The tools are then copied to
        MountPath\Windows\System32 so they are available during WinPE boot.
 
        Any older cached versions are cleaned up automatically.
        If 7zr.exe already exists in System32, this function exits immediately
        without re-downloading.
 
    .PARAMETER AppName
        Display name recorded in $global:BuildMedia.InstalledApps. Default: '7zip'.
 
    .PARAMETER Architecture
        Target architecture for the downloaded binary. Default: $global:BuildMedia.Architecture.
 
    .PARAMETER MountPath
        Path to the directory where the WIM image is mounted.
        Defaults to $global:BuildMedia.MountPath.
 
    .PARAMETER WinPEAppsPath
        Root directory used to cache downloaded WinPE app binaries.
        Defaults to $global:BuildMedia.WinPEAppsPath.
 
    .EXAMPLE
        Step-WinPEAppZip
 
        Downloads (if needed) and installs 7-Zip tools using values from the global BuildMedia hashtable.
 
    .INPUTS
        None. This function does not accept pipeline input.
 
    .OUTPUTS
        None.
 
    .NOTES
        Author: David Segura
        Company: Recast Software
        Called by Invoke-OSDeployMDT during the WIM stage.
    #>

    [CmdletBinding()]
    param (
        [System.String]
        $AppName = '7zip',
        [System.String]
        $Architecture = $global:BuildMedia.Architecture,
        [System.String]
        $MountPath = $global:BuildMedia.MountPath,
        [System.String]
        $WinPEAppsPath = $global:BuildMedia.WinPEAppsPath
    )
    #=================================================
    $Error.Clear()
    Write-Verbose "[$(Get-Date -format s)] Start"
    #=================================================
    Write-Verbose "[$(Get-Date -format s)] Architecture: $Architecture"
    Write-Verbose "[$(Get-Date -format s)] MountPath: $MountPath"
    Write-Verbose "[$(Get-Date -format s)] WinPEAppsPath: $WinPEAppsPath"
    #=================================================
    if (Test-Path -LiteralPath "$MountPath\Windows\System32\7za.exe") {
        Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] $AppName already installed at $MountPath\Windows\System32\7za.exe"
        return
    }
    #=================================================
    $zipVersion = '26.00'
    $CacheZipRoot = Join-Path $WinPEAppsPath "7zip"
    $CacheZip = Join-Path $CacheZipRoot $zipVersion

    # Cleanup any old versions of the app in the cache
    if (Test-Path -Path $CacheZipRoot) {
        Get-ChildItem -Path $CacheZipRoot -File | Remove-Item -Force
        Get-ChildItem -Path $CacheZipRoot -Directory | Where-Object { $_.Name -ne $zipVersion } | Remove-Item -Recurse -Force
    }

    # Ensure the cache directory for this version exists, if not create it
    if (Test-Path -Path $CacheZip) {
        Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] Source: $CacheZip"
    }
    else {
        Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] Adding cache content $CacheZip"
        New-Item -Path $CacheZip -ItemType Directory -Force | Out-Null
    }

    if (-not (Test-Path -Path "$CacheZip\7zr.exe")) {
        $DownloadStandalone = 'https://github.com/ip7z/7zip/releases/download/26.00/7zr.exe'
        curl.exe --location --output "$CacheZip\7zr.exe" $DownloadStandalone
    }

    if (-not (Test-Path -Path "$CacheZip\7za")) {
        $DownloadExtra = 'https://github.com/ip7z/7zip/releases/download/26.00/7z2600-extra.7z'
        curl.exe --location --output "$CacheZip\7z2600-extra.7z" $DownloadExtra
        $DownloadExtraResult = Get-Item -LiteralPath "$CacheZip\7z2600-extra.7z" -ErrorAction SilentlyContinue
        $null = & "$CacheZip\7zr.exe" x "$($DownloadExtraResult.FullName)" -o"$CacheZip\7za" -y
    }

    if ($Architecture -eq 'amd64') {
        Copy-Item -Path "$CacheZip\7za\x64\*" -Destination "$MountPath\Windows\System32" -Recurse -Force
        
        # Record the installed app
        $global:BuildMedia.InstalledApps += $AppName
    }
    <#
    if ($Architecture -eq 'arm64') {
        Copy-Item -Path "$CacheZip\7za\arm64\*" -Destination "$MountPath\Windows\System32" -Recurse -Force
         
        # Record the installed app
        $global:BuildMedia.InstalledApps += $AppName
    }
    #>

    #=================================================
    Write-Verbose "[$(Get-Date -format s)] End"
    #=================================================
}