Created
October 29, 2025 18:08
-
-
Save NickSlash/8be79d3403eb26c116d4cc9435bad183 to your computer and use it in GitHub Desktop.
Create a windows installation USB with two partitions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Requires -RunAsAdministrator | |
| param( | |
| [String]$image=(Join-Path $PSScriptRoot "Win10_2004_EnglishInternational_x64.iso"), | |
| [Switch]$update | |
| ) | |
| If ( -Not $update.IsPresent) { | |
| If ( -Not (Test-Path $image)) { | |
| Write-Host "Error: <image> not found" | |
| Exit 1 | |
| } | |
| $drives = Get-Disk | Select-Object -Property Number,FriendlyName | |
| Write-Host "Available Devices" | |
| $drives | ForEach-Object { | |
| Write-Host ("{0} : {1}" -f $_.Number.toString().PadLeft(2), $_.FriendlyName) | |
| } | |
| $choice = Read-Host -Prompt "Selection" | |
| If ($choice -match "^[\d\.]+$") { | |
| If ( (Read-Host -Prompt "Confirm Selection") -ne $choice) { | |
| Write-Host "Error: Failed to Confirm Selection" | |
| Exit 1 | |
| } | |
| } else { | |
| Write-Host "Error: Invalid Selection" | |
| Exit 1 | |
| } | |
| [int]$choice = $choice | |
| $gpt = $true | |
| If ( (Get-Disk | Where-Object { $_.Number -eq $choice}).PartitionStyle -eq "MBR" ) { | |
| $gpt = $false | |
| } | |
| $partition = Join-Path $PSScriptRoot "partition.txt" | |
| If (Test-Path $partition -PathType Leaf) { Remove-Item $partition -Force | Out-Null } | |
| New-Item -Path $PSScriptRoot -Name "partition.txt" -ItemType File | Out-Null | |
| Add-Content -Path $partition -Value "select disk $choice" | |
| Add-Content -Path $partition -Value "clean" | |
| If ( $gpt -eq $true ) { | |
| Add-Content -Path $partition -Value "convert mbr" | |
| } | |
| Add-Content -Path $partition -Value "create partition primary size=1024" | |
| Add-Content -Path $partition -Value "format quick fs=fat32 label=""USB-BOOT""" | |
| Add-Content -Path $partition -Value "assign" | |
| Add-Content -Path $partition -Value "create partition primary" | |
| Add-Content -Path $partition -Value "format quick fs=ntfs label=""USB-INSTALL""" | |
| Add-Content -Path $partition -Value "assign" | |
| Add-Content -Path $partition -Value "exit" | |
| Write-Host " > Repartitioning Disk" | |
| diskpart /s $partition | Out-Null | |
| Remove-Item $partition -Force | Out-Null | |
| $install = $null | |
| $boot = $null | |
| $count = 0 | |
| While (($null -eq $install) -or ($null -eq $boot)) { | |
| Write-Host " - Waiting for Disk" | |
| Get-Volume | ForEach-Object { | |
| If ($_.FileSystemLabel -eq "USB-INSTALL") { | |
| $install = ("{0}:" -f $_.DriveLetter) | |
| } | |
| If ($_.FileSystemLabel -eq "USB-BOOT") { | |
| $boot = ("{0}:" -f $_.DriveLetter) | |
| } | |
| } | |
| If ($count -gt 10) { break } else { $count = $count + 1 } | |
| Start-Sleep -Seconds 2 | |
| } | |
| If (($null -eq $install) -and ($null -eq $boot)) { | |
| Write-Host "Error: cant not find <USB-BOOT> or <USB-INSTALL>" | |
| Exit 1 | |
| } | |
| Write-Host " > Updating Boot Sector" | |
| bootsect.exe /nt60 $boot | Out-Null | |
| $iso = "{0}:" -f (Mount-DiskImage -ImagePath $image -PassThru | Get-Volume).DriveLetter | |
| Write-Host " > Copying <USB-BOOT> Files" | |
| $excludes = "sources" | |
| Get-ChildItem -Path $iso | Where-Object{$_.Name -notin $excludes} | Copy-Item -Destination $boot -Recurse -Force | Out-Null | |
| New-Item -Path $boot -Name "sources" -ItemType Directory | Out-Null | |
| Copy-Item -Path (Join-Path (Join-Path $iso "sources") "boot.wim") -Destination (Join-Path $boot "sources") | Out-Null | |
| Write-Host " > Copying <USB-INSTALL> Files (slow)" | |
| $excludes = @("install.wim", "install.esd") | |
| Get-ChildItem -Path $iso -Recurse -Exclude $excludes | Copy-Item -Destination { Join-Path $install $_.FullName.SubString($iso.length) } -Force | Out-Null | |
| Write-Host " > Copying <install.wim> to <USB-INSTALL> (slow)" | |
| Copy-Item -Path (Join-Path $PSScriptRoot "install.wim") -Destination (Join-Path $install "sources") | Out-Null | |
| } else { | |
| $install = Get-Volume | Where-Object { $_.FileSystemLabel -eq "USB-INSTALL" } | |
| If ( ($install | Measure-Object).Count -eq 1) { | |
| $install = "{0}:\" -f $install.DriveLetter | |
| } else { | |
| Write-Host "Error: cant find <USB-INSTALL>" | |
| Exit 1 | |
| } | |
| Write-Host " > Copying <install.wim> to <USB-INSTALL> (slow)" | |
| Copy-Item -Path (Join-Path $PSScriptRoot "install.wim") -Destination (Join-Path $install "sources") -Force | Out-Null | |
| } | |
| Write-Host "Complete." |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
to use this script you need:
place all files in the same folder and execute the script from an elevated powershell prompt.
the script takes two parameters, both optional.
$image : an absolute path to your iso
$update : if you have already configured the partitions, this will only copy install.wim.