private/Step-BuildBootUpdateUsbDrive.ps1

#Requires -PSEdition Core

function Step-BuildBootUpdateUsbDrive {
    <#
    .SYNOPSIS
        Updates USB volumes labeled USB-WinPE with the built media
 
    .DESCRIPTION
        When global BuildMedia.UpdateUSB is true, finds every volume whose filesystem
        label is USB-WinPE. For each volume with an accessible drive letter, it uses
        robocopy.exe to copy global BuildMedia.MediaPath recursively to the volume root
        while excluding the recycle bin and System Volume Information directories.
 
        A warning is written when no matching volume exists. The function performs no
        action when UpdateUSB is not true.
 
    .EXAMPLE
        PS> Step-BuildBootUpdateUsbDrive
 
        Updates accessible USB-WinPE volumes when USB updating is enabled in build state.
 
    .INPUTS
        None. This function does not accept pipeline input.
 
    .OUTPUTS
        System.String. Returns native stdout emitted by robocopy.exe for each updated volume.
 
    .NOTES
        Author: David Segura
        Company: Recast Software
        Version: 0.1.0
        Date: 2026-08-28
 
        Requires global BuildMedia.UpdateUSB and BuildMedia.MediaPath, Get-Volume,
        robocopy.exe, and write access to matching volume roots. Existing destination
        files can be overwritten; destination-only files are not purged.
    #>

    [CmdletBinding()]
    param ()

    $UpdateUSB = $global:BuildMedia.UpdateUSB
    $MediaPath = $global:BuildMedia.MediaPath

    if ($UpdateUSB -eq $true) {
        Write-HostDateTimeDarkGray 'Update USB-WinPE Partition'
        $WinpeVolumes = Get-Volume | Where-Object { $_.FileSystemLabel -eq 'USB-WinPE' }
        if ($WinpeVolumes) {
            foreach ($volume in $WinpeVolumes) {
                $driveLetter = $volume.DriveLetter
                if ($driveLetter -and (Test-Path -Path "$($driveLetter):\")) {
                    robocopy.exe "$MediaPath" "$($driveLetter):\" *.* /e /ndl /np /r:0 /w:0 /xd '$RECYCLE.BIN' 'System Volume Information' /xj
                }
            }
        }
        else {
            Write-Warning "[$(Get-Date -Format s)] No USB partition labeled USB-WinPE found"
        }
    }
}