mdelatorre

Create a bootable USB flash drive Windows Server 2022

Oct 29th, 2025 (edited)
739
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Create a bootable USB flash drive in Win Srv 2022
  2. # https://t.ly/HzBuo
  3. # Save as winsrv2022_bootable.ps1
  4.  
  5.  
  6. # Select USB drive letter
  7. $usbDriveLetter = Read-Host "Enter USB drive letter (Ex: E)"
  8.  
  9. # Format USB drive
  10. Format-Volume -DriveLetter $usbDriveLetter -FileSystem NTFS -NewFileSystemLabel "WinServerUSB" -Confirm:$false | Out-Null
  11.  
  12. # Select ISO file mount point
  13. $isoMountPointDriveLetter = Read-Host "Enter ISO mount point drive letter (Ex: F)"
  14.  
  15. # Copy ISO files to USB drive
  16. $source = "$($isoMountPointDriveLetter):\"
  17. $destination = "$($usbDriveLetter):\"
  18. robocopy $source $destination /COPYALL /Z /E /SEC /R:3 /W:3
  19.  
  20. # Make USB drive bootable
  21. $usbDriveNumber = (Get-WmiObject -Class Win32_DiskDrive | Where-Object {$_.InterfaceType -eq "USB" -and $_.DeviceID -like "*$usbDriveLetter"}).Index
  22. bootsect /nt60 $usbDriveLetter`:
  23.  
  24. # Task completion notification
  25. Write-Host "USB Creation Complete!"
  26. Start-Sleep -Seconds 2
  27.  
  28. #------------------------------------------------------------------------------------------------
  29. # Error with script policy not letting it run
  30.  
  31. The following will allow all local scripts to execute on the VM, irrespective of whether they're signed or not:
  32. Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
Advertisement
Add Comment
Please, Sign In to add comment