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

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

  1. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,375
    2,277
    60
    #81 freddie-o, Aug 6, 2019
    Last edited: Aug 6, 2019
    (OP)
    EDIT:

    Modified
    Code:
    $Result=$wshell.Popup("Please mount USB stick. Click ""OK"" when ready`r`nor ""Cancel"" to exit.",0,"Warning",1+48)
    with
    Code:
    $Result=$wshell.Popup("Mount USB disk first. Click ""OK"" when ready or ""Cancel"" to exit.",0,"No USB mounted",1+64)
     
  2. rpo

    rpo MDL Expert

    Jan 3, 2010
    1,445
    1,421
    60
    #82 rpo, Aug 6, 2019
    Last edited: Aug 6, 2019
    Yes, replace with :
    Code:
    while (!$global:SelectISO){
        $Result=$wshell.Popup("No ISO image selected. Type ""OK"" to retry or ""Cancel"" to exit.",0,"No ISO image selected",1+64)
        if ($result -eq 2) {exit} else {[void]$Form.ShowDialog()}
    }
     
  3. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,375
    2,277
    60
    #83 freddie-o, Aug 6, 2019
    Last edited: Aug 6, 2019
    (OP)
    Super!

    Or this?
    Code:
    $Result=$wshell.Popup("No ISO image selected. Click ""Retry"" to return or ""Cancel"" to exit.",0,"No ISO image selected",5+64)
    EDIT:

    When I click the "Cancel" button or the "Close" button (the "X" on the top right corner of the window) on the GUI, the same pop up message "No ISO image selected. Click ""Retry"" to return or ""Cancel"" to exit." appears.
     
  4. rpo

    rpo MDL Expert

    Jan 3, 2010
    1,445
    1,421
    60
    Try this sequence :
    Code:
    while (!$global:SelectISO){
        if ($Cancel) {exit}
        $Result=$wshell.Popup("No ISO image selected. Click ""Retry"" to return or ""Cancel"" to exit.",0,"No ISO image selected",5+64)
        if ($result -eq 2) {exit} else {[void]$Form.ShowDialog()}
    }
     
  5. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,375
    2,277
    60
    #85 freddie-o, Aug 6, 2019
    Last edited: Aug 6, 2019
    (OP)
    Still happens when I click the "Close" button (the "X" on the top right corner of the window) :(

    EDIT:

    Also when I click the "Cancel" button after returning from the "No ISO image selected. Click "Retry" to return or "Cancel" to exit." message, the "The script was cancelled." doesn't pop up anymore.
     
  6. rpo

    rpo MDL Expert

    Jan 3, 2010
    1,445
    1,421
    60
    If you add
    $Form.FormBorderStyle = "none"
    in the definition of $Form, you have no "X" to click upon...and no title is displayed (just define a label with title text).

    If you desire a "The script was cancelled." message, replace
    if ($Cancel) {exit}
    by
    if ($Cancel){$wshell.Popup("The script was cancelled.",0,"Cancel") | Out-Null;exit}

    You could also add
    $Form.KeyPreview = $True
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Enter"){$CreateUSBDiskButton.PerformClick()}})
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Escape"){$CancelButton.PerformClick()}})
    to define action when pressing the Enter a Escape key of your keyboard.
     
  7. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,375
    2,277
    60
    #88 freddie-o, Aug 6, 2019
    Last edited: Aug 6, 2019
    (OP)
    These is the modified script so far
    Code:
    <# : standard way of doing hybrid batch + powershell scripts
    @set "__CMD__=%~f0" &set "__ARGS__=%*" &powershell -noprofile -c "iex([io.file]::ReadAllText(\"%~f0\"))" &exit/b
    #>
    #
    #    This script must be executed with admin privilege
    #
    #    Test Administrator privileges
    If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(544))
    {
    #    Restart the script to get Administrator privileges and exit
         Start-Process cmd -ArgumentList ("/c call `"$Env:__CMD__`" $Env:__ARGS__") -Verb runAs
         exit
    }
    #    We have Administrator privileges
    #
    [console]::ForegroundColor = "Yellow"
    [console]::BackgroundColor = "blue"
    #
    Add-Type -AssemblyName System.Windows.Forms # Load the class System.Windows.Forms
    # Filebrowser dialog
    $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
       Multiselect = $false # One file can be chosen
        Filter = 'ISO images (*.iso)|*.iso' # Select iso files
    }
    $wshell=New-Object -ComObject Wscript.Shell
    #
    $global:SelectISO=$False
    $global:Cancel=$False
    #
    clear-host
    $Form=New-Object System.Windows.Forms.Form # Create the screen form (window)
    # Set the title and size of the window:
    $Form.Text='Windows Installation Disk v3        [forums.mydigitallife.net]'
    $Form.Width=495 ; $Form.Height=220
    $Form.AutoSize=$true # AutoSize property to make the form automatically stretch, if the elements on the form are out of bounds
    #    Select the USB disk
    # Create a drop-down list and fill it
    $Combobox=New-Object System.Windows.Forms.ComboBox
    $Combobox.DropDownStyle=[System.Windows.Forms.ComboBoxStyle]::DropDownList;
    $Combobox.Location=New-Object System.Drawing.Point(15,40)
    $Combobox.Width=450;$Combobox.height=20
    $USB=$Null
    $USBDisks=@() # array with USB disk number
    $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    While (!$Disks) {
        $Result=$wshell.Popup("Mount your USB disk first. Click ""OK"" when ready or ""Cancel"" to exit.",0,"Mount USB",1+64)
        if ($result -eq 2) {exit}
        $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    }
    $USBDiskList=New-Object System.Windows.Forms.ComboBox
    # $USBDiskList|gm
    $USBDiskList.Width=450
    Foreach ($USBDisk in $Disks) {
        $FriendlyName=($USBDisk.FriendlyName).PadRight(30," ").substring(0,30)   
        $Partitions = Get-Partition | Where-Object { $_.DiskNumber -eq $USBDisk.DiskNumber}
        If ($Partitions) {
            Foreach ($Partition in $Partitions) {
            $Volumes=get-volume | Where-Object {$Partition.AccessPaths -contains  $_.path }
            Foreach ($Volume in $Volumes) {
                $USBDisks+=$USBDisk.DiskNumber
                $Combobox.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
                }       
            }   
        } Else {
            $USBDisks+=$USBDisk.DiskNumber
            $USBDiskList.Items.Add((" {0,-30}| {1,1}| {2,-30}| {3:n2} GB" -f $FriendlyName, " ", " ",($USBDisk.Size/1GB)))|out-null
        }
    }
    $SelectUSBDisk=New-Object System.Windows.Forms.Label # Put the SelectUSBDisk label on the form
    $SelectUSBDisk.Location=New-Object System.Drawing.Size(15,20)
    $SelectUSBDisk.Text="Select USB disk";$SelectUSBDisk.Font='Segoe UI,10'
    $SelectUSBDisk.width=200;$SelectUSBDisk.height=20;
    $Form.Controls.Add($SelectUSBDisk)
    $form.Controls.Add($Combobox)
    $Combobox.SelectedIndex=0
    
    $SelectISOButton=New-Object System.Windows.Forms.Button # Put the SelectISO button on the form
    $SelectISOButton.Location=New-Object System.Drawing.Size(355,130)
    $SelectISOButton.Text="Select ISO";$SelectISOButton.Font='Segoe UI,10'
    $SelectISOButton.width=110;$SelectISOButton.height=30;
    $Form.Controls.Add($SelectISOButton)
    $SelectISOButton.Add_Click({$global:SelectISO=$true;[void]$FileBrowser.ShowDialog()})
    $Form.Controls.Add($SelectISOButton)
    
    $CreateUSBDiskButton=New-Object System.Windows.Forms.Button # Put the CreateUSBDisk button on the form
    $CreateUSBDiskButton.Location=New-Object System.Drawing.Size(15,130)
    $CreateUSBDiskButton.Text="Create USB Disk";$CreateUSBDiskButton.Font='Segoe UI,10'
    $CreateUSBDiskButton.width=110;$CreateUSBDiskButton.height=30;
    $Form.Controls.Add($CreateUSBDiskButton)
    $CreateUSBDiskButton.Add_Click({$Form.close()})
    $Form.Controls.Add($CreateUSBDiskButton)
    
    $CancelButton=New-Object System.Windows.Forms.Button # Put the Cancel button on the form
    $CancelButton.Location=New-Object System.Drawing.Size(230,130)
    $CancelButton.Size=New-Object System.Drawing.Size(120,20)
    $CancelButton.Text="Cancel";$CancelButton.Font='Segoe UI,10'
    $CancelButton.width=110;$CancelButton.height=30;
    $Form.Controls.Add($CancelButton)
    $CancelButton.Add_Click({$global:Cancel=$True;$Form.close()})
    #
    [void]$Form.ShowDialog()
    #
    $USB=$USBDisks[$USBDiskList.SelectedIndex]
    #
    
    while (!$global:SelectISO){
        if ($Cancel){$wshell.Popup("The script was cancelled.",0,"Cancel script") | Out-Null;exit}
        $Result=$wshell.Popup("Select your Windows ISO image first. Click ""Retry"" to return or ""Cancel"" to exit.",0,"Select Windows ISO",5+64)
        if ($result -eq 2) {exit} else {[void]$Form.ShowDialog()}
    }
    #
    $ImagePath = $FileBrowser.FileName;
    $ISO=":"
    If($FileBrowser.FileNames -like "*\*") {
    #  Check if iso already mounted
        $ISO = (Get-DiskImage -ImagePath $ImagePath | Get-Volume).DriveLetter
    #    Mount iso
        IF (!$ISO) {Mount-DiskImage -ImagePath $ImagePath -StorageType ISO |out-null
        $ISO = (Get-DiskImage -ImagePath $ImagePath | Get-Volume).DriveLetter}
        $ISO=$ISO+":"
    }
    if ($ISO -eq ":") {$wshell.Popup("No ISO image mounted or operation cancelled",0,"Error") | Out-Null;exit}
    #
    $Result=$wshell.Popup("All partitions and data on the USB disk will be lost.",0,"Warning",1+48)
    if ($result -eq 2) {DisMount-DiskImage -ImagePath $ImagePath |out-null;exit}
    #    Clear the USB stick
    Clear-Disk $usb -RemoveData -RemoveOEM -Confirm:$false
    Stop-Service ShellHWDetection
    #    Create the fat32 boot partition
    $usbfat32=(New-Partition -DiskNumber $usb -Size 700MB -AssignDriveLetter -IsActive | Format-Volume -FileSystem FAT32 -NewFileSystemLabel "BOOT").DriveLetter + ":"
    #    Create the ntfs intall partition
    $usbntfs=(New-Partition -DiskNumber $usb -UseMaximumSize -AssignDriveLetter | Format-Volume -FileSystem NTFS -NewFileSystemLabel "INSTALL").DriveLetter + ":"
    Start-Service ShellHWDetection
    #
    robocopy $iso $usbntfs /e
    robocopy $iso"\" $usbfat32"\" bootmgr bootmgr.efi
    robocopy $iso"\boot" $usbfat32"\boot" /e
    robocopy $iso"\efi" $usbfat32"\efi" /e
    robocopy $iso"\sources" $usbfat32"\sources" boot.wim
    # Eject the mounted iso image
    DisMount-DiskImage -ImagePath $ImagePath |out-null
    #done - keep a comment on the last line so the previous useful crlf never gets eaten
    
    


    There's just the issue with the "X" button
     
  8. rpo

    rpo MDL Expert

    Jan 3, 2010
    1,445
    1,421
    60
    You could test this version :
    Code:
    <# : standard way of doing hybrid batch + powershell scripts
    @set "__CMD__=%~f0" &set "__ARGS__=%*" &powershell -noprofile -c "iex([io.file]::ReadAllText(\"%~f0\"))" &exit/b
    #>
    #
    #    This script must be executed with admin privilege
    #
    #    Test Administrator privileges
    If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(544))
    {
    #    Restart the script to get Administrator privileges and exit
         Start-Process cmd -ArgumentList ("/c call `"$Env:__CMD__`" $Env:__ARGS__") -Verb runAs
         exit
    }
    #    We have Administrator privileges
    #
    [console]::ForegroundColor = "Yellow"
    [console]::BackgroundColor = "blue"
    #
    Add-Type -AssemblyName System.Windows.Forms # Load the class System.Windows.Forms
    # Filebrowser dialog
    $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
       Multiselect = $false # One file can be chosen
        Filter = 'ISO images (*.iso)|*.iso' # Select iso files
    }
    $wshell=New-Object -ComObject Wscript.Shell
    #
    $global:SelectISO=$False
    $global:Cancel=$False
    #
    clear-host
    $Form=New-Object System.Windows.Forms.Form # Create the screen form (window)
    # Set the title and size of the window:
    $Form.Text='Windows Installation Disk v3        [forums.mydigitallife.net]'
    $Form.Width=495 ; $Form.Height=220
    $Form.AutoSize=$true # AutoSize property to make the form automatically stretch, if the elements on the form are out of bounds
    #    Select the USB disk
    # Create a drop-down list and fill it
    $Combobox=New-Object System.Windows.Forms.ComboBox
    $Combobox.DropDownStyle=[System.Windows.Forms.ComboBoxStyle]::DropDownList;
    $Combobox.Location=New-Object System.Drawing.Point(15,40)
    $Combobox.Width=450;$Combobox.height=20
    $Form.KeyPreview = $True
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Enter"){$CreateUSBDiskButton.PerformClick()}})
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Escape"){$CancelButton.PerformClick()}})
    $USB=$Null
    $USBDisks=@() # array with USB disk number
    $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    While (!$Disks) {
        $Result=$wshell.Popup("Mount your USB disk first. Click ""OK"" when ready or ""Cancel"" to exit.",0,"Mount USB",1+64)
        if ($result -eq 2) {exit}
        $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    }
    $USBDiskList=New-Object System.Windows.Forms.ComboBox
    # $USBDiskList|gm
    $USBDiskList.Width=450
    Foreach ($USBDisk in $Disks) {
        $FriendlyName=($USBDisk.FriendlyName).PadRight(30," ").substring(0,30)   
        $Partitions = Get-Partition | Where-Object { $_.DiskNumber -eq $USBDisk.DiskNumber}
        If ($Partitions) {
            Foreach ($Partition in $Partitions) {
            $Volumes=get-volume | Where-Object {$Partition.AccessPaths -contains  $_.path }
            Foreach ($Volume in $Volumes) {
                $USBDisks+=$USBDisk.DiskNumber
                $Combobox.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
                }       
            }   
        } Else {
            $USBDisks+=$USBDisk.DiskNumber
            $USBDiskList.Items.Add((" {0,-30}| {1,1}| {2,-30}| {3:n2} GB" -f $FriendlyName, " ", " ",($USBDisk.Size/1GB)))|out-null
        }
    }
    $SelectUSBDisk=New-Object System.Windows.Forms.Label # Put the SelectUSBDisk label on the form
    $SelectUSBDisk.Location=New-Object System.Drawing.Size(15,20)
    $SelectUSBDisk.Text="Select USB disk";$SelectUSBDisk.Font='Segoe UI,10'
    $SelectUSBDisk.width=200;$SelectUSBDisk.height=20;
    $Form.Controls.Add($SelectUSBDisk)
    $form.Controls.Add($Combobox)
    $Combobox.SelectedIndex=0
    
    $SelectISOButton=New-Object System.Windows.Forms.Button # Put the SelectISO button on the form
    $SelectISOButton.Location=New-Object System.Drawing.Size(355,130)
    $SelectISOButton.Text="Select ISO";$SelectISOButton.Font='Segoe UI,10'
    $SelectISOButton.width=110;$SelectISOButton.height=30;
    $Form.Controls.Add($SelectISOButton)
    $SelectISOButton.Add_Click({$global:SelectISO=$true;[void]$FileBrowser.ShowDialog()})
    $Form.Controls.Add($SelectISOButton)
    
    $CreateUSBDiskButton=New-Object System.Windows.Forms.Button # Put the CreateUSBDisk button on the form
    $CreateUSBDiskButton.Location=New-Object System.Drawing.Size(15,130)
    $CreateUSBDiskButton.Text="Create USB Disk";$CreateUSBDiskButton.Font='Segoe UI,10'
    $CreateUSBDiskButton.width=110;$CreateUSBDiskButton.height=30;
    $Form.Controls.Add($CreateUSBDiskButton)
    $CreateUSBDiskButton.Add_Click({$Form.close()})
    $Form.Controls.Add($CreateUSBDiskButton)
    
    $CancelButton=New-Object System.Windows.Forms.Button # Put the Cancel button on the form
    $CancelButton.Location=New-Object System.Drawing.Size(230,130)
    $CancelButton.Size=New-Object System.Drawing.Size(120,20)
    $CancelButton.Text="Cancel";$CancelButton.Font='Segoe UI,10'
    $CancelButton.width=110;$CancelButton.height=30;
    $Form.Controls.Add($CancelButton)
    $CancelButton.Add_Click({$global:Cancel=$True;$Form.close()})
    #
    [void]$Form.ShowDialog()
    if ($Cancel){$wshell.Popup("The script was cancelled.",0,"Cancel script") | Out-Null;exit}
    #
    $USB=$USBDisks[$USBDiskList.SelectedIndex]
    #
    
    while (!$global:SelectISO){
        if ($Cancel){$wshell.Popup("The script was cancelled.",0,"Cancel script") | Out-Null;exit}
        $Result=$wshell.Popup("Select your Windows ISO image first. Click ""Retry"" to return or ""Cancel"" to exit.",0,"Select Windows ISO",5+64)
        if ($result -eq 2) {exit} else {[void]$Form.ShowDialog()}
    }
    #
    $ImagePath = $FileBrowser.FileName;
    $ISO=":"
    If($FileBrowser.FileNames -like "*\*") {
    #  Check if iso already mounted
        $ISO = (Get-DiskImage -ImagePath $ImagePath | Get-Volume).DriveLetter
    #    Mount iso
        IF (!$ISO) {Mount-DiskImage -ImagePath $ImagePath -StorageType ISO |out-null
        $ISO = (Get-DiskImage -ImagePath $ImagePath | Get-Volume).DriveLetter}
        $ISO=$ISO+":"
    }
    if ($ISO -eq ":") {$wshell.Popup("No ISO image mounted or operation cancelled",0,"Error") | Out-Null;exit}
    #
    $Result=$wshell.Popup("All partitions and data on the USB disk will be lost.",0,"Warning",1+48)
    if ($result -eq 2) {DisMount-DiskImage -ImagePath $ImagePath |out-null;exit}
    #    Clear the USB stick
    Clear-Disk $usb -RemoveData -RemoveOEM -Confirm:$false
    Stop-Service ShellHWDetection
    #    Create the fat32 boot partition
    $usbfat32=(New-Partition -DiskNumber $usb -Size 700MB -AssignDriveLetter -IsActive | Format-Volume -FileSystem FAT32 -NewFileSystemLabel "BOOT").DriveLetter + ":"
    #    Create the ntfs intall partition
    $usbntfs=(New-Partition -DiskNumber $usb -UseMaximumSize -AssignDriveLetter | Format-Volume -FileSystem NTFS -NewFileSystemLabel "INSTALL").DriveLetter + ":"
    Start-Service ShellHWDetection
    #
    robocopy $iso $usbntfs /e
    robocopy $iso"\" $usbfat32"\" bootmgr bootmgr.efi
    robocopy $iso"\boot" $usbfat32"\boot" /e
    robocopy $iso"\efi" $usbfat32"\efi" /e
    robocopy $iso"\sources" $usbfat32"\sources" boot.wim
    # Eject the mounted iso image
    DisMount-DiskImage -ImagePath $ImagePath |out-null
    #done - keep a comment on the last line so the previous useful crlf never gets eaten
    
     
  9. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,375
    2,277
    60
    This one at least has no issues.
    Code:
    <# : standard way of doing hybrid batch + powershell scripts
    @set "__CMD__=%~f0" &set "__ARGS__=%*" &powershell -noprofile -c "iex([io.file]::ReadAllText(\"%~f0\"))" &exit/b
    #>
    #
    #    This script must be executed with admin privilege
    #
    #    Test Administrator privileges
    If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(544))
    {
    #    Restart the script to get Administrator privileges and exit
         Start-Process cmd -ArgumentList ("/c call `"$Env:__CMD__`" $Env:__ARGS__") -Verb runAs
         exit
    }
    #    We have Administrator privileges
    #
    [console]::ForegroundColor = "Yellow"
    [console]::BackgroundColor = "blue"
    #
    Add-Type -AssemblyName System.Windows.Forms # Load the class System.Windows.Forms
    # Filebrowser dialog
    $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
       Multiselect = $false # One file can be chosen
        Filter = 'ISO images (*.iso)|*.iso' # Select iso files
    }
    $wshell=New-Object -ComObject Wscript.Shell
    #
    $global:SelectISO=$False
    $global:Cancel=$False
    #
    clear-host
    $Form=New-Object System.Windows.Forms.Form # Create the screen form (window)
    # Set the title and size of the window:
    $Form.Text='Windows Installation Disk v3        [forums.mydigitallife.net]'
    $Form.Width=495 ; $Form.Height=220
    $Form.AutoSize=$true # AutoSize property to make the form automatically stretch, if the elements on the form are out of bounds
    #    Select the USB disk
    # Create a drop-down list and fill it
    $Combobox=New-Object System.Windows.Forms.ComboBox
    $Combobox.DropDownStyle=[System.Windows.Forms.ComboBoxStyle]::DropDownList;
    $Combobox.Location=New-Object System.Drawing.Point(15,40)
    $Combobox.Width=450;$Combobox.height=20
    $USB=$Null
    $USBDisks=@() # array with USB disk number
    $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    While (!$Disks) {
        $Result=$wshell.Popup("Mount your USB disk first. Click ""OK"" when ready or ""Cancel"" to exit.",0,"Mount USB",1+64)
        if ($result -eq 2) {exit}
        $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    }
    $USBDiskList=New-Object System.Windows.Forms.ComboBox
    # $USBDiskList|gm
    $USBDiskList.Width=450
    Foreach ($USBDisk in $Disks) {
        $FriendlyName=($USBDisk.FriendlyName).PadRight(30," ").substring(0,30)   
        $Partitions = Get-Partition | Where-Object { $_.DiskNumber -eq $USBDisk.DiskNumber}
        If ($Partitions) {
            Foreach ($Partition in $Partitions) {
            $Volumes=get-volume | Where-Object {$Partition.AccessPaths -contains  $_.path }
            Foreach ($Volume in $Volumes) {
                $USBDisks+=$USBDisk.DiskNumber
                $Combobox.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
                }       
            }   
        } Else {
            $USBDisks+=$USBDisk.DiskNumber
            $USBDiskList.Items.Add((" {0,-30}| {1,1}| {2,-30}| {3:n2} GB" -f $FriendlyName, " ", " ",($USBDisk.Size/1GB)))|out-null
        }
    }
    $SelectUSBDisk=New-Object System.Windows.Forms.Label # Put the SelectUSBDisk label on the form
    $SelectUSBDisk.Location=New-Object System.Drawing.Size(15,20)
    $SelectUSBDisk.Text="Select USB disk";$SelectUSBDisk.Font='Segoe UI,10'
    $SelectUSBDisk.width=200;$SelectUSBDisk.height=20;
    $Form.Controls.Add($SelectUSBDisk)
    $form.Controls.Add($Combobox)
    $Combobox.SelectedIndex=0
    
    $SelectISOButton=New-Object System.Windows.Forms.Button # Put the SelectISO button on the form
    $SelectISOButton.Location=New-Object System.Drawing.Size(355,130)
    $SelectISOButton.Text="Select ISO";$SelectISOButton.Font='Segoe UI,10'
    $SelectISOButton.width=110;$SelectISOButton.height=30;
    $Form.Controls.Add($SelectISOButton)
    $SelectISOButton.Add_Click({$global:SelectISO=$true;[void]$FileBrowser.ShowDialog()})
    $Form.Controls.Add($SelectISOButton)
    
    $CreateUSBDiskButton=New-Object System.Windows.Forms.Button # Put the CreateUSBDisk button on the form
    $CreateUSBDiskButton.Location=New-Object System.Drawing.Size(15,130)
    $CreateUSBDiskButton.Text="Create USB Disk";$CreateUSBDiskButton.Font='Segoe UI,10'
    $CreateUSBDiskButton.width=110;$CreateUSBDiskButton.height=30;
    $Form.Controls.Add($CreateUSBDiskButton)
    $CreateUSBDiskButton.Add_Click({$Form.close()})
    $Form.Controls.Add($CreateUSBDiskButton)
    
    $CancelButton=New-Object System.Windows.Forms.Button # Put the Cancel button on the form
    $CancelButton.Location=New-Object System.Drawing.Size(230,130)
    $CancelButton.Size=New-Object System.Drawing.Size(120,20)
    $CancelButton.Text="Cancel";$CancelButton.Font='Segoe UI,10'
    $CancelButton.width=110;$CancelButton.height=30;
    $Form.Controls.Add($CancelButton)
    $CancelButton.Add_Click({$global:Cancel=$True;$Form.close()})
    #
    [void]$Form.ShowDialog()
    #
    $USB=$USBDisks[$USBDiskList.SelectedIndex]
    #
    $ImagePath = $FileBrowser.FileName;
    $ISO=":"
    If($FileBrowser.FileNames -like "*\*") {
    #  Check if iso already mounted
        $ISO = (Get-DiskImage -ImagePath $ImagePath | Get-Volume).DriveLetter
    #    Mount iso
        IF (!$ISO) {Mount-DiskImage -ImagePath $ImagePath -StorageType ISO |out-null
        $ISO = (Get-DiskImage -ImagePath $ImagePath | Get-Volume).DriveLetter}
        $ISO=$ISO+":"
    }
    if ($ISO -eq ":") {$wshell.Popup("No ISO image mounted or operation cancelled. Click ""OK"" to exit.",0,"Error") | Out-Null;exit}
    #
    $Result=$wshell.Popup("All partitions and data on the USB disk will be lost.",0,"Warning",1+48)
    if ($result -eq 2) {DisMount-DiskImage -ImagePath $ImagePath |out-null;exit}
    #    Clear the USB stick
    Clear-Disk $usb -RemoveData -RemoveOEM -Confirm:$false
    Stop-Service ShellHWDetection
    #    Create the fat32 boot partition
    $usbfat32=(New-Partition -DiskNumber $usb -Size 700MB -AssignDriveLetter -IsActive | Format-Volume -FileSystem FAT32 -NewFileSystemLabel "BOOT").DriveLetter + ":"
    #    Create the ntfs intall partition
    $usbntfs=(New-Partition -DiskNumber $usb -UseMaximumSize -AssignDriveLetter | Format-Volume -FileSystem NTFS -NewFileSystemLabel "INSTALL").DriveLetter + ":"
    Start-Service ShellHWDetection
    #
    robocopy $iso $usbntfs /e
    robocopy $iso"\" $usbfat32"\" bootmgr bootmgr.efi
    robocopy $iso"\boot" $usbfat32"\boot" /e
    robocopy $iso"\efi" $usbfat32"\efi" /e
    robocopy $iso"\sources" $usbfat32"\sources" boot.wim
    # Eject the mounted iso image
    DisMount-DiskImage -ImagePath $ImagePath |out-null
    #done - keep a comment on the last line so the previous useful crlf never gets eaten
    
    
     
  10. rpo

    rpo MDL Expert

    Jan 3, 2010
    1,445
    1,421
    60
    Add $Form.ControlBox = $False : that removes all controls - minimise, maximise and close, hence no "X".
     
  11. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,375
    2,277
    60
    #93 freddie-o, Aug 6, 2019
    Last edited: Aug 6, 2019
    (OP)
    More housekeeping to simplify things :)
    Code:
    <# : standard way of doing hybrid batch + powershell scripts
    @set "__CMD__=%~f0" &set "__ARGS__=%*" &powershell -noprofile -c "iex([io.file]::ReadAllText(\"%~f0\"))" &exit/b
    #>
    #
    #    This script must be executed with admin privilege
    #
    #    Test Administrator privileges
    If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(544))
    {
    #    Restart the script to get Administrator privileges and exit
         Start-Process cmd -ArgumentList ("/c call `"$Env:__CMD__`" $Env:__ARGS__") -Verb runAs
         exit
    }
    #    We have Administrator privileges
    #
    [console]::ForegroundColor = "Yellow"
    [console]::BackgroundColor = "blue"
    #
    Add-Type -AssemblyName System.Windows.Forms # Load the class System.Windows.Forms
    # Filebrowser dialog
    $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
       Multiselect = $false # One file can be chosen
        Filter = 'ISO images (*.iso)|*.iso' # Select iso files
    }
    $wshell=New-Object -ComObject Wscript.Shell
    #
    $global:SelectISO=$False
    $global:Cancel=$False
    #
    clear-host
    $Form=New-Object System.Windows.Forms.Form # Create the screen form (window)
    # Set the title and size of the window:
    $Form.Text='Windows Installation Disk v3        [forums.mydigitallife.net]'
    $Form.Width=495 ; $Form.Height=220
    $Form.AutoSize=$true # AutoSize property to make the form automatically stretch, if the elements on the form are out of bounds
    #    Select the USB disk
    # Create a drop-down list and fill it
    $Combobox=New-Object System.Windows.Forms.ComboBox
    $Combobox.DropDownStyle=[System.Windows.Forms.ComboBoxStyle]::DropDownList;
    $Combobox.Location=New-Object System.Drawing.Point(15,40)
    $Combobox.Width=450;$Combobox.height=20
    $USB=$Null
    $USBDisks=@() # array with USB disk number
    $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    While (!$Disks) {
        $Result=$wshell.Popup("Mount your USB disk first. Click ""OK"" when ready or ""Cancel"" to exit.",0,"Mount USB",1+64)
        if ($result -eq 2) {exit}
        $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    }
    $USBDiskList=New-Object System.Windows.Forms.ComboBox
    # $USBDiskList|gm
    $USBDiskList.Width=450
    Foreach ($USBDisk in $Disks) {
        $FriendlyName=($USBDisk.FriendlyName).PadRight(30," ").substring(0,30)  
        $Partitions = Get-Partition | Where-Object { $_.DiskNumber -eq $USBDisk.DiskNumber}
        If ($Partitions) {
            Foreach ($Partition in $Partitions) {
            $Volumes=get-volume | Where-Object {$Partition.AccessPaths -contains  $_.path }
            Foreach ($Volume in $Volumes) {
                $USBDisks+=$USBDisk.DiskNumber
                $Combobox.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
                }      
            }  
        } Else {
            $USBDisks+=$USBDisk.DiskNumber
            $USBDiskList.Items.Add((" {0,-30}| {1,1}| {2,-30}| {3:n2} GB" -f $FriendlyName, " ", " ",($USBDisk.Size/1GB)))|out-null
        }
    }
    $SelectUSBDisk=New-Object System.Windows.Forms.Label # Put the SelectUSBDisk label on the form
    $SelectUSBDisk.Location=New-Object System.Drawing.Size(15,20)
    $SelectUSBDisk.Text="Select USB disk";$SelectUSBDisk.Font='Segoe UI,10'
    $SelectUSBDisk.width=200;$SelectUSBDisk.height=20;
    $Form.Controls.Add($SelectUSBDisk)
    $form.Controls.Add($Combobox)
    $Combobox.SelectedIndex=0
    
    $SelectISOButton=New-Object System.Windows.Forms.Button # Put the SelectISO button on the form
    $SelectISOButton.Location=New-Object System.Drawing.Size(355,130)
    $SelectISOButton.Text="Select ISO";$SelectISOButton.Font='Segoe UI,10'
    $SelectISOButton.width=110;$SelectISOButton.height=30;
    $Form.Controls.Add($SelectISOButton)
    $SelectISOButton.Add_Click({$global:SelectISO=$true;[void]$FileBrowser.ShowDialog()})
    $Form.Controls.Add($SelectISOButton)
    $SelectISOButton.Add_Click({$Form.close()})
    
    
    $CancelButton=New-Object System.Windows.Forms.Button # Put the Cancel button on the form
    $CancelButton.Location=New-Object System.Drawing.Size(230,130)
    $CancelButton.Size=New-Object System.Drawing.Size(120,20)
    $CancelButton.Text="Cancel";$CancelButton.Font='Segoe UI,10'
    $CancelButton.width=110;$CancelButton.height=30;
    $Form.Controls.Add($CancelButton)
    $CancelButton.Add_Click({$global:Cancel=$True;$Form.close()})
    #
    [void]$Form.ShowDialog()
    #
    $USB=$USBDisks[$USBDiskList.SelectedIndex]
    #
    $ImagePath = $FileBrowser.FileName;
    $ISO=":"
    If($FileBrowser.FileNames -like "*\*") {
    #  Check if iso already mounted
        $ISO = (Get-DiskImage -ImagePath $ImagePath | Get-Volume).DriveLetter
    #    Mount iso
        IF (!$ISO) {Mount-DiskImage -ImagePath $ImagePath -StorageType ISO |out-null
        $ISO = (Get-DiskImage -ImagePath $ImagePath | Get-Volume).DriveLetter}
        $ISO=$ISO+":"
    }
    if ($ISO -eq ":") {$wshell.Popup("Operation cancelled. Click ""OK"" to exit.",0,"Cancel operation") | Out-Null;exit}
    #
    $Result=$wshell.Popup("All partitions and data on the USB disk will be lost. `r`n`r`nClick ""OK"" to begin creating the Windows installation disk or ""Cancel"" to exit.",0,"Warning",1+48)
    if ($result -eq 2) {DisMount-DiskImage -ImagePath $ImagePath |out-null;exit}
    #    Clear the USB stick
    Clear-Disk $usb -RemoveData -RemoveOEM -Confirm:$false
    Stop-Service ShellHWDetection
    #    Create the fat32 boot partition
    $usbfat32=(New-Partition -DiskNumber $usb -Size 700MB -AssignDriveLetter -IsActive | Format-Volume -FileSystem FAT32 -NewFileSystemLabel "BOOT").DriveLetter + ":"
    #    Create the ntfs intall partition
    $usbntfs=(New-Partition -DiskNumber $usb -UseMaximumSize -AssignDriveLetter | Format-Volume -FileSystem NTFS -NewFileSystemLabel "INSTALL").DriveLetter + ":"
    Start-Service ShellHWDetection
    #
    robocopy $iso $usbntfs /e
    robocopy $iso"\" $usbfat32"\" bootmgr bootmgr.efi
    robocopy $iso"\boot" $usbfat32"\boot" /e
    robocopy $iso"\efi" $usbfat32"\efi" /e
    robocopy $iso"\sources" $usbfat32"\sources" boot.wim
    # Eject the mounted iso image
    DisMount-DiskImage -ImagePath $ImagePath |out-null
    #done - keep a comment on the last line so the previous useful crlf never gets eaten
    
    
     
  12. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,375
    2,277
    60
    #94 freddie-o, Aug 7, 2019
    Last edited: Aug 7, 2019
    (OP)
    More cosmetic changes :)
    Code:
    <# : standard way of doing hybrid batch + powershell scripts
    @set "__CMD__=%~f0" &set "__ARGS__=%*" &powershell -noprofile -c "iex([io.file]::ReadAllText(\"%~f0\"))" &exit/b
    #>
    #
    #    This script must be executed with admin privilege
    #
    #    Test Administrator privileges
    If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(544))
    {
    #    Restart the script to get Administrator privileges and exit
         Start-Process cmd -ArgumentList ("/c call `"$Env:__CMD__`" $Env:__ARGS__") -Verb runAs
         exit
    }
    #    We have Administrator privileges
    #
    [console]::ForegroundColor = "Yellow"
    [console]::BackgroundColor = "blue"
    #
    Add-Type -AssemblyName System.Windows.Forms # Load the class System.Windows.Forms
    # Filebrowser dialog
    $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
       Multiselect = $false # One file can be chosen
        Filter = 'ISO images (*.iso)|*.iso' # Select iso files
    }
    $wshell=New-Object -ComObject Wscript.Shell
    #
    $global:SelectISO=$False
    $global:Cancel=$False
    #
    clear-host
    $Form=New-Object System.Windows.Forms.Form # Create the screen form (window)
    # Set the title and size of the window:
    $Form.Text='Windows Installation Disk v1.3'
    $Form.Width=450 ; $Form.Height=250
    $Form.AutoSize=$true # AutoSize property to make the form automatically stretch, if the elements on the form are out of bounds
    #    Select the USB disk
    # Create a drop-down list and fill it
    $Combobox=New-Object System.Windows.Forms.ComboBox
    $Combobox.DropDownStyle=[System.Windows.Forms.ComboBoxStyle]::DropDownList;
    $Combobox.Location=New-Object System.Drawing.Point(30,35)
    $Combobox.Width=380;$Combobox.height=20
    $USB=$Null
    $USBDisks=@() # array with USB disk number
    $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    While (!$Disks) {
        $Result=$wshell.Popup("No USB disk was detected. `r`n`r`Plug in your USB disk first. Click ""OK"" after you have plugged in your disk or ""Cancel"" to exit.",0,"Plug in the USB",1+64)
        if ($result -eq 2) {exit}
        $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    }
    $USBDiskList=New-Object System.Windows.Forms.ComboBox
    # $USBDiskList|gm
    $USBDiskList.Width=450
    Foreach ($USBDisk in $Disks) {
        $FriendlyName=($USBDisk.FriendlyName).PadRight(30," ").substring(0,30)  
        $Partitions = Get-Partition | Where-Object { $_.DiskNumber -eq $USBDisk.DiskNumber}
        If ($Partitions) {
            Foreach ($Partition in $Partitions) {
            $Volumes=get-volume | Where-Object {$Partition.AccessPaths -contains  $_.path }
            Foreach ($Volume in $Volumes) {
                $USBDisks+=$USBDisk.DiskNumber
                $Combobox.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
                }      
            }  
        } Else {
            $USBDisks+=$USBDisk.DiskNumber
            $USBDiskList.Items.Add((" {0,-30}| {1,1}| {2,-30}| {3:n2} GB" -f $FriendlyName, " ", " ",($USBDisk.Size/1GB)))|out-null
        }
    }
    $SelectUSBDisk=New-Object System.Windows.Forms.Label # Put the SelectUSBDisk label on the form
    $SelectUSBDisk.Location=New-Object System.Drawing.Size(30,15)
    $SelectUSBDisk.Text="Select the USB disk";$SelectUSBDisk.Font='Consolas,10'
    $SelectUSBDisk.width=150;$SelectUSBDisk.height=15;
    $Form.Controls.Add($SelectUSBDisk)
    $form.Controls.Add($Combobox)
    $Combobox.SelectedIndex=0
    
    $SelectISO=New-Object System.Windows.Forms.Label # Put the SelectISO label on the form
    $SelectISO.Location=New-Object System.Drawing.Size(30,90)
    $SelectISO.Text="Select the Windows ISO to start creating the `r`ninstallation disk.";$SelectISO.Font='Consolas,10'
    $SelectISO.width=380;$SelectISO.height=40;
    $Form.Controls.Add($SelectISO)
    $form.Controls.Add($Combobox)
    $Combobox.SelectedIndex=0
    
    $Credits=New-Object System.Windows.Forms.Label # Put the Credits label on the form
    $Credits.Location=New-Object System.Drawing.Size(30,195)
    $Credits.Text="Credits: rpo/abbodi1406/BAU/freddie-o [forums.mydigitallife.net]";$Credits.Font='Consolas,7.5'
    $Credits.width=380;$Credits.height=15;
    $Form.Controls.Add($Credits)
    $form.Controls.Add($Combobox)
    $Combobox.SelectedIndex=0
    
    $SelectISOButton=New-Object System.Windows.Forms.Button # Put the SelectISO button on the form
    $SelectISOButton.Location=New-Object System.Drawing.Size(300,160)
    $SelectISOButton.Text="Select ISO";$SelectISOButton.Font='Consolas,10'
    $SelectISOButton.width=110;$SelectISOButton.height=30;
    $Form.Controls.Add($SelectISOButton)
    $SelectISOButton.Add_Click({$global:SelectISO=$true;[void]$FileBrowser.ShowDialog()})
    $Form.Controls.Add($SelectISOButton)
    $SelectISOButton.Add_Click({$Form.close()})
    
    $CancelButton=New-Object System.Windows.Forms.Button # Put the Cancel button on the form
    $CancelButton.Location=New-Object System.Drawing.Size(180,160)
    $CancelButton.Size=New-Object System.Drawing.Size(120,20)
    $CancelButton.Text="Cancel";$CancelButton.Font='Consolas,10'
    $CancelButton.width=110;$CancelButton.height=30;
    $Form.Controls.Add($CancelButton)
    $CancelButton.Add_Click({$global:Cancel=$True;$Form.close()})
    #
    [void]$Form.ShowDialog()
    #
    $USB=$USBDisks[$USBDiskList.SelectedIndex]
    #
    $ImagePath = $FileBrowser.FileName;
    $ISO=":"
    If($FileBrowser.FileNames -like "*\*") {
    #  Check if iso already mounted
        $ISO = (Get-DiskImage -ImagePath $ImagePath | Get-Volume).DriveLetter
    #    Mount iso
        IF (!$ISO) {Mount-DiskImage -ImagePath $ImagePath -StorageType ISO |out-null
        $ISO = (Get-DiskImage -ImagePath $ImagePath | Get-Volume).DriveLetter}
        $ISO=$ISO+":"
    }
    if ($ISO -eq ":") {$wshell.Popup("Operation cancelled. Click ""OK"" to exit.",0,"Cancel operation") | Out-Null;exit}
    #
    $Result=$wshell.Popup("All partitions and data on the USB disk will be lost. `r`n`r`nAre you sure you want to start creating the Windows installation disk?",0,"Warning",1+48)
    if ($result -eq 2) {DisMount-DiskImage -ImagePath $ImagePath |out-null;exit}
    #    Clear the USB stick
    Clear-Disk $usb -RemoveData -RemoveOEM -Confirm:$false
    Stop-Service ShellHWDetection
    #    Create the fat32 boot partition
    $usbfat32=(New-Partition -DiskNumber $usb -Size 700MB -AssignDriveLetter -IsActive | Format-Volume -FileSystem FAT32 -NewFileSystemLabel "BOOT").DriveLetter + ":"
    #    Create the ntfs intall partition
    $usbntfs=(New-Partition -DiskNumber $usb -UseMaximumSize -AssignDriveLetter | Format-Volume -FileSystem NTFS -NewFileSystemLabel "INSTALL").DriveLetter + ":"
    Start-Service ShellHWDetection
    #
    robocopy $iso $usbntfs /e
    robocopy $iso"\" $usbfat32"\" bootmgr bootmgr.efi
    robocopy $iso"\boot" $usbfat32"\boot" /e
    robocopy $iso"\efi" $usbfat32"\efi" /e
    robocopy $iso"\sources" $usbfat32"\sources" boot.wim
    # Eject the mounted iso image
    DisMount-DiskImage -ImagePath $ImagePath |out-null
    #done - keep a comment on the last line so the previous useful crlf never gets eaten
    
    
     
  13. vigipirate

    vigipirate MDL Senior Member

    Feb 24, 2011
    349
    87
    10
    Hello sir
    i do not know if i can ask you because you look more than me professional in the script gives you the link for the site
    http://www.alexcomputerbubble.com/winpe-gui-windows-10/
    attach the script simply modify the script as in the picture 1 a external usb folder
    I have
    : \ Image \ Wim folder no letter
    so that it finds my usb key or external hard drive so that it finds the way of the key usb because me I am no letter is not realizing the capture
     
  14. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,375
    2,277
    60
    #96 freddie-o, Aug 7, 2019
    Last edited: Aug 7, 2019
    (OP)
    Sorry to disappoint you @vigipirate but I have a very limited knowledge about scripting. o_O The scripts I post here in MDL were not done by me but by members here that are more knowledgeable. If it were not for them I wouldn't be able to put these scripts together. I just modify them the best I can. Try asking the members who might be able to help.
     
  15. rpo

    rpo MDL Expert

    Jan 3, 2010
    1,445
    1,421
    60
    Corrected some errors and deleted unnecessary instructions.
    Code:
    <# : standard way of doing hybrid batch + powershell scripts
    @set "__CMD__=%~f0" &set "__ARGS__=%*" &powershell -noprofile -c "iex([io.file]::ReadAllText(\"%~f0\"))" &exit/b
    #>
    #
    #    This script must be executed with admin privilege
    #
    #    Test Administrator privileges
    If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(544))
    {
    #    Restart the script to get Administrator privileges and exit
         Start-Process cmd -ArgumentList ("/c call `"$Env:__CMD__`" $Env:__ARGS__") -Verb runAs
         exit
    }
    #    We have Administrator privileges
    #
    $pswindow = $host.ui.rawui          # create a reference to the console’s UI.RawUI child object
    $pswindow.windowtitle = "Create USB boot disk"
    [console]::ForegroundColor = "Yellow"
    [console]::BackgroundColor = "blue"
    #
    Add-Type -AssemblyName System.Windows.Forms # Load the class System.Windows.Forms
    # Filebrowser dialog
    $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
       Multiselect = $false # One file can be chosen
        Filter = 'ISO images (*.iso)|*.iso' # Select iso files
    }
    $wshell=New-Object -ComObject Wscript.Shell
    #
    clear-host
    #
    $Form=New-Object System.Windows.Forms.Form # Create the screen form (window)
    # Set the title and size of the window:
    $Form.Text='Windows Installation Disk v1.3'
    $Form.Width=450 ; $Form.Height=270
    $Form.AutoSize=$true # AutoSize property to make the form automatically stretch, if the elements on the form are out of bounds
    #
    #    Select the USB disk
    # Create a 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(30,35)
    $USBDiskList.Width=380;$USBDiskList.height=20
    $Form.Controls.Add($SelectUSBDisk)
    $USB=$Null
    $USBDisks=@() # array with USB disk number
    $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    While (!$Disks) {
        $Result=$wshell.Popup("No USB disk was detected. `r`n`r`Plug in your USB disk first. Click ""OK"" after you have plugged in your disk or ""Cancel"" to exit.",0,"Plug in the USB",1+64)
            if ($result -eq 2) {$wshell.Popup("The script was cancelled.",0,"Cancel script",0+64) | Out-Null;exit}
        $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    }
    Foreach ($USBDisk in $Disks) {
        $FriendlyName=($USBDisk.FriendlyName).PadRight(30," ").substring(0,30)
        $Partitions = Get-Partition | Where-Object { $_.DiskNumber -eq $USBDisk.DiskNumber}
        If ($Partitions) {
            Foreach ($Partition in $Partitions) {
            $Volumes=get-volume | Where-Object {$Partition.AccessPaths -contains  $_.path }
            Foreach ($Volume in $Volumes) {
                $USBDisks+=$USBDisk.DiskNumber
                $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
                }     
            }
        } Else {
            $USBDisks+=$USBDisk.DiskNumber
            $USBDiskList.Items.Add((" {0,-30}| {1,1}| {2,-30}| {3:n2} GB" -f $FriendlyName, " ", " ",($USBDisk.Size/1GB)))|out-null
        }
    }
    
    $SelectUSBDisk=New-Object System.Windows.Forms.Label # Put the SelectUSBDisk label on the form
    $SelectUSBDisk.Location=New-Object System.Drawing.Size(30,15)
    $SelectUSBDisk.Text="Select the USB disk";$SelectUSBDisk.Font='Consolas,10'
    $SelectUSBDisk.width=150;$SelectUSBDisk.height=15;
    $form.Controls.Add($USBDiskList)
    $USBDiskList.SelectedIndex=0
    
    $Credits=New-Object System.Windows.Forms.Label # Put the Credits label on the form
    $Credits.Location=New-Object System.Drawing.Size(30,215)
    $Credits.Text="Script by: rpo/abbodi1406/BAU/freddie-o [forums.mydigitallife.net]";$Credits.Font='Consolas,7.5'
    $Credits.width=380;$Credits.height=15;
    $Form.Controls.Add($Credits)
    
    $SelectISOButton=New-Object System.Windows.Forms.Button # Put the SelectISO button on the form
    $SelectISOButton.Location=New-Object System.Drawing.Size(300,160)
    $SelectISOButton.Text="Select ISO";$SelectISOButton.Font='Consolas,10'
    $SelectISOButton.Size=New-Object System.Drawing.Size(120,20)
    $SelectISOButton.width=110;$SelectISOButton.height=30;
    $Form.Controls.Add($SelectISOButton)
    $SelectISOButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    
    $CancelButton=New-Object System.Windows.Forms.Button # Put the Cancel button on the form
    $CancelButton.Location=New-Object System.Drawing.Size(180,160)
    $CancelButton.Text="Cancel";$CancelButton.Font='Consolas,10'
    $CancelButton.Size=New-Object System.Drawing.Size(120,20)
    $CancelButton.width=110;$CancelButton.height=30;
    $Form.Controls.Add($CancelButton)
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    #
    $result = $form.ShowDialog()
    if ($result -eq "Cancel"){$wshell.Popup("The script was cancelled.",0,"Cancel script",0+64) | Out-Null;exit}
    #
    $USB=$USBDisks[$USBDiskList.SelectedIndex]
    #
    [void]$FileBrowser.ShowDialog()
    #
    $ImagePath = $FileBrowser.FileName;
    if ($ImagePath -eq ""){$wshell.Popup("The script was cancelled.",0,"Cancel script",0+64) | Out-Null;exit}
    $ISO=":"
    If($FileBrowser.FileNames -like "*\*") {
    #  Check if iso already mounted
        $ISO = (Get-DiskImage -ImagePath $ImagePath | Get-Volume).DriveLetter
    #    Mount iso
        IF (!$ISO) {Mount-DiskImage -ImagePath $ImagePath -StorageType ISO |out-null
        $ISO = (Get-DiskImage -ImagePath $ImagePath | Get-Volume).DriveLetter}
        $ISO=$ISO+":"
    }
    if ($ISO -eq ":") {$wshell.Popup("The script was cancelled.",0,"Cancel operation",0+64) | Out-Null;exit}
    #
    $Result=$wshell.Popup("All partitions and data on the USB disk will be lost. `r`n`r`nAre you sure you want to start creating the Windows installation disk?",0,"Warning",1+48)
    if ($result -eq 2) {DisMount-DiskImage -ImagePath $ImagePath |out-null;exit}
    #    Clear the USB stick
    Clear-Disk $usb -RemoveData -RemoveOEM -Confirm:$false
    Stop-Service ShellHWDetection
    #    Create the fat32 boot partition
    $usbfat32=(New-Partition -DiskNumber $usb -Size 700MB -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 "INSTALL").DriveLetter + ":"
    Start-Service ShellHWDetection
    #
    robocopy $iso $usbntfs /e
    robocopy $iso"\" $usbfat32"\" bootmgr bootmgr.efi
    robocopy $iso"\boot" $usbfat32"\boot" /e
    robocopy $iso"\efi" $usbfat32"\efi" /e
    robocopy $iso"\sources" $usbfat32"\sources" boot.wim
    
    Copy-Item $ISO"\boot\bcd" $env:temp -Force
    & bcdedit /store ($env:temp + "\bcd") /set '{default}' bootmenupolicy Legacy
    Set-ItemProperty -Path ($env:temp + "\bcd") -Name IsReadOnly -Value $true
    Copy-Item ($env:temp + "\bcd") $usbfat32"\boot\bcd" -Force
    Copy-Item ($env:temp + "\bcd") $usbntfs"\boot\bcd" -Force
    Copy-Item $ISO"\EFI\Microsoft\boot\bcd" $env:temp -Force
    & bcdedit /store ($env:temp + "\bcd") /set '{default}' bootmenupolicy Legacy
    Set-ItemProperty -Path ($env:temp + "\bcd") -Name IsReadOnly -Value $true
    Copy-Item ($env:temp + "\bcd") $usbfat32"\EFI\Microsoft\boot\bcd" -Force
    Copy-Item ($env:temp + "\bcd") $usbntfs"\EFI\Microsoft\boot\bcd" -Force
    remove-item ($env:temp + "\bcd") -force
    remove-item ($env:temp + "\bcd.*") -force
    
    # Eject the mounted iso image
    DisMount-DiskImage -ImagePath $ImagePath |out-null
    #done - keep a comment on the last line so the previous useful crlf never gets eaten
    
    Also :
    The ntfs partition is marked active
    Modified the bcd store to add bootmenupolicy legacy. This is facultative. I use it because a have an old Sony VAIO laptop which take many minutes to load boot.wim and i use to have a progression bar.
     
  16. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,375
    2,277
    60
    #99 freddie-o, Aug 8, 2019
    Last edited: Aug 8, 2019
    (OP)
    EDIT:

    Got this error when starting the script
    Code:
    H:\> : standard way of doing hybrid batch + powershell scripts 0<#
    The system cannot find the file specified.

    Also it seems "USBDiskDropDownList" and "SelectUSBDiskLabel" are conflicting with each other.
     
  17. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,375
    2,277
    60
    #100 freddie-o, Aug 8, 2019
    Last edited: Aug 8, 2019
    (OP)
    Is setting the partition as active still needed? In older versions of Windows?

    The default progress bar of Windows ISO doesn't show up on old PCs? Or the older versions of Windows ISOs don't have a progress bar?