14 January 2020

How to create a bootable USB stick for Windows Server 2019 with PowerShell

To create a USB stick to install Windows Server 2019 on a UEFI (GPT) system it has to be at least 8GB formatted in FAT32

  • Plug in your USB drive to your computer.
  • Open a PowerShell using the Run as Administrator option.
  • Change the path of the Windows Server 2019 ISO to your location
  • Replace the USB Friendly Name in the script with your USB stick name
# Define Path to the Windows Server 2019 ISO
$ISOFile = "C:\Temp\WindowsServer2019.iso"            
             
# Get the USB Drive you want to use, copy the friendly name            
Get-Disk | Where BusType -eq "USB"            
             
# Get the right USB Drive (You will need to change the FriendlyName)            
$USBDrive = Get-Disk | Where FriendlyName -eq "Kingston DT Workspace"            
             
# Replace the Friendly Name to clean the USB Drive (THIS WILL REMOVE EVERYTHING)            
$USBDrive | Clear-Disk -RemoveData -Confirm:$true -PassThru            
             
# Convert Disk to GPT            
$USBDrive | Initialize-Disk -PartitionStyle GPT            
             
# Create partition primary and format to FAT32            
$Volume = $USBDrive | New-Partition -UseMaximumSize -AssignDriveLetter | Format-Volume -FileSystem FAT32 -NewFileSystemLabel WS2019            
             
# Mount iso            
$ISOMounted = Mount-DiskImage -ImagePath $ISOFile -StorageType ISO -PassThru            
             
# Driver letter            
$ISODriveLetter = ($ISOMounted | Get-Volume).DriveLetter            
             
# Copy Files to USB             
Copy-Item -Path ($ISODriveLetter +":\*") -Destination ($Volume.DriveLetter + ":\") -Recurse            
             
# Dismount ISO
Dismount-DiskImage -ImagePath $ISOFile

If Install.wim is larger than 4GB, you cannot copy the file to the drive, because of the FAT32 based partition limitation. The solution for this is to split the wim file into smaller files.

Split wim file using dism (you may have to change the drive letters):

dism /Split-Image /ImageFile:e:\sources\install.wim /SWMFile:k:\sources\install.swm /FileSize:4096

After this completes manually copy the 2 files to the sources directory on the USB stick.

To create a USB stick to install Windows Server 2019 on a BIOS (MBR) system it has to be at least 8GB formatted in NTFS and the partition has be set active.

  • Plug in your USB drive to your computer.
  • Open a PowerShell using the Run as Administrator option.
  • Change the path of the Windows Server 2019 ISO to your location
  • Replace the USB Friendly Name in the script with your USB stick name

# Define Path to the Windows Server 2019 ISO            
$ISOFile = "C:\Temp\WindowsServer2019.iso"            
             
# Get the USB Drive you want to use, copy the friendly name            
Get-Disk | Where BusType -eq "USB"            
             
# Get the right USB Drive (You will need to change the FriendlyName)            
$USBDrive = Get-Disk | Where FriendlyName -eq "Kingston DT Workspace"            
             
# Replace the Friendly Name to clean the USB Drive (THIS WILL REMOVE EVERYTHING)            
$USBDrive | Clear-Disk -RemoveData -Confirm:$true -PassThru            
             
# Convert Disk to MBR            
$USBDrive | Initialize-Disk -PartitionStyle MBR            
             
# Create partition primary and format to NTFS            
$Volume = $USBDrive | New-Partition -UseMaximumSize -AssignDriveLetter | Format-Volume -FileSystem NTFS -NewFileSystemLabel WS2019            
             
# Set Partiton to Active            
$Volume | Get-Partition | Set-Partition -IsActive $true            
             
# Mount ISO            
$ISOMounted = Mount-DiskImage -ImagePath $ISOFile -StorageType ISO -PassThru            
             
# Driver letter            
$ISODriveLetter = ($ISOMounted | Get-Volume).DriveLetter            
             
# Copy Files to USB            
Copy-Item -Path ($ISODriveLetter +":\*") -Destination ($Volume.DriveLetter + ":\") -Recurse            
             
# Dismount ISO            
Dismount-DiskImage -ImagePath $ISOFile

Source

1 comment:

  1. Dan Visan11/3/21 21:18

    Hi, the following command returns an error. Can you explain a bit. Thanks.

    Copy-Item -Path ($ISODriveLetter +":\*") -Destination ($Volume.DriveLetter + ":\") -Recurse

    Copy-Item : The given path's format is not supported.
    At line:1 char:1
    + Copy-Item -Path ($ISODriveLetter +":\*") -Destination ($Volume.DriveL ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Copy-Item], NotSupportedException
    + FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.CopyItemCommand

    ReplyDelete