private/Step-BuildBootCoreWinPEWallpaper.ps1

#Requires -PSEdition Core

function Step-BuildBootCoreWinPEWallpaper {
    <#
    .SYNOPSIS
        Copies the selected wallpaper into the mounted WinPE image
 
    .DESCRIPTION
        Selects wallpaper.jpg beside the build profile. If it does not exist,
        the function falls back to the module's bundled default.jpg.
 
        The selected file replaces Windows\System32\winpe.jpg in the mounted image.
        When the destination exists, the function takes ownership, grants the local
        Administrators group full control, clears file attributes, and removes it.
        A warning is written when no source wallpaper is available.
 
    .EXAMPLE
        PS> Step-BuildBootCoreWinPEWallpaper
 
        Selects and copies the highest-precedence available wallpaper.
 
    .INPUTS
        None. This function does not accept pipeline input.
 
    .OUTPUTS
        None.
 
    .NOTES
        Author: David Segura
        Company: Recast Software
        Version: 0.1.0
        Date: 2026-08-28
 
        Requires global BuildMedia.MountPath and BuildMedia.BuildProfile and script
        OSDeployModuleBase. BuildProfileContentPath optionally overrides the profile-local
        content directory. Replacing an existing wallpaper requires takeown.exe and icacls.exe
        and modifies its ownership and permissions before deletion.
    #>

    [CmdletBinding()]
    param ()

    $MountPath = $global:BuildMedia.MountPath
    $BuildProfilePath = $global:BuildMedia.BuildProfile
    $BuildProfileContentPath = $global:BuildMedia.BuildProfileContentPath
    if (-not $BuildProfileContentPath -and $BuildProfilePath) {
        $BuildProfileContentPath = Split-Path -Path $BuildProfilePath -Parent
    }
    $ProfileWallpaper = if ($BuildProfileContentPath) {
        Join-Path $BuildProfileContentPath 'wallpaper.jpg'
    }
    $BundledWallpaper = Join-Path $Script:OSDeployModuleBase 'core' 'boot-wallpaper' 'default.jpg'

    if ($ProfileWallpaper -and (Test-Path -LiteralPath $ProfileWallpaper -PathType Leaf)) {
        $Wallpaper = $ProfileWallpaper
    }
    elseif (Test-Path -LiteralPath $BundledWallpaper -PathType Leaf) {
        $Wallpaper = $BundledWallpaper
    }
    else {
        Write-Warning "[$(Get-Date -Format s)] No profile or bundled WinPE wallpaper was found."
        return
    }

    $Destination = "$MountPath\Windows\System32\winpe.jpg"
    Write-Host -ForegroundColor DarkGray "[$(Get-Date -format s)] [INFO] Copying WinPE wallpaper $Wallpaper"

    if (Test-Path -LiteralPath $Destination) {
        # Take ownership so we can modify the file
        & takeown.exe /F $Destination /A | Out-Null
        # Grant full control to Administrators
        & icacls.exe $Destination /grant 'Administrators:F' | Out-Null
        # Clear read-only and system attributes
        Set-ItemProperty -LiteralPath $Destination -Name Attributes -Value 'Normal'
        Remove-Item -LiteralPath $Destination -Force -ErrorAction SilentlyContinue
    }

    Copy-Item -LiteralPath $Wallpaper -Destination $Destination -Force
}