private/WinPEDrivers/CoreWinPEDriver/Expand-CoreWinPEDriverCab.ps1

#Requires -PSEdition Core

function Expand-CoreWinPEDriverCab {
    <#
    .SYNOPSIS
        Extracts the contents of a .cab file using expand.exe.
 
    .DESCRIPTION
        Internal helper that invokes the native Windows expand.exe to extract
        cabinet archives. Returns the destination directory on success.
 
    .PARAMETER Path
        Full path to the .cab file.
 
    .PARAMETER DestinationPath
        Directory where contents will be extracted.
 
    .OUTPUTS
        [System.IO.DirectoryInfo] The extraction destination directory.
    #>

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

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

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

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

    Write-Verbose "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Extracting cab: $Path -> $DestinationPath"
    $expandExe = Join-Path $env:SystemRoot 'System32\expand.exe'

    $process = Start-Process -FilePath $expandExe `
        -ArgumentList "`"$Path`"", '-F:*', "`"$DestinationPath`"" `
        -NoNewWindow -Wait -PassThru

    if ($process.ExitCode -ne 0) {
        $PSCmdlet.ThrowTerminatingError(
            [System.Management.Automation.ErrorRecord]::new(
                [System.InvalidOperationException]::new(
                    "expand.exe failed with exit code $($process.ExitCode) for '$Path'"
                ),
                'CabExpandFailed',
                [System.Management.Automation.ErrorCategory]::InvalidResult,
                $Path
            )
        )
    }

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