Create a bootable USB drive for Windows Server

 
 
  • Gérald Barré

Windows Server ISO images contain a .wim file larger than 4 GB, which exceeds the FAT32 file size limit. To work around this, split the .wim file into smaller parts using the dism command. The script below extracts the ISO contents, splits the .wim file, formats the USB drive as FAT32, and copies all files to the drive.

PowerShell
# TODO must be run as administrator
# TODO configure both the ISO file path and the USB drive

$ErrorActionPreference = "Stop"
$PSNativeCommandUseErrorActionPreference = $true

$IsoFile = Resolve-Path "en-us_windows_server_x64_dvd.iso"
$UsbDrive = Get-Disk | Where-Object BusType -eq "USB"

# Check if the USB drive is found
if ($null -eq $UsbDrive) {
    throw "No USB drive found"
}

# Extract the ISO to a temporary folder
$TempFolderPath = Join-Path $env:temp ([System.Guid]::NewGuid())
$TempFolder = New-Item -Path $TempFolderPath -ItemType Directory
$DiskImage = Mount-DiskImage -ImagePath $IsoFile -StorageType ISO -PassThru -NoDriveLetter
New-PSDrive -Name ISOFile -PSProvider FileSystem -Root (Get-Volume -DiskImage $DiskImage).UniqueId
try {
    Get-ChildItem "ISOFILE:" | Copy-Item -Destination $TempFolder -Recurse

    # Split the wim file into multiple 4GB files, so that it can be copied to a FAT32 drive
    dism /Split-Image "/ImageFile:$TempFolderPath\sources\install.wim" "/SWMFile:$TempFolderPath\sources\install.swm" /FileSize:4096
    Remove-Item -LiteralPath $TempFolderPath\sources\install.wim -Force

    # Format the USB drive to FAT32
    $UsbDrive | Clear-Disk -RemoveData -Confirm:$true -PassThru
    $UsbDrive | Set-Disk -PartitionStyle GPT
    $Volume = $UsbDrive | New-Partition -Size 8GB -AssignDriveLetter | Format-Volume -FileSystem FAT32 -NewFileSystemLabel WS2022

    # Copy Files to the USB drive
    Copy-Item -Path $TempFolderPath\* -Destination ($Volume.DriveLetter + ":\") -Recurse
}
finally {
    # Cleanup
    Remove-PSDrive -Name ISOFile
    Dismount-DiskImage -DevicePath $DiskImage.DevicePath
    $TempFolder | Remove-Item -Recurse -Force
}

Do you have a question or a suggestion about this post? Contact me!

Follow me:
Enjoy this blog?