Advertisement
mdelatorre

Windows Server 2022 USB Installation from ISO

Jun 30th, 2022
1,581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Open a PowerShell using the Run as Administrator option. You will need to change the path of the Windows Server 2022 ISO, and you will need to replace the disk number in the script before running the third command and make sure C:\Temp exists
  2.  
  3. # Define Path to the Windows Server 2022 ISO
  4. $ISOFile = "C:\Temp\WindowsServer2022.iso"
  5.  
  6. # Create temp diectroy for new image
  7. $newImageDir = New-Item -Path 'C:\Temp\newimage' -ItemType Directory
  8.  
  9. # Mount iso
  10. $ISOMounted = Mount-DiskImage -ImagePath $ISOFile -StorageType ISO -PassThru
  11.  
  12. # Driver letter
  13. $ISODriveLetter = ($ISOMounted | Get-Volume).DriveLetter
  14.  
  15. # Copy Files to temporary new image folder
  16. Copy-Item -Path ($ISODriveLetter +":\*") -Destination C:\Temp\newimage -Recurse
  17.  
  18. # Split and copy install.wim (because of the filesize)
  19. dism /Split-Image /ImageFile:C:\Temp\newimage\sources\install.wim /SWMFile:C:\Temp\newimage\sources\install.swm /FileSize:4096
  20.  
  21.  
  22. # Get the USB Drive you want to use, copy the disk number
  23. Get-Disk | Where BusType -eq "USB"
  24.  
  25. # Get the right USB Drive (You will need to change the number)
  26. $USBDrive = Get-Disk | Where Number -eq 2
  27.  
  28. # Replace the Friendly Name to clean the USB Drive (THIS WILL REMOVE EVERYTHING)
  29. $USBDrive | Clear-Disk -RemoveData -Confirm:$true -PassThru
  30.  
  31. # Convert Disk to GPT
  32. $USBDrive | Set-Disk -PartitionStyle GPT
  33.  
  34. # Create partition primary and format to FAT32
  35. $Volume = $USBDrive | New-Partition -Size 8GB -AssignDriveLetter | Format-Volume -FileSystem FAT32 -NewFileSystemLabel WS2022
  36.  
  37. # Copy Files to USB (Ignore install.wim)
  38. Copy-Item -Path C:\Temp\newimage\* -Destination ($Volume.DriveLetter + ":\") -Recurse -Exclude install.wim
  39.  
  40. # Dismount ISO
  41. Dismount-DiskImage -ImagePath $ISOFile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement