Create a bootable USB drive for Windows Server
The Windows Server image contains a .wim
file which is bigger than 4GB. This is a problem because FAT32 does not support files bigger than 4GB. To solve this problem, we need to split the .wim
file into smaller files. This can be done with the dism
command. The following script extracts the content of the ISO file, splits the .wim
file, formats the USB drive, and copies the files to the USB 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!
Enjoy this blog?💖 Sponsor on GitHub