Advertisement
LaDEEKill3R

Win10 Setup Disk 2.6 Re-Upload by ITGroceries

Feb 18th, 2022
751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 9.73 KB | None | 0 0
  1. <# : standard way of doing hybrid batch + powershell scripts
  2. @title Win10+ Setup Disk 2.6 &color 3E
  3. @set "__ARGS__=%*" &powershell -noprofile "$ScriptPath='%~f0';iex((Get-Content('%~f0') -Raw))" &exit/b
  4. #>
  5. #
  6. #   Homepage: https://forums.mydigitallife.net/threads/win10-setup-disk-works-with-uefi-with-secure-boot-bios-install-wim-over-4-gb.79268/
  7. #   Credits: @rpo, @freddie-o, @BAU & @abbodi1406
  8. #
  9. #   This script must be executed with admin privilege
  10. #
  11. #   Test Administrator privileges
  12. If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
  13. {
  14.     $uac_error="Elevating UAC for Administrator Privileges failed"
  15.     #   The following test is to avoid infinite looping if elevating UAC for Administrator Privileges failed
  16.     if ($env:__ARGS__ -eq $uac_error) {
  17.     read-host "$uac_error`r`nRight click on the script and select ""Run as administrator""`r`nPress Enter to exit...";exit}
  18. #   Restart the script to get Administrator privileges and exit
  19.     Start-Process "$ScriptPath" $uac_error -Verb runAs; exit
  20. }
  21. $pswindow = $host.ui.rawui          # create a reference to the console’s UI.RawUI child object
  22. $pswindow.windowtitle = ([string]($pswindow.windowtitle -split "^.*  ")).trim() ; $ShortTitle=($pswindow.windowtitle -split " @ .*")[0]
  23. #
  24. Add-Type -AssemblyName System.Windows.Forms # Load the class System.Windows.Forms
  25. # Filebrowser dialog object
  26. $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
  27.     Multiselect = $false # One file can be chosen
  28.     Filter = 'ISO images (*.iso)|*.iso' # Select only iso files
  29. }
  30. #
  31. Function MsgBox {[System.Windows.Forms.MessageBox]::Show($Args[0],$ShortTitle,$Args[1],$Args[2])}
  32. #
  33. Function Update_bcd
  34. {
  35. param ($iso, $usbpartition)
  36. "Set bootmenupolicy Legacy for $usbpartition"
  37. Copy-Item "$iso\boot\bcd" "$usbpartition\boot\bcd" -Force
  38. Copy-Item "$iso\EFI\Microsoft\boot\bcd" "$usbpartition\EFI\Microsoft\boot\bcd" -Force
  39. & bcdedit /store "$usbpartition\boot\bcd" /set '{default}' bootmenupolicy Legacy |Out-Null
  40. & bcdedit /store "$usbpartition\EFI\Microsoft\boot\bcd" /set '{default}' bootmenupolicy Legacy |Out-Null
  41. Set-ItemProperty -Path "$usbpartition\boot\bcd" -Name IsReadOnly -Value $true
  42. Set-ItemProperty -Path "$usbpartition\EFI\Microsoft\boot\bcd" -Name IsReadOnly -Value $true
  43. remove-item "$usbpartition\boot\bcd.*" -force
  44. remove-item "$usbpartition\EFI\Microsoft\boot\bcd.*" -force
  45. }
  46. #
  47. clear-host
  48. $USB=$Null
  49. $Form=New-Object System.Windows.Forms.Form # Create the screen form (window)
  50. $Form.TopMost = $True
  51. # Set the title and size of the window:
  52. $Form.Text=$ShortTitle
  53. $Form.Font='Consolas,10'
  54. $Form.Width=420 ; $Form.Height=270
  55. $Form.StartPosition = "CenterScreen"
  56. $Form.SizeGripStyle = "Hide"
  57. [System.Windows.Forms.Application]::EnableVisualStyles();
  58.  
  59. # Add a Source Windows ISO label on the form
  60. $SourceISO=New-Object System.Windows.Forms.Label
  61. $SourceISO.Location=New-Object System.Drawing.Point(20,30)                 
  62. $SourceISO.Size=New-Object System.Drawing.Size(200,20)
  63. $SourceISO.text="Source Windows ISO"
  64. $Form.Controls.Add($SourceISO)
  65.  
  66. # Add the ISO file name on the form
  67. $ISOFile=New-Object System.Windows.Forms.Label
  68. $ISOFile.Location=New-Object System.Drawing.Point(20,50)
  69. $ISOFile.Text=" "
  70. $ISOFile.Size=New-Object System.Drawing.Size(364,24)
  71. $ISOFile.Backcolor = [System.Drawing.Color]::White
  72. $ISOFile.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle
  73. $Form.Controls.Add($ISOFile)
  74.  
  75. # Add a Target USB Disk label on the form
  76. $TargetUSB=New-Object System.Windows.Forms.Label
  77. $TargetUSB.Location=New-Object System.Drawing.Point(20,100)
  78. $TargetUSB.Text="Target USB Disk"
  79. $TargetUSB.Size=New-Object System.Drawing.Size(200,20)
  80. $Form.Controls.Add($TargetUSB)
  81.  
  82. # Create a USB Disk drop-down list and fill it
  83. $USBDiskList=New-Object System.Windows.Forms.ComboBox
  84. $USBDiskList.DropDownStyle=[System.Windows.Forms.ComboBoxStyle]::DropDownList;
  85. $USBDiskList.Location=New-Object System.Drawing.Point(20,120)
  86. $USBDiskList.Size=New-Object System.Drawing.Size(363,22)
  87. $Form.Controls.Add($SelectUSBDiskList)
  88. $USBDisks=@() # array with USB disk number
  89. #
  90. $Disks=$Null;While (!$Disks){
  91.     If(!($Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")})){
  92.     If((MsgBox "Plug in your USB disk first. `n`nClick OK to continue." "OKCancel" "Information") -eq "Cancel") {exit}}}
  93.  
  94. # Add a Create Setup Disk button on the form
  95. $CreateDiskButton=New-Object System.Windows.Forms.Button
  96. $CreateDiskButton.Location=New-Object System.Drawing.Point(20,185)
  97. $CreateDiskButton.Text="Create Setup Disk"
  98. $CreateDiskButton.Size=New-Object System.Drawing.Size(155,26)
  99. $Form.Controls.Add($CreateDiskButton)
  100. $CreateDiskButton.Enabled = $false
  101. $CreateDiskButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
  102.  
  103. # Add a Select ISO button on the form
  104. $SelectISOButton=New-Object System.Windows.Forms.Button
  105. $SelectISOButton.Location=New-Object System.Drawing.Point(185,185)
  106. $SelectISOButton.Text="Select ISO"                           
  107. $SelectISOButton.Size=New-Object System.Drawing.Size(110,26)
  108. $Form.Controls.Add($SelectISOButton)
  109.  
  110. # Add a Cancel button on the form
  111. $CancelButton=New-Object System.Windows.Forms.Button
  112. $CancelButton.Location=New-Object System.Drawing.Point(305,185)
  113. $CancelButton.Text="Cancel"                        
  114. $CancelButton.Size=New-Object System.Drawing.Size(80,26)
  115. $Form.Controls.Add($CancelButton)
  116. $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
  117. #
  118. $SelectISOButton.Add_Click({
  119.     If ($FileBrowser.ShowDialog() -ne "Cancel"){ # if Cancel, just ignore
  120.         $Global:ImagePath = $FileBrowser.filename    # return the file name
  121.         $ISOFile.Text= Split-Path -Path $ImagePath -leaf # extract filename and extension (iso)
  122.         if (($ISOFile.Text).length -gt 44) {$ISOFile.Text = $ImagePath.PadRight(100," ").substring(0,43)+"..."}
  123.         $CreateDiskButton.Enabled = $true
  124.         $CreateDiskButton.Focus()}})
  125.  
  126. Foreach ($USBDisk in $Disks) {
  127.     $FriendlyName=($USBDisk.FriendlyName).PadRight(40," ").substring(0,39)
  128.     $USBDisks+=$USBDisk.DiskNumber
  129.     $USBDiskList.Items.Add(("{0,-39}{1:n2} GB" -f $FriendlyName,($USBDisk.Size/1GB)))|out-null
  130.     $Partitions = Get-Partition | Where-Object { $_.DiskNumber -eq $USBDisk.DiskNumber}
  131.     If ($Partitions) {
  132.         Foreach ($Partition in $Partitions) {
  133.         If(!$Partition.DriveLetter){$AccessPath="  "} Else {$AccessPath=$Partition.DriveLetter+":"}
  134.         $Volumes=get-volume | Where-Object {$Partition.AccessPaths -contains  $_.path }
  135.             Foreach ($Volume in $Volumes) {
  136.                 $USBDisks+=$USBDisk.DiskNumber
  137.                 $USBDiskList.Items.Add(("{0,0}({1,2}){2,-32}[{3:n2} GB]" -f " ", ($AccessPath), $Volume.FileSystemLabel.PadRight(25," ").substring(0,25), ($Partition.Size/1GB)))|out-null
  138.             }
  139.         }
  140.     }
  141. }
  142. $form.Controls.Add($USBDiskList)
  143. $USBDiskList.SelectedIndex=0
  144. #
  145. $Groupbox=New-Object System.Windows.Forms.Groupbox # Add a group box on the form
  146. $Groupbox.Location=New-Object System.Drawing.Point(7,0)
  147. $Groupbox.Text=""
  148. $Groupbox.Size=New-Object System.Drawing.Size(390,225)
  149. $Form.Controls.Add($Groupbox)
  150. #
  151. @"
  152. Select your Source Windows ISO and Target USB disk.
  153.  
  154. Click on the 'Create Setup Disk' button to begin creating your Windows setup disk.
  155. "@| Write-Host
  156. #
  157. if($form.ShowDialog() -eq "Cancel") {MsgBox "The script was cancelled." "OK" "Information"|out-Null;exit}
  158. #
  159. # At this point the mounted USB disk and the iso image file path are defined
  160. #
  161. $USB=$USBDisks[$USBDiskList.SelectedIndex]
  162. #
  163. If((MsgBox "WARNING! `n`nYour USB disk will be converted to MBR scheme, repartitioned and reformatted.`n`nAll partitions and data currently stored in the USB disk will be erased.`r`n`r`nAre you sure you want to continue?" "YESNO" "Warning") -eq "NO"){exit}
  164. # Mount the image file if not already mounted and get the drive letter
  165. #
  166. #   Check if iso already mounted by getting driveletter
  167. If($ISO=(Get-DiskImage -ImagePath $ImagePath | Get-Volume).DriveLetter){$Mounted=$True}    Else
  168. #    Mount iso and get drive letter
  169.     {$Mounted=$False; Mount-DiskImage -ImagePath $ImagePath -StorageType ISO |out-null
  170.      If(!($ISO=(Get-DiskImage -ImagePath $ImagePath | Get-Volume).DriveLetter)){Exit}}
  171. $ISO=$ISO+":"
  172. #
  173. Clear-Host
  174. #    Clear the USB disk
  175. @"
  176. Select disk $USB
  177. clean
  178. convert MBR
  179. rescan
  180. exit
  181. "@ | diskpart
  182. Stop-Service ShellHWDetection -erroraction silentlycontinue|out-null
  183. #   Create the fat32 boot partition
  184. $usbfat32=(New-Partition -DiskNumber $usb -Size  1GB -AssignDriveLetter | Format-Volume -FileSystem FAT32 -NewFileSystemLabel "BOOT").DriveLetter + ":"
  185. #   Copy needed files to the fat32 boot partition
  186. robocopy $iso"\" $usbfat32"\" bootmgr bootmgr.efi
  187. robocopy $iso"\boot" $usbfat32"\boot" /e /xf $iso\boot\bcd
  188. robocopy $iso"\efi" $usbfat32"\efi" /e /xf $iso\efi\microsoft\boot\bcd
  189. robocopy $iso"\sources" $usbfat32"\sources" boot.wim
  190. Update_BCD $iso $usbfat32
  191. #   Remove the drive letter to hide the device
  192. Get-Volume -DriveLetter $usbfat32.substring(0,$usbfat32.Length-1) | Get-Partition | Remove-PartitionAccessPath -accesspath $usbfat32
  193. #    Create the NTFS intall partition
  194. $usbntfs=(New-Partition -DiskNumber $usb -UseMaximumSize -AssignDriveLetter -IsActive | Format-Volume -FileSystem NTFS -NewFileSystemLabel "SETUP").DriveLetter + ":"
  195. Start-Service ShellHWDetection -erroraction silentlycontinue|out-null
  196. #   Copy needed files to the NTFS install partition
  197. robocopy $iso $usbntfs /e /xf $iso\boot\bcd $iso\efi\microsoft\boot\bcd
  198. Update_BCD $iso $usbntfs                                                                     
  199. # Eject the mounted iso image if it was loaded by the script
  200. If(!$Mounted){DisMount-DiskImage -ImagePath $ImagePath |out-null}
  201. MsgBox (($ShortTitle -split " @.*")[0]+" was created successfully.") "OK" "Information" |Out-Null;exit
  202.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement