private/Expand-WinPEDriverPackageZip.ps1

#Requires -PSEdition Core

function Expand-WinPEDriverPackageZip {
    <#
    .SYNOPSIS
        Expands a ZIP archive to a destination directory
 
    .DESCRIPTION
        Validates that the source ZIP archive exists, creates the destination directory
        when needed, and extracts the archive with Expand-Archive. Extraction errors are
        terminating.
 
    .PARAMETER Path
        Specifies the path to the ZIP archive. The path must resolve before the destination
        directory is created.
 
    .PARAMETER DestinationPath
        Specifies the directory that receives the extracted files. The function creates
        the directory when it does not exist.
 
    .PARAMETER Force
        Allows Expand-Archive to overwrite existing files in the destination directory.
 
    .EXAMPLE
        PS> Expand-WinPEDriverPackageZip -Path 'C:\Drivers\WinPE.zip' -DestinationPath 'C:\Drivers\WinPE' -Force
 
        Expands WinPE.zip to C:\Drivers\WinPE, overwrites existing files, and returns the
        destination directory.
 
    .INPUTS
        None. This function does not accept pipeline input.
 
    .OUTPUTS
        System.IO.DirectoryInfo. Returns the destination directory after Expand-Archive
        completes successfully.
 
    .NOTES
        Author: David Segura
        Company: Recast Software
        Version: 1.0.0
        Date: 2026-08-28
 
        Dependencies:
          PowerShell Modules: Microsoft.PowerShell.Archive
          DotNet Classes: System.IO.DirectoryInfo, System.IO.FileNotFoundException,
                          System.Management.Automation.ErrorCategory,
                          System.Management.Automation.ErrorRecord
    #>

    [CmdletBinding()]
    [OutputType([System.IO.DirectoryInfo])]
    param (
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$Path,

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$DestinationPath,

        [Parameter()]
        [switch]$Force
    )

    if (-not (Test-Path -Path $Path)) {
        $PSCmdlet.ThrowTerminatingError(
            [System.Management.Automation.ErrorRecord]::new(
                [System.IO.FileNotFoundException]::new("Zip file not found: $Path"),
                'ZipFileNotFound',
                [System.Management.Automation.ErrorCategory]::ObjectNotFound,
                $Path
            )
        )
    }

    if (-not (Test-Path -Path $DestinationPath)) {
        New-Item -ItemType Directory -Path $DestinationPath -Force | Out-Null
    }

    Write-Verbose "[$($MyInvocation.MyCommand.Name)] Extracting zip: $Path -> $DestinationPath"

    # Splat common arguments and pass Force only when the caller explicitly requested overwrite behavior.
    $expandParams = @{
        Path            = $Path
        DestinationPath = $DestinationPath
        ErrorAction     = 'Stop'
    }
    if ($Force) {
        $expandParams['Force'] = $true
    }

    Expand-Archive @expandParams

    Write-Verbose "[$($MyInvocation.MyCommand.Name)] Zip extraction complete: $DestinationPath"
    Get-Item -Path $DestinationPath
}