[BOOT DISK] UEFI or Legacy BIOS (Install.wim over 4GB)

Discussion in 'Scripting' started by freddie-o, Mar 21, 2019.

  1. freddie-o

    freddie-o MDL Senior Member

    Jul 29, 2009
    300
    323
    10
    #1 freddie-o, Mar 21, 2019
    Last edited: Apr 15, 2019 at 12:29
    This method works with both Legacy BIOS and the native UEFI boot process. This boot disk is especially useful for systems that use UEFI with Secure Boot when installing a Windows ISO that has an Install.WIM over 4GB. It creates 2 partitions on your boot disk.
    1. A FAT32 partition big enough to fit all the Windows installation boot files
    2. An NTFS partition for all the setup files needed to install Windows.



    Credit to @rpo as the original source of this automated script.


    Code:
    @echo off
    (
    echo #
    echo #   This script must be executed with admin privilege
    echo #
    echo #   Test Administrator privileges
    echo If ^(-NOT ^([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent^(^)^).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"^)^)
    echo {
    echo #   Restart the script to get Administrator privileges and exit
    echo    Start-Process 'powershell.exe' -Verb runAs -ArgumentList ^("-NoLogo -WindowStyle Normal -noprofile -file " + """" + $PSCommandPath + """"^)
    echo    exit
    echo }
    echo #   We have Administrator privileges
    echo #
    echo [console]::ForegroundColor = "Yellow"
    echo [console]::BackgroundColor = "blue"
    echo #
    echo Add-Type -AssemblyName System.Windows.Forms # Load the class System.Windows.Forms
    echo # Filebrowser dialog
    echo $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
    echo    Multiselect = $false # One file can be chosen
    echo    Filter = 'ISO images ^(*.iso^)^|*.iso' # Select iso files
    echo }
    echo #
    echo clear-host
    echo $wshell=New-Object -ComObject Wscript.Shell
    echo $wshell.Popup^("Insert the USB disk.`r`nClick OK when ready.",0,"USB disk"^) ^| Out-Null
    echo #   Select the USB disk
    echo $USBDiskForm=New-Object System.Windows.Forms.Form # Create the screen form ^(window^)
    echo # $USBDiskForm ^|gm;pause
    echo # Set the title and size of the window:
    echo $USBDiskForm.Text='Available USB disk^(s^)'
    echo $USBDiskForm.Width=500 ; $USBDiskForm.Height=170
    echo $USBDiskForm.AutoSize=$true # AutoSize property to make the form automatically stretch, if the elements on the form are out of bounds
    echo # Create a drop-down list and fill it
    echo $USB=$Null
    echo $USBDisks=@^(^) # array whith USB disk number
    echo $Disks=Get-Disk ^| Where-Object {^($_.BusType -eq "USB"^) -and ^($_.OperationalStatus -eq "Online"^)}
    echo if ^($Disks.count -eq 0^){"No USB disk available";exit}
    echo $USBDiskList=New-Object System.Windows.Forms.ComboBox
    echo # $USBDiskList^|gm
    echo $USBDiskList.Width=450
    echo Foreach ^($USBDisk in $Disks^) {
    echo    $FriendlyName=^($USBDisk.FriendlyName^).PadRight^(30," "^).substring^(0,30^)  
    echo    $Partitions = Get-Partition ^| Where-Object { $_.DiskNumber -eq $USBDisk.DiskNumber}
    echo    If ^($Partitions^) {
    echo        Foreach ^($Partition in $Partitions^) {
    echo        $Volumes=get-volume ^| Where-Object {$Partition.AccessPaths -contains  $_.path }
    echo        Foreach ^($Volume in $Volumes^) {
    echo            $USBDisks+=$USBDisk.DiskNumber
    echo            $USBDiskList.Items.Add^(^(" {0,-30}| {1,1}:| {2,-30}| {3:n2} GB" -f $FriendlyName, ^($Partition.DriveLetter^), $Volume.FileSystemLabel.PadRight^(30," "^).substring^(0,30^), ^($Partition.Size/1GB^)^)^)^|out-null
    echo            }      
    echo        }
    echo    } Else {
    echo        $USBDisks+=$USBDisk.DiskNumber
    echo        $USBDiskList.Items.Add^(^(" {0,-30}| {1,1}| {2,-30}| {3:n2} GB" -f $FriendlyName, " ", " ",^($USBDisk.Size/1GB^)^)^)^|out-null
    echo    }
    echo }
    echo $USBDiskList.DropDownStyle=[System.Windows.Forms.ComboBoxStyle]::DropDownList;
    echo $USBDiskList.Location=New-Object System.Drawing.Point^(20,20^)
    echo $USBDiskList.SelectedIndex=0
    echo $USBDiskList.width=450;$USBDiskList.height=20
    echo $USBDiskList.backColor="blue" ; $USBDiskList.ForeColor="White"
    echo $USBDiskList.Font='Segoe UI,10'
    echo $USBDiskForm.Controls.Add^($USBDiskList^)
    echo $OKButton=New-Object System.Windows.Forms.Button # Put the OK button on the form
    echo $OKButton.Location=New-Object System.Drawing.Size^(400,80^)
    echo $OKButton.Text="OK";$OKButton.Font='Segoe UI,10'
    echo $OKButton.width=60;$OKButton.height=30;
    echo $USBDiskForm.Controls.Add^($OKButton^)
    echo $OKButton.Add_Click^({$global:OK=$true;$USBDiskForm.close^(^)}^)
    echo $CancelButton=New-Object System.Windows.Forms.Button # Put the Cancel button on the form
    echo $CancelButton.Location=New-Object System.Drawing.Size^(320,80^)
    echo $CancelButton.Size=New-Object System.Drawing.Size^(120,20^)
    echo $CancelButton.Text="Cancel";$CancelButton.Font='Segoe UI,10'
    echo $CancelButton.width=60;$CancelButton.height=30;
    echo $USBDiskForm.Controls.Add^($CancelButton^)
    echo $CancelButton.Add_Click^({$global:OK=$False;$USBDiskForm.close^(^)}^)
    echo [void]$USBDiskForm.ShowDialog^(^) # Display the form on the screen
    echo if ^(!$global:OK^){$wshell.Popup^("Operation cancelled",0,"Error"^) ^| Out-Null;exit}
    echo $USB=$USBDisks[$USBDiskList.SelectedIndex]
    echo #   Clear the USB stick
    echo Clear-Disk $usb -RemoveData -RemoveOEM -Confirm:$false
    echo Stop-Service ShellHWDetection
    echo #   Create the fat32 boot partition
    echo $usbfat32=^(New-Partition -DiskNumber $usb -Size 500MB -AssignDriveLetter -IsActive ^| Format-Volume -FileSystem FAT32 -NewFileSystemLabel "BOOT"^).DriveLetter + ":"
    echo #   Create the ntfs intall partition
    echo $usbntfs=^(New-Partition -DiskNumber $usb -UseMaximumSize -AssignDriveLetter ^| Format-Volume -FileSystem NTFS -NewFileSystemLabel "INSTALL"^).DriveLetter + ":"
    echo Start-Service ShellHWDetection
    echo # Read-Host "Eject the iso if it is mounted. When ready press Enter"
    echo # $Volumes = ^(Get-Volume^).Where^({$_.DriveLetter}^).DriveLetter
    echo # Read-Host "Mount the iso. When ready press Enter"
    echo # $ISO = ^(Compare-Object -ReferenceObject $Volumes -DifferenceObject ^(Get-Volume^).Where^({$_.DriveLetter}^).DriveLetter^).InputObject
    echo # $ISO = ^(get-volume^| Where-Object {^($_.DriveType -eq "CD-ROM"^) -and ^($_.filesystemlabel -ne ""^) -and ^($_.OperationalStatus -eq "OK"^)} ^|Out-GridView -Title 'Select Cd-Rom image' -OutputMode Single^).DriveLetter + ":"
    echo $wshell.Popup^("Select the ISO.",0,"ISO image"^) ^| Out-Null
    echo #
    echo [void]$FileBrowser.ShowDialog^(^)
    echo #
    echo $ImagePath = $FileBrowser.FileName;
    echo $ISO=":"
    echo If^($FileBrowser.FileNames -like "*\*"^) {
    echo #  Check if iso already mounted
    echo    $ISO = ^(Get-DiskImage -ImagePath $ImagePath ^| Get-Volume^).DriveLetter
    echo #   Mount iso
    echo    IF ^(!$ISO^) {Mount-DiskImage -ImagePath $ImagePath -StorageType ISO ^|out-null
    echo    $ISO = ^(Get-DiskImage -ImagePath $ImagePath ^| Get-Volume^).DriveLetter}
    echo    $ISO=$ISO+":"
    echo }
    echo if ^($ISO -eq ":"^) {$wshell.Popup^("No ISO image mounted or operation cancelled",0,"Error"^) ^| Out-Null;exit}
    echo # Copy-Item -Path $ISO\* -Destination "$($usbntfs)" -Recurse -Verbose
    echo # Copy-Item -Path $ISO"\bootmgr" -Destination $usbfat32"\" -Verbose
    echo # Copy-Item -Path $ISO"\bootmgr.efi" -Destination $usbfat32"\" -Verbose
    echo # Copy-Item -Path $ISO"\boot" -Destination $usbfat32"\boot" -Recurse  -Verbose
    echo # Copy-Item -Path $ISO"\efi" -Destination $usbfat32"\efi" -Recurse  -Verbose
    echo # new-item $usbfat32"\sources" -itemType Directory
    echo # Copy-Item -Path $ISO"\sources\boot.wim" -Destination $usbfat32"\sources\boot.wim"  -Verbose
    echo robocopy $iso $usbntfs /e
    echo robocopy $iso"\" $usbfat32"\" bootmgr bootmgr.efi
    echo robocopy $iso"\boot" $usbfat32"\boot" /e
    echo robocopy $iso"\efi" $usbfat32"\efi" /e
    echo robocopy $iso"\sources" $usbfat32"\sources" boot.wim
    echo # Eject the mounted iso image
    echo # ^(New-Object -ComObject Shell.Application^).Namespace^(17^).ParseName^($ISO^).InvokeVerb^("Eject"^)
    echo DisMount-DiskImage -ImagePath $ImagePath ^|out-null
    echo Remove-item ^($env:TEMP + "\script.ps1"^)   
    )>"%temp%\script.ps1"
    powershell "%temp%\script.ps1" -ExecutionPolicy Bypass -WindowStyle Normal
    
    


    What the script does:
    1. Run the script (as Administrator)
    2. After you plug in the USB disk
    3. It creates 2 partitions. A FAT32 and an NTFS partition.
    4. After you select your Windows ISO
    5. It copies the entire contents of Mounted Windows ISO to the NTFS partition of the boot disk.
    6. Then it copies the “boot” and “efi” folders and the “bootmgr” and “bootmgr.efi” files from the mounted ISO to the FAT32 partition.
    7. It then creates a “sources” folder in the FAT32 partition then copies the “boot.wim” from the sources folder of mounted ISO to the newly created folder.



    Download:
    Create_Boot_Disk






     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. Krager

    Krager MDL Senior Member

    Jan 9, 2017
    374
    212
    10
    That's handy, added to watched threads. I actually enabled TPM and secure boot for the sake of BIOS security so it's nice if I don't have to turn if off for installs. My image is actually still under the limit, but at some point it will exceed it.
     
  3. rpo

    rpo MDL Addicted

    Jan 3, 2010
    742
    519
    30
    1. After statement 30, add :
    Code:
    echo if ^($ISO -eq ":"^) {$wshell.Popup^("No ISO image mounted or operation cancelled",0,"Error"^) ^| Out-Null;exit}
    2. The script elevates itself to run as admin if necessary
     
  4. freddie-o

    freddie-o MDL Senior Member

    Jul 29, 2009
    300
    323
    10
    #4 freddie-o, Mar 22, 2019
    Last edited: Mar 22, 2019
    (OP)
    Like so?

    Code:
    @echo off
    rem    This script must be executed with admin privilege
    (
    echo #    This script must be executed with admin privilege
    echo #    Test Administrator privileges
    echo If ^(-NOT ^([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent^(^)^).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"^)^)
    echo {
    echo #    Restart the script to get Administrator privileges and exit
    echo     Start-Process 'powershell.exe' -Verb runAs -ArgumentList ^("-NoLogo -WindowStyle Maximized -noprofile -file " + """" + $PSCommandPath + """"^)
    echo     exit
    echo }
    echo #    We have Administrator privileges
    echo [console]::ForegroundColor = "Yellow"
    echo [console]::BackgroundColor = "blue"
    echo clear-host
    echo $wshell=New-Object -ComObject Wscript.Shell
    echo $wshell.Popup^("Plug in the USB stick",0,"USB stick"^) ^| Out-Null
    echo #    Select the USB stick
    echo $usb = ^(Get-Disk ^| Where-Object {^($_.BusType -eq "USB"^) -and ^($_.OperationalStatus -eq "Online"^)} ^| Out-GridView -Title 'Select USB Drive to Format' -OutputMode Single^).number
    echo if ^($usb -eq $null^) {$wshell.Popup^("No USB stick online or operation cancelled",0,"Erro"^) ^| Out-Null;exit}
    echo #    Clear the USB stick
    echo Clear-Disk $usb -RemoveData -RemoveOEM -Confirm:$false
    echo Stop-Service ShellHWDetection
    echo #    Create the fat32 boot partition
    echo $usbfat32=^(New-Partition -DiskNumber $usb -Size 600MB -AssignDriveLetter -IsActive ^| Format-Volume -FileSystem FAT32 -NewFileSystemLabel "BOOT"^).DriveLetter + ":"
    echo #    Create the ntfs intall partition
    echo $usbntfs=^(New-Partition -DiskNumber $usb -UseMaximumSize -AssignDriveLetter ^| Format-Volume -FileSystem NTFS -NewFileSystemLabel "ISO"^).DriveLetter + ":"
    echo Start-Service ShellHWDetection
    echo $wshell.Popup^("Mount the iso image",0,"ISO image"^) ^| Out-Null
    echo $ISO = ^(get-volume^| Where-Object {^($_.DriveType -eq "CD-ROM"^) -and ^($_.filesystemlabel -ne ""^) -and ^($_.OperationalStatus -eq "OK"^)} ^|Out-GridView -Title 'Select Cd-Rom image' -OutputMode Single^).DriveLetter + ":"
    echo if ^($ISO -eq ":"^) {$wshell.Popup^("No ISO image mounted or operation cancelled",0,"Error"^) ^| Out-Null;exit}
    echo robocopy $iso $usbntfs /e
    echo robocopy $iso"\" $usbfat32"\" bootmgr bootmgr.efi
    echo robocopy $iso"\boot" $usbfat32"\boot" /e
    echo robocopy $iso"\efi" $usbfat32"\efi" /e
    echo robocopy $iso"\sources" $usbfat32"\sources" boot.wim
    echo # Eject the mounted iso image
    echo ^(New-Object -ComObject Shell.Application^).Namespace^(17^).ParseName^($ISO^).InvokeVerb^("Eject"^)
    echo Remove-item ^($env:TEMP + "\script.ps1"^)
    )>"%temp%\script.ps1"
    powershell "%temp%\script.ps1" -ExecutionPolicy Bypass

    OK updated the OP script and download link
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. rpo

    rpo MDL Addicted

    Jan 3, 2010
    742
    519
    30
    Yes, that's right.
     
  6. freddie-o

    freddie-o MDL Senior Member

    Jul 29, 2009
    300
    323
    10
    Would be nice if this was a GUI :)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. freddie-o

    freddie-o MDL Senior Member

    Jul 29, 2009
    300
    323
    10
    I just help make it available here :)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. rpo

    rpo MDL Addicted

    Jan 3, 2010
    742
    519
    30
    Changed the way to select the iso image by introducing a file browser dialog :
    Code:
    @echo off
    (
    echo #
    echo #    This script must be executed with admin privilege
    echo #
    echo #    Test Administrator privileges
    echo If ^(-NOT ^([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent^(^)^).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"^)^)
    echo {
    echo #    Restart the script to get Administrator privileges and exit
    echo     Start-Process 'powershell.exe' -Verb runAs -ArgumentList ^("-NoLogo -WindowStyle Maximized -noprofile -file " + """" + $PSCommandPath + """"^)
    echo     exit
    echo }
    echo #    We have Administrator privileges
    echo #
    echo [console]::ForegroundColor = "Yellow"
    echo [console]::BackgroundColor = "blue"
    echo #
    echo # Filebrowser dialog
    echo Add-Type -AssemblyName System.Windows.Forms
    echo $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
    echo    Multiselect = $false # One file can be chosen
    echo     Filter = 'ISO images ^(*.iso^)^|*.iso' # Select iso files
    echo }
    echo #
    echo clear-host
    echo $wshell=New-Object -ComObject Wscript.Shell
    echo $wshell.Popup^("Mount the USB stick`r`nand click ""OK"" when ready.",0,"USB stick"^) ^| Out-Null
    echo #    Select the USB stick
    echo $usb = ^(Get-Disk ^| Where-Object {^($_.BusType -eq "USB"^) -and ^($_.OperationalStatus -eq "Online"^)} ^| Out-GridView -Title 'Select USB Drive to Format' -OutputMode Single^).number
    echo if ^($usb -eq $null^) {$wshell.Popup^("No USB stick online or operation cancelled",0,"Error"^) ^| Out-Null;exit}
    echo #    Clear the USB stick
    echo Clear-Disk $usb -RemoveData -RemoveOEM -Confirm:$false
    echo Stop-Service ShellHWDetection
    echo #    Create the fat32 boot partition
    echo $usbfat32=^(New-Partition -DiskNumber $usb -Size 1GB -AssignDriveLetter -IsActive ^| Format-Volume -FileSystem FAT32 -NewFileSystemLabel "BOOTFAT"^).DriveLetter + ":"
    echo #    Create the ntfs intall partition
    echo $usbntfs=^(New-Partition -DiskNumber $usb -UseMaximumSize -AssignDriveLetter ^| Format-Volume -FileSystem NTFS -NewFileSystemLabel "INSTALL"^).DriveLetter + ":"
    echo Start-Service ShellHWDetection
    echo $wshell.Popup^("Select the iso.",0,"ISO image"^) ^| Out-Null
    echo #
    echo [void]$FileBrowser.ShowDialog^(^)
    echo #
    echo $ImagePath = $FileBrowser.FileName;
    echo $ISO=":"
    echo If^($FileBrowser.FileNames -like "*\*"^) {
    echo #  Check if iso already mounted
    echo     $ISO = ^(Get-DiskImage -ImagePath $ImagePath ^| Get-Volume^).DriveLetter
    echo #    Mount iso
    echo     IF ^(!$ISO^) {Mount-DiskImage -ImagePath $ImagePath -StorageType ISO ^|out-null
    echo     $ISO = ^(Get-DiskImage -ImagePath $ImagePath ^| Get-Volume^).DriveLetter}
    echo     $ISO=$ISO+":"
    echo }
    echo if ^($ISO -eq ":"^) {$wshell.Popup^("No ISO image mounted or operation cancelled",0,"Error"^) ^| Out-Null;exit}
    echo robocopy $iso $usbntfs /e
    echo robocopy $iso"\" $usbfat32"\" bootmgr bootmgr.efi
    echo robocopy $iso"\boot" $usbfat32"\boot" /e
    echo robocopy $iso"\efi" $usbfat32"\efi" /e
    echo robocopy $iso"\sources" $usbfat32"\sources" boot.wim
    echo # Eject the mounted iso image
    echo DisMount-DiskImage -ImagePath $ImagePath ^|out-null  
    )>"%temp%\script.ps1"
    powershell "%temp%\script.ps1" -ExecutionPolicy Bypass
    
     
  9. freddie-o

    freddie-o MDL Senior Member

    Jul 29, 2009
    300
    323
    10
    Will the file browser dialog work with selecting the usb stick?
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  10. rpo

    rpo MDL Addicted

    Jan 3, 2010
    742
    519
    30
    Short answer : no.
     
  11. freddie-o

    freddie-o MDL Senior Member

    Jul 29, 2009
    300
    323
    10
    too bad
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  12. freddie-o

    freddie-o MDL Senior Member

    Jul 29, 2009
    300
    323
    10
    OK modified it a little bit and updated the OP. Thanks again rpo :thumbsup:
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  13. rpo

    rpo MDL Addicted

    Jan 3, 2010
    742
    519
    30
    Try this one.
     
  14. freddie-o

    freddie-o MDL Senior Member

    Jul 29, 2009
    300
    323
    10
    #14 freddie-o, Apr 4, 2019
    Last edited: Apr 4, 2019
    (OP)
    Nice :thumbsup: What do you think of this? No more "Mount the USB stick and click "OK" when ready."
    Also what do you think about the "$USBDiskForm" the size of the Windows "Run" window?

    @echo off
    (
    echo #
    echo # This script must be executed with admin privilege
    echo #
    echo # Test Administrator privileges
    echo If ^(-NOT ^([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent^(^)^).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"^)^)
    echo {
    echo # Restart the script to get Administrator privileges and exit
    echo Start-Process 'powershell.exe' -Verb runAs -ArgumentList ^("-NoLogo -WindowStyle Normal -noprofile -file " + """" + $PSCommandPath + """"^)
    echo exit
    echo }
    echo # We have Administrator privileges
    echo #
    echo [console]::ForegroundColor = "Yellow"
    echo [console]::BackgroundColor = "blue"
    echo #
    echo Add-Type -AssemblyName System.Windows.Forms # Load the class System.Windows.Forms
    echo # Filebrowser dialog
    echo $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
    echo Multiselect = $false # One file can be chosen
    echo Filter = 'ISO images ^(*.iso^)^|*.iso' # Select iso files
    echo }
    echo #
    echo clear-host
    echo # Select the USB stick
    echo $USBDiskForm=New-Object System.Windows.Forms.Form # Create the screen form ^(window^)
    echo # $USBDiskForm ^|gm;pause
    echo # Set the title and size of the window:
    echo $USBDiskForm.Text='Available USB disk^(s^)'
    echo $USBDiskForm.Width=900 ; $USBDiskForm.Height=200
    echo $USBDiskForm.AutoSize=$true # AutoSize property to make the form automatically stretch, if the elements on the form are out of bounds
    echo # Create a drop-down list and fill it
    echo $USB=$Null
    echo $USBDisks=@^(^) # array whith USB disk number
    echo $Disks=Get-Disk ^| Where-Object {^($_.BusType -eq "USB"^) -and ^($_.OperationalStatus -eq "Online"^)}
    echo if ^($Disks.count -eq 0^){"No USB disk available";exit}
    echo $USBDiskList=New-Object System.Windows.Forms.ComboBox
    echo # $USBDiskList^|gm
    echo $USBDiskList.Width=560
    echo Foreach ^($USBDisk in $Disks^) {
    echo $FriendlyName=^($USBDisk.FriendlyName^).PadRight^(30," "^).substring^(0,30^)
    echo $Partitions = Get-Partition ^| Where-Object { $_.DiskNumber -eq $USBDisk.DiskNumber}
    echo If ^($Partitions^) {
    echo Foreach ^($Partition in $Partitions^) {
    echo $Volumes=get-volume ^| Where-Object {$Partition.AccessPaths -contains $_.path }
    echo Foreach ^($Volume in $Volumes^) {
    echo $USBDisks+=$USBDisk.DiskNumber
    echo $USBDiskList.Items.Add^(^(" {0,-30}| {1,1}:| {2,-30}| {3:n2} GB" -f $FriendlyName, ^($Partition.DriveLetter^), $Volume.FileSystemLabel.PadRight^(30," "^).substring^(0,30^), ^($Partition.Size/1GB^)^)^)^|out-null
    echo }
    echo }
    echo } Else {
    echo $USBDisks+=$USBDisk.DiskNumber
    echo $USBDiskList.Items.Add^(^(" {0,-30}| {1,1}| {2,-30}| {3:n2} GB" -f $FriendlyName, " ", " ",^($USBDisk.Size/1GB^)^)^)^|out-null
    echo }
    echo }
    echo $USBDiskList.DropDownStyle=[System.Windows.Forms.ComboBoxStyle]::DropDownList;
    echo $USBDiskList.Location=New-Object System.Drawing.Point^(5,40^)
    echo $USBDiskList.SelectedIndex=0
    echo $USBDiskList.width=950;$USBDiskList.height=20
    echo $USBDiskList.backColor="blue" ; $USBDiskList.ForeColor="White"
    echo $USBDiskList.Font='Courier New,15,style=Bold'
    echo $USBDiskForm.Controls.Add^($USBDiskList^)
    echo $OKButton=New-Object System.Windows.Forms.Button # Put the OK button on the form
    echo $OKButton.Location=New-Object System.Drawing.Size^(890,100^)
    echo $OKButton.Text="OK";$OKButton.Font='Microsoft Sans Serif,10,style=Bold'
    echo $OKButton.width=60;$OKButton.height=30;
    echo $USBDiskForm.Controls.Add^($OKButton^)
    echo $OKButton.Add_Click^({$global:OK=$true;$USBDiskForm.close^(^)}^)
    echo $CancelButton=New-Object System.Windows.Forms.Button # Put the Cancel button on the form
    echo $CancelButton.Location=New-Object System.Drawing.Size^(800,100^)
    echo $CancelButton.Size=New-Object System.Drawing.Size^(120,20^)
    echo $CancelButton.Text="Cancel";$CancelButton.Font='Microsoft Sans Serif,10,style=Bold'
    echo $CancelButton.width=60;$CancelButton.height=30;
    echo $USBDiskForm.Controls.Add^($CancelButton^)
    echo $CancelButton.Add_Click^({$global:OK=$False;$USBDiskForm.close^(^)}^)
    echo [void]$USBDiskForm.ShowDialog^(^) # Display the form on the screen
    echo if ^(!$global:OK^){$wshell.Popup^("Operation cancelled",0,"Error"^) ^| Out-Null;exit}
    echo $USB=$USBDisks[$USBDiskList.SelectedIndex]
    echo # Clear the USB stick
    echo Clear-Disk $usb -RemoveData -RemoveOEM -Confirm:$false
    echo Stop-Service ShellHWDetection
    echo # Create the fat32 boot partition
    echo $usbfat32=^(New-Partition -DiskNumber $usb -Size 1GB -AssignDriveLetter -IsActive ^| Format-Volume -FileSystem FAT32 -NewFileSystemLabel "BOOTFAT"^).DriveLetter + ":"
    echo # Create the ntfs intall partition
    echo $usbntfs=^(New-Partition -DiskNumber $usb -UseMaximumSize -AssignDriveLetter ^| Format-Volume -FileSystem NTFS -NewFileSystemLabel "INSTALL"^).DriveLetter + ":"
    echo Start-Service ShellHWDetection
    echo # Read-Host "Eject the iso if it is mounted. When ready press Enter"
    echo # $Volumes = ^(Get-Volume^).Where^({$_.DriveLetter}^).DriveLetter
    echo # Read-Host "Mount the iso. When ready press Enter"
    echo # $ISO = ^(Compare-Object -ReferenceObject $Volumes -DifferenceObject ^(Get-Volume^).Where^({$_.DriveLetter}^).DriveLetter^).InputObject
    echo # $ISO = ^(get-volume^| Where-Object {^($_.DriveType -eq "CD-ROM"^) -and ^($_.filesystemlabel -ne ""^) -and ^($_.OperationalStatus -eq "OK"^)} ^|Out-GridView -Title 'Select Cd-Rom image' -OutputMode Single^).DriveLetter + ":"
    echo $wshell=New-Object -ComObject Wscript.Shell
    echo $wshell.Popup^("Select the iso.",0,"ISO image"^) ^| Out-Null
    echo #
    echo [void]$FileBrowser.ShowDialog^(^)
    echo #
    echo $ImagePath = $FileBrowser.FileName;
    echo $ISO=":"
    echo If^($FileBrowser.FileNames -like "*\*"^) {
    echo # Check if iso already mounted
    echo $ISO = ^(Get-DiskImage -ImagePath $ImagePath ^| Get-Volume^).DriveLetter
    echo # Mount iso
    echo IF ^(!$ISO^) {Mount-DiskImage -ImagePath $ImagePath -StorageType ISO ^|out-null
    echo $ISO = ^(Get-DiskImage -ImagePath $ImagePath ^| Get-Volume^).DriveLetter}
    echo $ISO=$ISO+":"
    echo }
    echo if ^($ISO -eq ":"^) {$wshell.Popup^("No ISO image mounted or operation cancelled",0,"Error"^) ^| Out-Null;exit}
    echo # Copy-Item -Path $ISO\* -Destination "$($usbntfs)" -Recurse -Verbose
    echo # Copy-Item -Path $ISO"\bootmgr" -Destination $usbfat32"\" -Verbose
    echo # Copy-Item -Path $ISO"\bootmgr.efi" -Destination $usbfat32"\" -Verbose
    echo # Copy-Item -Path $ISO"\boot" -Destination $usbfat32"\boot" -Recurse -Verbose
    echo # Copy-Item -Path $ISO"\efi" -Destination $usbfat32"\efi" -Recurse -Verbose
    echo # new-item $usbfat32"\sources" -itemType Directory
    echo # Copy-Item -Path $ISO"\sources\boot.wim" -Destination $usbfat32"\sources\boot.wim" -Verbose
    echo robocopy $iso $usbntfs /e
    echo robocopy $iso"\" $usbfat32"\" bootmgr bootmgr.efi
    echo robocopy $iso"\boot" $usbfat32"\boot" /e
    echo robocopy $iso"\efi" $usbfat32"\efi" /e
    echo robocopy $iso"\sources" $usbfat32"\sources" boot.wim
    echo # Eject the mounted iso image
    echo # ^(New-Object -ComObject Shell.Application^).Namespace^(17^).ParseName^($ISO^).InvokeVerb^("Eject"^)
    echo DisMount-DiskImage -ImagePath $ImagePath ^|out-null
    echo Remove-item ^($env:TEMP + "\script.ps1"^)
    )>"%temp%\script.ps1"
    powershell "%temp%\script.ps1" -ExecutionPolicy Bypass -WindowStyle Normal
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  15. rpo

    rpo MDL Addicted

    Jan 3, 2010
    742
    519
    30
    1. 'No more "Mount the USB stick and click "OK" when ready."'
    If no USB stick is mounted, the script will terminate : the popup is just a reminder for the user. Workaround : introduce a wait loop until the stick is mounted.
    2. ' "$USBDiskForm" the size of the Windows "Run" window?" : what do you suggest?
     
  16. freddie-o

    freddie-o MDL Senior Member

    Jul 29, 2009
    300
    323
    10
    #16 freddie-o, Apr 4, 2019
    Last edited: Apr 4, 2019
    (OP)
    No I just took out these lines.

    Code:
    echo $wshell=New-Object -ComObject Wscript.Shell
    echo $wshell.Popup^("Mount the USB stick`r`nand click ""OK"" when ready.",0,"USB stick"^) ^| Out-Null
    So it skips the "Mount the USB stick and click "OK" when ready." pop up window and goes straight to the "$USBDiskForm"

    When you click "Run" in the Start menu... the little run window
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  17. rpo

    rpo MDL Addicted

    Jan 3, 2010
    742
    519
    30
  18. freddie-o

    freddie-o MDL Senior Member

    Jul 29, 2009
    300
    323
    10
    What do you think about something like this?
    [​IMG]
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  19. freddie-o

    freddie-o MDL Senior Member

    Jul 29, 2009
    300
    323
    10
    Got this error
    Code:
    Exception setting "windowsize": "Window cannot be taller than 44.
    Parameter name: value.Height
    Actual value was 50."
    At C:\Users\Administrator\AppData\Local\Temp\script.ps1:16 char:77
    + ... ze.height = 50;  $newsize.width = 150;$pswindow.windowsize = $newsize
    +                                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
        + FullyQualifiedErrorId : ExceptionWhenSetting
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  20. rpo

    rpo MDL Addicted

    Jan 3, 2010
    742
    519
    30
    Sorry, i can't reproduce this issue on my system. All is working fine here.