Public/Publish-ImageToUSB.ps1

function Publish-ImageToUSB {
    [cmdletbinding()]
    param (
        [parameter(Mandatory = $true)]
        [string]$imagePath,

        [parameter(Mandatory = $false)]
        [string]$imageName = "Default Win10 Image",

        [parameter(Mandatory = $false)]
        [System.IO.FileInfo]$autopilotJson
    )
    #region Main Process
    try {
        #region start diagnostic // show welcome
        $sw = [System.Diagnostics.Stopwatch]::StartNew()
        $welcomeScreen = "ICAgIF9fICBfXyAgICBfXyAgX19fX19fICBfX19fX18gIF9fX19fXwogICAvXCBcL1wgIi0uLyAgXC9cICBfXyBcL1wgIF9fX1wvXCAgX19fXAogICBcIFwgXCBcIFwtLi9cIFwgXCAgX18gXCBcIFxfXyBcIFwgIF9fXAogICAgXCBcX1wgXF9cIFwgXF9cIFxfXCBcX1wgXF9fX19fXCBcX19fX19cCiAgICAgXC9fL1wvXy8gIFwvXy9cL18vXC9fL1wvX19fX18vXC9fX19fXy8KIF9fX19fXyAgX18gIF9fICBfXyAgX18gICAgICBfX19fXyAgIF9fX19fXyAgX19fX19fCi9cICA9PSBcL1wgXC9cIFwvXCBcL1wgXCAgICAvXCAgX18tLi9cICBfX19cL1wgID09IFwKXCBcICBfXzxcIFwgXF9cIFwgXCBcIFwgXF9fX1wgXCBcL1wgXCBcICBfX1xcIFwgIF9fPAogXCBcX19fX19cIFxfX19fX1wgXF9cIFxfX19fX1wgXF9fX18tXCBcX19fX19cIFxfXCBcX1wKICBcL19fX19fL1wvX19fX18vXC9fL1wvX19fX18vXC9fX19fLyBcL19fX19fL1wvXy8gL18vCiAgICAgICAgIF9fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fCiAgICAgICAgIFdpbmRvd3MgMTAgRGV2aWNlIFByb3Zpc2lvbmluZyBUb29sCiAgICAgICAgICoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioq"
        Write-Host $([system.text.encoding]::UTF8.GetString([system.convert]::FromBase64String($welcomeScreen)))
        if (!(Test-Admin)) {
            throw "Exiting -- need admin right to execute"
        }
        #endregion start diagnostic // show welcome
        #region set configuration
        Write-Host "`nSetting up configuration paths.." -ForegroundColor Yellow
        $usb = [ImageUSBClass]::new($imagePath, $imageName)
        New-TempFolder -path $usb.downloadPath
        $count = $usb.config.UrlPath.Count
        if ($count -eq 0) {
            exit
        }
        elseif ($count -eq 1) {
            $usb.url = $usb.config.UrlPath[0].path
            $usb.image = $usb.config.UrlPath[0].imageName
        }
        else {
            $item = Select-ImageToDownload -usbImage $usb
            $usb.url = $item.path
            $usb.image = $item.imageName
        }
        $urlPath = [system.uri]([System.Web.HttpUtility]::UrlDecode($usb.url))
        $usb.zipFile = Split-Path $urlPath.localPath -Leaf
        $usb.destFolder = Split-Path $usb.zipFile -LeafBase
        $usb.localPath = "$($usb.destFolder)\$($usb.zipFile)"
        #endregion set configuration
        #region get installation media
        Write-Host "`nChecking for Win10-Image.zip.." -ForegroundColor Yellow
        $usb = Get-Image -usbImage $usb
        #endregion get installation media
        #region apply the image to the usb
        Write-Host "`nApplying the windows image from the USB.." -ForegroundColor Yellow
        $usb.SetWimPath( $(Get-ChildItem "$($usb.downloadPath)\$(($usb.localPath -split ".zip")[0])" -Recurse -Include "install.wim") )
        $chooseDisk = Get-DiskToUse
        Write-Host "`nSetting up USB partitions.." -ForegroundColor Yellow
        $usb = Set-USBPartiton -partcount $usb.DirCount -usbImage $usb -disknum $chooseDisk
        Write-Host "`nWriting media to USB (This may take a while).." -ForegroundColor Yellow
        Write-ToUsb -usbImage $usb
        #endregion apply the image to the usb
        #region apply autopilot.json to usb
        if ($autopilotJson) {
            Write-Host "`nWriting autopilot configuration to USB.." -ForegroundColor Yellow
            Copy-Item $autopilotJson -Destination "$($usb.drive):\scripts" -Force | Out-Null
        }
        #endregion apply autopilot.json to usb
        #region download and apply powershell 7 to usb
        Write-Host "`nGrabbing PWSH 7.." -ForegroundColor Yellow
        Start-BitsTransfer -Source 'https://aka.ms/install-powershell.ps1' -Destination "$env:Temp\install-powershell.ps1"
        . $env:Temp\install-powershell.ps1 -daily -Destination "C:\bin\pwsh"
        Write-Host "`nMoving scripts to USB.." -ForegroundColor Yellow
        #endregion download and apply powershell 7 to usb
        $completed = $true
    }
    catch {
        $errorMsg = $_.Exception.Message
    }
    finally {
        $sw.Stop()
        if ($errorMsg) {
            Write-Warning $errorMsg
        }
        else {
            if ($completed) {
                Write-Host "`nUSB Image built successfully..`nTime taken: $($sw.Elapsed)" -ForegroundColor Green
            }
            else {
                Write-Host "`nScript stopped before completion..`nTime taken: $($sw.Elapsed)" -ForegroundColor Green
            }
        }
    }
    #endregion
}