Win10+ Setup Disk (Works with UEFI Secure Boot / BIOS / Install.wim over 4 GB)

Discussion in 'MDL Projects and Applications' started by rpo, Mar 18, 2019.

  1. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,605
    2,676
    60
    For one, if you have Secure boot enabled, you won't have to disable it first temporarily to install Windows
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. kyrcy

    kyrcy MDL Member

    Feb 25, 2020
    241
    15
    10
    I don't, but I would still like to know under what scenario could I use the script.
     
  3. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,605
    2,676
    60
    I just told you. Read the OP.
    If it doesn't apply to your scenario then you don't need the script
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. rpo

    rpo MDL Expert

    Jan 3, 2010
    1,503
    1,512
    60
    If your PC can boot from a NTFS formatted USB drive, you don't need this script; all depends on the firmware of your PC.
     
  5. rpo

    rpo MDL Expert

    Jan 3, 2010
    1,503
    1,512
    60
    #405 rpo, Jan 29, 2025
    Last edited: Jan 29, 2025
    (OP)
    The implemented correction is incomplete. Change lines 172-173 by :
    Code:
    Try{Clear-Disk $USB -RemoveData -RemoveOEM -Confirm:$False -ErrorAction Stop
        Set-Disk $USB -PartitionStyle MBR  -ErrorAction Stop} 
    
    (add -ErrorAction Stop)

    The complete updated script :
    Code:
    <# : standard way of doing hybrid batch + powershell scripts
    @title Win10+ Setup Disk 2.8 &color 3E
    @powershell -noprofile "$param='%*';$ScriptPath='%~f0';iex((Get-Content('%~f0') -Raw))" &exit/b
    #>
    #
    #   Homepage: https://forums.mydigitallife.net/threads/win10-setup-disk-works-with-uefi-with-secure-boot-bios-install-wim-over-4-gb.79268/
    #   Credits: @rpo, @freddie-o, @BAU & @abbodi1406
    #
    #   This script must be executed with admin privilege
    #
    #   Test Administrator privileges
    If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
    {
        $uac_error="Elevating UAC for Administrator Privileges failed"
        #   The following test is to avoid infinite looping if elevating UAC for Administrator Privileges failed
        if ($param -eq $uac_error) {
        read-host "$uac_error`r`nRight click on the script and select ""Run as administrator""`r`nPress Enter to exit...";exit}
    #   Restart the script to get Administrator privileges and exit
        Start-Process "$ScriptPath" $uac_error -Verb runAs; exit
    }
    $pswindow = $host.ui.rawui          # create a reference to the console’s UI.RawUI child object
    $pswindow.windowtitle = ([string]($pswindow.windowtitle -split "^.*  ")).trim() ; $ShortTitle=($pswindow.windowtitle -split " @ .*")[0]
    #
    Add-Type -AssemblyName System.Windows.Forms # Load the class System.Windows.Forms
    # Filebrowser dialog object
    $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
        Multiselect = $false # One file can be chosen
        Filter = 'ISO images (*.iso)|*.iso' # Select only iso files
    }
    #
    Function MsgBox {[System.Windows.Forms.MessageBox]::Show($Args[0],$ShortTitle,$Args[1],$Args[2])}
    #
    Function Update_bcd ($usbpartition)
    {
    "Set bootmenupolicy Legacy for $usbpartition"
    & bcdedit /store "$usbpartition\boot\bcd" /set '{default}' bootmenupolicy Legacy |Out-Null
    & bcdedit /store "$usbpartition\EFI\Microsoft\boot\bcd" /set '{default}' bootmenupolicy Legacy |Out-Null
    remove-item "$usbpartition\boot\bcd.*" -force
    remove-item "$usbpartition\EFI\Microsoft\boot\bcd.*" -force
    }
    #
    clear-host
    $USB=$Null
    $Form=New-Object System.Windows.Forms.Form # Create the screen form (window)
    $Form.TopMost = $True
    # Set the title and size of the window:
    $Form.Text=$ShortTitle
    $Form.Font='Consolas,10'
    $Form.Width=420 ; $Form.Height=270
    $Form.StartPosition = "CenterScreen"
    $Form.SizeGripStyle = "Hide"
    [System.Windows.Forms.Application]::EnableVisualStyles();
    
    # Add a Source Windows ISO label on the form
    $SourceISO=New-Object System.Windows.Forms.Label
    $SourceISO.Location=New-Object System.Drawing.Point(20,30)                   
    $SourceISO.Size=New-Object System.Drawing.Size(200,20)
    $SourceISO.text="Source Windows ISO"
    $Form.Controls.Add($SourceISO)
    
    # Add the ISO file name on the form
    $ISOFile=New-Object System.Windows.Forms.Label
    $ISOFile.Location=New-Object System.Drawing.Point(20,50)
    $ISOFile.Text=" "
    $ISOFile.Size=New-Object System.Drawing.Size(364,24)
    $ISOFile.Backcolor = [System.Drawing.Color]::White
    $ISOFile.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle
    $Form.Controls.Add($ISOFile)
    
    # Add a Target USB Disk label on the form
    $TargetUSB=New-Object System.Windows.Forms.Label
    $TargetUSB.Location=New-Object System.Drawing.Point(20,100)
    $TargetUSB.Text="Target USB Disk"
    $TargetUSB.Size=New-Object System.Drawing.Size(200,20)
    $Form.Controls.Add($TargetUSB)
    
    # Create a USB Disk drop-down list and fill it
    $USBDiskList=New-Object System.Windows.Forms.ComboBox
    $USBDiskList.DropDownStyle=[System.Windows.Forms.ComboBoxStyle]::DropDownList;
    $USBDiskList.Location=New-Object System.Drawing.Point(20,120)
    $USBDiskList.Size=New-Object System.Drawing.Size(363,22)
    $Form.Controls.Add($SelectUSBDiskList)
    $USBDisks=@() # array with USB disk number
    #
    $Disks=$Null;While (!$Disks){
        If(!($Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")})){
        If((MsgBox "Plug in your USB disk first. `n`nClick OK to continue." "OKCancel" "Information") -eq "Cancel") {exit}}}
    
    # Add a Select ISO button on the form
    $SelectISOButton=New-Object System.Windows.Forms.Button
    $SelectISOButton.Location=New-Object System.Drawing.Point(20,185)
    $SelectISOButton.Text="Select ISO"                             
    $SelectISOButton.Size=New-Object System.Drawing.Size(110,26)
    $Form.Controls.Add($SelectISOButton)
    
    # Add a Create Setup Disk button on the form
    $CreateDiskButton=New-Object System.Windows.Forms.Button
    $CreateDiskButton.Location=New-Object System.Drawing.Point(140,185)
    $CreateDiskButton.Text="Create Setup Disk"
    $CreateDiskButton.Size=New-Object System.Drawing.Size(155,26)
    $Form.Controls.Add($CreateDiskButton)
    $CreateDiskButton.Enabled = $false
    $CreateDiskButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    
    # Add a Cancel button on the form
    $CancelButton=New-Object System.Windows.Forms.Button
    $CancelButton.Location=New-Object System.Drawing.Point(305,185)
    $CancelButton.Text="Cancel"                           
    $CancelButton.Size=New-Object System.Drawing.Size(80,26)
    $Form.Controls.Add($CancelButton)
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    #
    $SelectISOButton.Add_Click({
        If ($FileBrowser.ShowDialog() -ne "Cancel"){ # if Cancel, just ignore
            $Global:ImagePath = $FileBrowser.filename    # return the file name
            $ISOFile.Text= Split-Path -Path $ImagePath -leaf # extract filename and extension (iso)
            if (($ISOFile.Text).length -gt 44) {$ISOFile.Text = $ImagePath.PadRight(100," ").substring(0,43)+"..."}
            $CreateDiskButton.Enabled = $true
            $CreateDiskButton.Focus()}})
    
    Foreach ($USBDisk in $Disks) {
        $FriendlyName=($USBDisk.FriendlyName).PadRight(40," ").substring(0,39)
        $USBDisks+=$USBDisk.DiskNumber
        $USBDiskList.Items.Add(("{0,-39}{1:n2} GB" -f $FriendlyName,($USBDisk.Size/1GB)))|out-null
        If ($Partitions) {
            Foreach ($Partition in $Partitions) {
            If(!$Partition.DriveLetter){$AccessPath="  "} Else {$AccessPath=$Partition.DriveLetter+":"}
            $Volumes=get-volume | Where-Object {$Partition.AccessPaths -contains  $_.path }
                Foreach ($Volume in $Volumes) {
                    $USBDisks+=$USBDisk.DiskNumber
                    $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
                }
            }
        }
    }
    $form.Controls.Add($USBDiskList)
    $USBDiskList.SelectedIndex=0
    #
    $Groupbox=New-Object System.Windows.Forms.Groupbox # Add a group box on the form
    $Groupbox.Location=New-Object System.Drawing.Point(7,0)
    $Groupbox.Text=""
    $Groupbox.Size=New-Object System.Drawing.Size(390,225)
    $Form.Controls.Add($Groupbox)
    #
    @"
    1.) Select "Source Windows ISO"
    
    2.) Select "Target USB disk"
    
    3.) Start "Create Setup Disk"
    "@| Write-Host
    #
    if($form.ShowDialog() -eq "Cancel") {MsgBox "The script was cancelled." "OK" "Information"|out-Null;exit}
    #
    # At this point the mounted USB disk and the iso image file path are defined
    #
    $USB=$USBDisks[$USBDiskList.SelectedIndex]
    #
    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}
    # Mount the image file if not already mounted and get the drive letter
    #
    #   Check if iso already mounted by getting driveletter
    If($ISO=(Get-DiskImage -ImagePath $ImagePath | Get-Volume).DriveLetter){$Mounted=$True}    Else
    #    Mount iso and get drive letter
        {$Mounted=$False; Mount-DiskImage -ImagePath $ImagePath -StorageType ISO |out-null
         If(!($ISO=(Get-DiskImage -ImagePath $ImagePath | Get-Volume).DriveLetter)){Exit}}
    $ISO=$ISO+":"
    #
    Clear-Host
    Stop-Service ShellHWDetection -erroraction silentlycontinue|out-null
    #    Clear the USB disk
    Try{Clear-Disk $USB -RemoveData -RemoveOEM -Confirm:$False -ErrorAction Stop
        Set-Disk $USB -PartitionStyle MBR -ErrorAction Stop}
    Catch{"Select disk $USB`nclean`nconvert MBR`nrescan`nexit`n"|diskpart}
    #   Create the fat32 boot partition
    $usbfat32=(New-Partition -DiskNumber $usb -Size  1GB -AssignDriveLetter | Format-Volume -FileSystem FAT32 -NewFileSystemLabel "BOOT").DriveLetter + ":"
    #    Create the NTFS intall partition
    $usbntfs=(New-Partition -DiskNumber $usb -UseMaximumSize -AssignDriveLetter -IsActive | Format-Volume -FileSystem NTFS -NewFileSystemLabel "SETUP").DriveLetter + ":"
    Start-Service ShellHWDetection -erroraction silentlycontinue|out-null
    #    Copy needed files to the fat32 boot partition
    robocopy $iso"\" $usbfat32"\" bootmgr bootmgr.efi /a-:R
    robocopy $iso"\boot" $usbfat32"\boot" /e /a-:R
    robocopy $iso"\efi" $usbfat32"\efi" /e /a-:R
    robocopy $iso"\sources" $usbfat32"\sources" boot.wim /a-:R
    Update_BCD $usbfat32
    #     Remove the drive letter to hide the device
    Get-Volume -DriveLetter $usbfat32.substring(0,$usbfat32.Length-1) | Get-Partition | Remove-PartitionAccessPath -accesspath $usbfat32
    #    Copy needed files to the NTFS install partition
    robocopy $iso $usbntfs /e   /a-:R
    Update_BCD $usbntfs                                                                     
    # Eject the mounted iso image if it was loaded by the script
    If(!$Mounted){DisMount-DiskImage -ImagePath $ImagePath |out-null}
    MsgBox (($ShortTitle -split " @.*")[0]+" was created successfully.") "OK" "Information" |Out-Null;exit
    
     
  6. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,605
    2,676
    60





    :good3::thumbsup:

    Updated the script (Win10+ Setup Disk 3.0) and OP
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. rpo

    rpo MDL Expert

    Jan 3, 2010
    1,503
    1,512
    60
    @freddie-o.
    I just remarked that the partitions informations of the usb disks are no longer displayed : an instriction after line 124 is missing (intentionnaly dropped?).
    To restore the displays, use the following version of the script :
    Code:
    <# : standard way of doing hybrid batch + powershell scripts
    @title Win10+ Setup Disk 3.1 &color 3E
    @powershell -noprofile "$param='%*';$ScriptPath='%~f0';iex((Get-Content('%~f0') -Raw))" &exit/b
    #>
    #
    #   Homepage: https://forums.mydigitallife.net/threads/win10-setup-disk-works-with-uefi-with-secure-boot-bios-install-wim-over-4-gb.79268/
    #   Credits: @rpo, @freddie-o, @BAU & @abbodi1406
    #
    #   This script must be executed with admin privilege
    #
    #   Test Administrator privileges
    If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
    {
        $uac_error="Elevating UAC for Administrator Privileges failed"
        #   The following test is to avoid infinite looping if elevating UAC for Administrator Privileges failed
        if ($param -eq $uac_error) {
        read-host "$uac_error`r`nRight click on the script and select ""Run as administrator""`r`nPress Enter to exit...";exit}
    #   Restart the script to get Administrator privileges and exit
        Start-Process "$ScriptPath" $uac_error -Verb runAs; exit
    }
    $pswindow = $host.ui.rawui          # create a reference to the console’s UI.RawUI child object
    $pswindow.windowtitle = ([string]($pswindow.windowtitle -split "^.*  ")).trim() ; $ShortTitle=($pswindow.windowtitle -split " @ .*")[0]
    #
    Add-Type -AssemblyName System.Windows.Forms # Load the class System.Windows.Forms
    # Filebrowser dialog object
    $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
        Multiselect = $false # One file can be chosen
        Filter = 'ISO images (*.iso)|*.iso' # Select only iso files
    }
    #
    Function MsgBox {[System.Windows.Forms.MessageBox]::Show($Args[0],$ShortTitle,$Args[1],$Args[2])}
    #
    Function Update_bcd ($usbpartition)
    {
    "Set bootmenupolicy Legacy for $usbpartition"
    & bcdedit /store "$usbpartition\boot\bcd" /set '{default}' bootmenupolicy Legacy |Out-Null
    & bcdedit /store "$usbpartition\EFI\Microsoft\boot\bcd" /set '{default}' bootmenupolicy Legacy |Out-Null
    remove-item "$usbpartition\boot\bcd.*" -force
    remove-item "$usbpartition\EFI\Microsoft\boot\bcd.*" -force
    }
    #
    clear-host
    $USB=$Null
    $Form=New-Object System.Windows.Forms.Form # Create the screen form (window)
    $Form.TopMost = $True
    # Set the title and size of the window:
    $Form.Text=$ShortTitle
    $Form.Font='Consolas,10'
    $Form.Width=420 ; $Form.Height=270
    $Form.StartPosition = "CenterScreen"
    $Form.SizeGripStyle = "Hide"
    [System.Windows.Forms.Application]::EnableVisualStyles();
    
    # Add a Source Windows ISO label on the form
    $SourceISO=New-Object System.Windows.Forms.Label
    $SourceISO.Location=New-Object System.Drawing.Point(20,30)                   
    $SourceISO.Size=New-Object System.Drawing.Size(200,20)
    $SourceISO.text="Source Windows ISO"
    $Form.Controls.Add($SourceISO)
    
    # Add the ISO file name on the form
    $ISOFile=New-Object System.Windows.Forms.Label
    $ISOFile.Location=New-Object System.Drawing.Point(20,50)
    $ISOFile.Text=" "
    $ISOFile.Size=New-Object System.Drawing.Size(364,24)
    $ISOFile.Backcolor = [System.Drawing.Color]::White
    $ISOFile.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle
    $Form.Controls.Add($ISOFile)
    
    # Add a Target USB Disk label on the form
    $TargetUSB=New-Object System.Windows.Forms.Label
    $TargetUSB.Location=New-Object System.Drawing.Point(20,100)
    $TargetUSB.Text="Target USB Disk"
    $TargetUSB.Size=New-Object System.Drawing.Size(200,20)
    $Form.Controls.Add($TargetUSB)
    
    # Create a USB Disk drop-down list and fill it
    $USBDiskList=New-Object System.Windows.Forms.ComboBox
    $USBDiskList.DropDownStyle=[System.Windows.Forms.ComboBoxStyle]::DropDownList;
    $USBDiskList.Location=New-Object System.Drawing.Point(20,120)
    $USBDiskList.Size=New-Object System.Drawing.Size(363,22)
    $Form.Controls.Add($SelectUSBDiskList)
    $USBDisks=@() # array with USB disk number
    #
    $Disks=$Null;While (!$Disks){
        If(!($Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")})){
        If((MsgBox "Plug in your USB disk first. `n`nClick OK to continue." "OKCancel" "Information") -eq "Cancel") {exit}}}
    
    # Add a Select ISO button on the form
    $SelectISOButton=New-Object System.Windows.Forms.Button
    $SelectISOButton.Location=New-Object System.Drawing.Point(20,185)
    $SelectISOButton.Text="Select ISO"                             
    $SelectISOButton.Size=New-Object System.Drawing.Size(110,26)
    $Form.Controls.Add($SelectISOButton)
    
    # Add a Create Setup Disk button on the form
    $CreateDiskButton=New-Object System.Windows.Forms.Button
    $CreateDiskButton.Location=New-Object System.Drawing.Point(140,185)
    $CreateDiskButton.Text="Create Setup Disk"
    $CreateDiskButton.Size=New-Object System.Drawing.Size(155,26)
    $Form.Controls.Add($CreateDiskButton)
    $CreateDiskButton.Enabled = $false
    $CreateDiskButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    
    # Add a Cancel button on the form
    $CancelButton=New-Object System.Windows.Forms.Button
    $CancelButton.Location=New-Object System.Drawing.Point(305,185)
    $CancelButton.Text="Cancel"                           
    $CancelButton.Size=New-Object System.Drawing.Size(80,26)
    $Form.Controls.Add($CancelButton)
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    #
    $SelectISOButton.Add_Click({
        If ($FileBrowser.ShowDialog() -ne "Cancel"){ # if Cancel, just ignore
            $Global:ImagePath = $FileBrowser.filename    # return the file name
            $ISOFile.Text= Split-Path -Path $ImagePath -leaf # extract filename and extension (iso)
            if (($ISOFile.Text).length -gt 44) {$ISOFile.Text = $ImagePath.PadRight(100," ").substring(0,43)+"..."}
            $CreateDiskButton.Enabled = $true
            $CreateDiskButton.Focus()}})
    
    Foreach ($USBDisk in $Disks) {
        $FriendlyName=($USBDisk.FriendlyName).PadRight(40," ").substring(0,39)
        $USBDisks+=$USBDisk.DiskNumber
        $USBDiskList.Items.Add(("{0,-39}{1:n2} GB" -f $FriendlyName,($USBDisk.Size/1GB)))|out-null
        Foreach ($Partition in (Get-Partition $USBDisk.DiskNumber)) {
            If(!$Partition.DriveLetter){$AccessPath="  "} Else {$AccessPath=$Partition.DriveLetter+":"}         
            $USBDisks+=$USBDisk.DiskNumber
            $USBDiskList.Items.Add((" ({0,2}){1,-32}[{2:n2} GB]" -f $AccessPath,($Partition|Get-Volume).FileSystemLabel.PadRight(25," ").substring(0,25), ($Partition.Size/1GB)))|out-null
        }
    }
    $form.Controls.Add($USBDiskList)
    $USBDiskList.SelectedIndex=0
    #
    $Groupbox=New-Object System.Windows.Forms.Groupbox # Add a group box on the form
    $Groupbox.Location=New-Object System.Drawing.Point(7,0)
    $Groupbox.Text=""
    $Groupbox.Size=New-Object System.Drawing.Size(390,225)
    $Form.Controls.Add($Groupbox)
    #
    @"
    1.) Click "Select ISO" to select your source Windows ISO
    
    2.) Select your "Target USB disk" from the drop-down menu
    
    3.) Click "Create Setup Disk" to start creating your Windows setup disk
    "@| Write-Host
    #
    if($form.ShowDialog() -eq "Cancel") {MsgBox "The script was cancelled." "OK" "Information"|out-Null;exit}
    #
    # At this point the mounted USB disk and the iso image file path are defined
    #
    $USB=$USBDisks[$USBDiskList.SelectedIndex]
    #
    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}
    # Mount the image file if not already mounted and get the drive letter
    #
    #   Check if iso already mounted by getting driveletter
    If($ISO=(Get-DiskImage -ImagePath $ImagePath | Get-Volume).DriveLetter){$Mounted=$True}    Else
    #    Mount iso and get drive letter
        {$Mounted=$False; Mount-DiskImage -ImagePath $ImagePath -StorageType ISO |out-null
         If(!($ISO=(Get-DiskImage -ImagePath $ImagePath | Get-Volume).DriveLetter)){Exit}}
    $ISO=$ISO+":"
    #
    Clear-Host
    Stop-Service ShellHWDetection -erroraction silentlycontinue|out-null
    #    Clear the USB disk
    Try{Clear-Disk $USB -RemoveData -RemoveOEM -Confirm:$False -ErrorAction Stop
        Set-Disk $USB -PartitionStyle MBR -ErrorAction Stop}
    Catch{"Select disk $USB`nclean`nconvert MBR`nrescan`nexit`n"|diskpart}
    #   Create the fat32 boot partition
    $usbfat32=(New-Partition -DiskNumber $usb -Size  1GB -AssignDriveLetter | Format-Volume -FileSystem FAT32 -NewFileSystemLabel "BOOT").DriveLetter + ":"
    #    Create the NTFS intall partition
    $usbntfs=(New-Partition -DiskNumber $usb -UseMaximumSize -AssignDriveLetter -IsActive | Format-Volume -FileSystem NTFS -NewFileSystemLabel "SETUP").DriveLetter + ":"
    Start-Service ShellHWDetection -erroraction silentlycontinue|out-null
    #    Copy needed files to the fat32 boot partition
    robocopy $iso"\" $usbfat32"\" bootmgr bootmgr.efi /a-:R
    robocopy $iso"\boot" $usbfat32"\boot" /e /a-:R
    robocopy $iso"\efi" $usbfat32"\efi" /e /a-:R
    robocopy $iso"\sources" $usbfat32"\sources" boot.wim /a-:R
    Update_BCD $usbfat32
    #     Remove the drive letter to hide the device
    Get-Volume -DriveLetter $usbfat32.substring(0,$usbfat32.Length-1) | Get-Partition | Remove-PartitionAccessPath -accesspath $usbfat32
    #    Copy needed files to the NTFS install partition
    robocopy $iso $usbntfs /e   /a-:R
    Update_BCD $usbntfs                                                                     
    # Eject the mounted iso image if it was loaded by the script
    If(!$Mounted){DisMount-DiskImage -ImagePath $ImagePath |out-null}
    MsgBox (($ShortTitle -split " @.*")[0]+" was created successfully.") "OK" "Information" |Out-Null;exit
     
  8. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,605
    2,676
    60
    In v3.1 and previous versions there are 2 entries per disk

    1.png



    Whereas in v3.0 just 1 entry per disk. I think this looks simpler and less confusing.

    2.png
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. rpo

    rpo MDL Expert

    Jan 3, 2010
    1,503
    1,512
    60
    #409 rpo, Feb 18, 2025
    Last edited: Feb 18, 2025
    (OP)
    To keep the script clean lines 125-134 should be deleted. Or adopt the following version :
    Code:
    <# : standard way of doing hybrid batch + powershell scripts
    @title Win10+ Setup Disk 3.1 &color 3E
    @powershell -noprofile "$param='%*';$ScriptPath='%~f0';iex((Get-Content('%~f0') -Raw))" &exit/b
    #>
    #
    #   Homepage: https://forums.mydigitallife.net/threads/win10-setup-disk-works-with-uefi-with-secure-boot-bios-install-wim-over-4-gb.79268/
    #   Credits: @rpo, @freddie-o, @BAU & @abbodi1406
    #
    #   This script must be executed with admin privilege
    #
    #   Test Administrator privileges
    If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
    {
        $uac_error="Elevating UAC for Administrator Privileges failed"
        #   The following test is to avoid infinite looping if elevating UAC for Administrator Privileges failed
        if ($param -eq $uac_error) {
        read-host "$uac_error`r`nRight click on the script and select ""Run as administrator""`r`nPress Enter to exit...";exit}
    #   Restart the script to get Administrator privileges and exit
        Start-Process "$ScriptPath" $uac_error -Verb runAs; exit
    }
    $pswindow = $host.ui.rawui          # create a reference to the console’s UI.RawUI child object
    $pswindow.windowtitle = ([string]($pswindow.windowtitle -split "^.*  ")).trim() ; $ShortTitle=($pswindow.windowtitle -split " @ .*")[0]
    #
    Add-Type -AssemblyName System.Windows.Forms # Load the class System.Windows.Forms
    # Filebrowser dialog object
    $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
        Multiselect = $false # One file can be chosen
        Filter = 'ISO images (*.iso)|*.iso' # Select only iso files
    }
    #
    Function MsgBox {[System.Windows.Forms.MessageBox]::Show($Args[0],$ShortTitle,$Args[1],$Args[2])}
    #
    Function Update_bcd ($usbpartition)
    {
    "Set bootmenupolicy Legacy for $usbpartition"
    & bcdedit /store "$usbpartition\boot\bcd" /set '{default}' bootmenupolicy Legacy |Out-Null
    & bcdedit /store "$usbpartition\EFI\Microsoft\boot\bcd" /set '{default}' bootmenupolicy Legacy |Out-Null
    remove-item "$usbpartition\boot\bcd.*" -force
    remove-item "$usbpartition\EFI\Microsoft\boot\bcd.*" -force
    }
    #
    clear-host
    $USB=$Null
    $Form=New-Object System.Windows.Forms.Form # Create the screen form (window)
    $Form.TopMost = $True
    # Set the title and size of the window:
    $Form.Text=$ShortTitle
    $Form.Font='Consolas,10'
    $Form.Width=420 ; $Form.Height=270
    $Form.StartPosition = "CenterScreen"
    $Form.SizeGripStyle = "Hide"
    [System.Windows.Forms.Application]::EnableVisualStyles();
    
    # Add a Source Windows ISO label on the form
    $SourceISO=New-Object System.Windows.Forms.Label
    $SourceISO.Location=New-Object System.Drawing.Point(20,30)              
    $SourceISO.Size=New-Object System.Drawing.Size(200,20)
    $SourceISO.text="Source Windows ISO"
    $Form.Controls.Add($SourceISO)
    
    # Add the ISO file name on the form
    $ISOFile=New-Object System.Windows.Forms.Label
    $ISOFile.Location=New-Object System.Drawing.Point(20,50)
    $ISOFile.Text=" "
    $ISOFile.Size=New-Object System.Drawing.Size(364,24)
    $ISOFile.Backcolor = [System.Drawing.Color]::White
    $ISOFile.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle
    $Form.Controls.Add($ISOFile)
    
    # Add a Target USB Disk label on the form
    $TargetUSB=New-Object System.Windows.Forms.Label
    $TargetUSB.Location=New-Object System.Drawing.Point(20,100)
    $TargetUSB.Text="Target USB Disk"
    $TargetUSB.Size=New-Object System.Drawing.Size(200,20)
    $Form.Controls.Add($TargetUSB)
    
    # Create a USB Disk drop-down list and fill it
    $USBDiskList=New-Object System.Windows.Forms.ComboBox
    $USBDiskList.DropDownStyle=[System.Windows.Forms.ComboBoxStyle]::DropDownList;
    $USBDiskList.Location=New-Object System.Drawing.Point(20,120)
    $USBDiskList.Size=New-Object System.Drawing.Size(363,22)
    $Form.Controls.Add($SelectUSBDiskList)
    $USBDisks=@() # array with USB disk number
    #
    $Disks=$Null;While (!$Disks){
        If(!($Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")})){
        If((MsgBox "Plug in your USB disk first. `n`nClick OK to continue." "OKCancel" "Information") -eq "Cancel") {exit}}}
    
    # Add a Select ISO button on the form
    $SelectISOButton=New-Object System.Windows.Forms.Button
    $SelectISOButton.Location=New-Object System.Drawing.Point(20,185)
    $SelectISOButton.Text="Select ISO"                        
    $SelectISOButton.Size=New-Object System.Drawing.Size(110,26)
    $Form.Controls.Add($SelectISOButton)
    
    # Add a Create Setup Disk button on the form
    $CreateDiskButton=New-Object System.Windows.Forms.Button
    $CreateDiskButton.Location=New-Object System.Drawing.Point(140,185)
    $CreateDiskButton.Text="Create Setup Disk"
    $CreateDiskButton.Size=New-Object System.Drawing.Size(155,26)
    $Form.Controls.Add($CreateDiskButton)
    $CreateDiskButton.Enabled = $false
    $CreateDiskButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    
    # Add a Cancel button on the form
    $CancelButton=New-Object System.Windows.Forms.Button
    $CancelButton.Location=New-Object System.Drawing.Point(305,185)
    $CancelButton.Text="Cancel"                      
    $CancelButton.Size=New-Object System.Drawing.Size(80,26)
    $Form.Controls.Add($CancelButton)
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    #
    $SelectISOButton.Add_Click({
        If ($FileBrowser.ShowDialog() -ne "Cancel"){ # if Cancel, just ignore
            $Global:ImagePath = $FileBrowser.filename    # return the file name
            $ISOFile.Text= Split-Path -Path $ImagePath -leaf # extract filename and extension (iso)
            if (($ISOFile.Text).length -gt 44) {$ISOFile.Text = $ImagePath.PadRight(100," ").substring(0,43)+"..."}
            $CreateDiskButton.Enabled = $true
            $CreateDiskButton.Focus()}})
    
    Foreach ($USBDisk in $Disks) {
        $FriendlyName=($USBDisk.FriendlyName).PadRight(40," ").substring(0,35)
        $USBDisks+=$USBDisk.DiskNumber
        $USBDiskList.Items.Add(("{0,-30}{1,10:n2} GB" -f $FriendlyName,($USBDisk.Size/1GB)))|out-null
    <#
        Foreach ($Partition in (Get-Partition $USBDisk.DiskNumber)) {
            If(!$Partition.DriveLetter){$AccessPath="  "} Else {$AccessPath=$Partition.DriveLetter+":"}    
            $USBDisks+=$USBDisk.DiskNumber
            $USBDiskList.Items.Add((" ({0,2}){1,-30}{2,10:n2} GB" -f $AccessPath,($Partition|Get-Volume).FileSystemLabel.PadRight(25," ").substring(0,25), ($Partition.Size/1GB)))|out-null
        }
    #>
    }
    $form.Controls.Add($USBDiskList)
    $USBDiskList.SelectedIndex=0
    #
    $Groupbox=New-Object System.Windows.Forms.Groupbox # Add a group box on the form
    $Groupbox.Location=New-Object System.Drawing.Point(7,0)
    $Groupbox.Text=""
    $Groupbox.Size=New-Object System.Drawing.Size(390,225)
    $Form.Controls.Add($Groupbox)
    #
    @"
    1.) Click "Select ISO" to select your source Windows ISO
    
    2.) Select your "Target USB disk" from the drop-down menu
    
    3.) Click "Create Setup Disk" to start creating your Windows setup disk
    "@| Write-Host
    #
    if($form.ShowDialog() -eq "Cancel") {MsgBox "The script was cancelled." "OK" "Information"|out-Null;exit}
    #
    # At this point the mounted USB disk and the iso image file path are defined
    #
    $USB=$USBDisks[$USBDiskList.SelectedIndex]
    #
    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}
    # Mount the image file if not already mounted and get the drive letter
    #
    #   Check if iso already mounted by getting driveletter
    If($ISO=(Get-DiskImage -ImagePath $ImagePath | Get-Volume).DriveLetter){$Mounted=$True}    Else
    #    Mount iso and get drive letter
        {$Mounted=$False; Mount-DiskImage -ImagePath $ImagePath -StorageType ISO |out-null
         If(!($ISO=(Get-DiskImage -ImagePath $ImagePath | Get-Volume).DriveLetter)){Exit}}
    $ISO=$ISO+":"
    #
    Clear-Host
    Stop-Service ShellHWDetection -erroraction silentlycontinue|out-null
    #    Clear the USB disk
    Try{Clear-Disk $USB -RemoveData -RemoveOEM -Confirm:$False -ErrorAction Stop
        Set-Disk $USB -PartitionStyle MBR -ErrorAction Stop}
    Catch{"Select disk $USB`nclean`nconvert MBR`nrescan`nexit`n"|diskpart}
    #   Create the fat32 boot partition
    $usbfat32=(New-Partition -DiskNumber $usb -Size  1GB -AssignDriveLetter | Format-Volume -FileSystem FAT32 -NewFileSystemLabel "BOOT").DriveLetter + ":"
    #    Create the NTFS intall partition
    $usbntfs=(New-Partition -DiskNumber $usb -UseMaximumSize -AssignDriveLetter -IsActive | Format-Volume -FileSystem NTFS -NewFileSystemLabel "SETUP").DriveLetter + ":"
    Start-Service ShellHWDetection -erroraction silentlycontinue|out-null
    #    Copy needed files to the fat32 boot partition
    robocopy $iso"\" $usbfat32"\" bootmgr bootmgr.efi /a-:R
    robocopy $iso"\boot" $usbfat32"\boot" /e /a-:R
    robocopy $iso"\efi" $usbfat32"\efi" /e /a-:R
    robocopy $iso"\sources" $usbfat32"\sources" boot.wim /a-:R
    Update_BCD $usbfat32
    #     Remove the drive letter to hide the device
    Get-Volume -DriveLetter $usbfat32.substring(0,$usbfat32.Length-1) | Get-Partition | Remove-PartitionAccessPath -accesspath $usbfat32
    #    Copy needed files to the NTFS install partition
    robocopy $iso $usbntfs /e   /a-:R
    Update_BCD $usbntfs                                                                
    # Eject the mounted iso image if it was loaded by the script
    If(!$Mounted){DisMount-DiskImage -ImagePath $ImagePath |out-null}
    MsgBox "$ShortTitle was created successfully." "OK" "Information" |Out-Null;exit
    
     
  10. rpo

    rpo MDL Expert

    Jan 3, 2010
    1,503
    1,512
    60
  11. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,605
    2,676
    60


    :shake:
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  12. kyrcy

    kyrcy MDL Member

    Feb 25, 2020
    241
    15
    10
    #412 kyrcy, Feb 22, 2025
    Last edited: Feb 22, 2025
    Any way to make the ISO created with this script fit a 4GB usb flash?
     
  13. shhnedo

    shhnedo MDL Expert

    Mar 20, 2011
    1,884
    2,528
    60
    @kyrcy that's completely not the purpose of this script. This script makes a bootable USB with the iso YOU PROVIDE. These days you won't fit a windows 10/11 iso on a 4GB(~3.73GB) usb without severely chopping it down so the install.wim/.esd becomes smaller than 3GB(don't forget the ~500MB+ boot.wim).
    Solution in your case: find an 8GB+ usb.
     
  14. kyrcy

    kyrcy MDL Member

    Feb 25, 2020
    241
    15
    10
    I am aware of the purpose of the script. I tried it and the ISO it created was 3,82 GB, this is why I asked.
     
  15. shhnedo

    shhnedo MDL Expert

    Mar 20, 2011
    1,884
    2,528
    60
    I mean... you keep referring to "the iso created by the script", which is not true. The script doesn't create anything.
    If you're hard stuck with a 4GB usb stick, you need an iso that's way under 3GB in size, since the script partitions the usb into two parts, one of which is probably going to reach 1GB so it can safely fit all the bootable files from the iso. So in your case, you're left with ~2.73GB(maybe a touch more) for the second NTFS partition which will fit the entire contents of your iso.
    The problem is not in the script to begin with. It's either the iso or your usb - one is too big, the other is too small for your needs.
     
  16. kyrcy

    kyrcy MDL Member

    Feb 25, 2020
    241
    15
    10
    #416 kyrcy, Feb 23, 2025
    Last edited: Feb 23, 2025
    I have plenty of 8GB usb sticks, but if the script doesn't create anything, what does it do?
     
  17. shhnedo

    shhnedo MDL Expert

    Mar 20, 2011
    1,884
    2,528
    60
    Well, you're not. Everything is explained in OP. Quite literally nothing in OP can be simplified any further than it already is.
    The script partitions the usb into a FAT32 partition for the bootable stuff and an NTFS partition for the entire contents of the iso, so that you can install windows from this same usb under virtually ANY machine with legacy or uefi bios without having to do any extra changes/settings/etc. It's so simple. I don't understand what is unclear about it.
     
  18. Tito

    Tito Admin / Adviser
    Staff Member

    Nov 30, 2009
    18,942
    19,419
    340
    @kyrcy

    There is a thing called forum etiquette, and looks like you prefer not to follow it. Unfortunately, that's not how an online community operates.

    At least provide clear descriptions and post logs/screenshots related to the issues you are facing. Keep in mind that this is neither a paid service, nor the author is bound to answer your "questions," period.

    In case you are not satisfied with this project, or don't know how to work with it - even after multiple suggestions from the fellow forum members - perhaps just move on? There are countless other ways to achieve the same goal.
     
  19. garf02

    garf02 MDL Member

    Sep 15, 2007
    189
    73
    10
    Is not possible hide the hard drive,leaving only the USB keys, in the selection of USB Disk Drive?
     
  20. shhnedo

    shhnedo MDL Expert

    Mar 20, 2011
    1,884
    2,528
    60
    @garf02 that would go against what the script does, or half of it at least. It's mean to work on usb flash sticks and external hard drives. You have to either mind the drive you're selecting or just disconnect anything you're do not want to wipe.