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,606
    2,676
    60
    #81 freddie-o, Aug 5, 2019
    Last edited: Aug 5, 2019
    Updated the [Windows installation disk] script in the OP with v3. A new GUI and script elevates and runs using a hybrid batch-powershell format.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. rpo

    rpo MDL Expert

    Jan 3, 2010
    1,505
    1,518
    60
    The script with some housekeeping
    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'
    $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("Please mount USB stick. Click ""OK"" when ready`r`nor ""Cancel"" to exit.",0,"Warning",1+48)
        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]
    #
    if ($Cancel){$wshell.Popup("The script was cancelled.",0,"Cancel") | Out-Null;exit}
    if ($USB -eq $Null){$wshell.Popup("No USB disk detected.",0,"Error") | Out-Null;exit}
    if (!$global:SelectISO){$wshell.Popup("No ISO image selected.",0,"Error") | Out-Null;exit}
    #
    $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
    
     
  3. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,606
    2,676
    60
    So we don't need this anymore?
    Code:
    if ($USB -eq $Null){$wshell.Popup("No USB disk detected.",0,"Error") | Out-Null;exit}
    Can this scenario
    Code:
    $Result=$wshell.Popup("Please mount USB stick. Click ""OK"" when ready`r`nor ""Cancel"" to exit.",0,"Warning",1+48)
        if ($result -eq 2) {exit}
        $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    be applied here too?
    Code:
    if (!$global:SelectISO){$wshell.Popup("No ISO image selected.",0,"Error") | Out-Null;exit}
    so instead of exiting the script, you can go back to the GUI to select the ISO?
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,606
    2,676
    60
    #84 freddie-o, Aug 6, 2019
    Last edited: Aug 6, 2019
    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)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. rpo

    rpo MDL Expert

    Jan 3, 2010
    1,505
    1,518
    60
    #85 rpo, Aug 6, 2019
    Last edited: Aug 6, 2019
    (OP)
    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()}
    }
     
  6. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,606
    2,676
    60
    #86 freddie-o, Aug 6, 2019
    Last edited: Aug 6, 2019
    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.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. rpo

    rpo MDL Expert

    Jan 3, 2010
    1,505
    1,518
    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()}
    }
     
  8. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,606
    2,676
    60
    #88 freddie-o, Aug 6, 2019
    Last edited: Aug 6, 2019
    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.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. rpo

    rpo MDL Expert

    Jan 3, 2010
    1,505
    1,518
    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.
     
  10. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,606
    2,676
    60
    Can we add a script for the "X" button?
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  11. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,606
    2,676
    60
    #91 freddie-o, Aug 6, 2019
    Last edited: Aug 6, 2019
    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
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  12. rpo

    rpo MDL Expert

    Jan 3, 2010
    1,505
    1,518
    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
    
     
  13. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,606
    2,676
    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
    
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  14. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,606
    2,676
    60
    #94 freddie-o, Aug 6, 2019
    Last edited: Aug 6, 2019
    Deleted.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  15. rpo

    rpo MDL Expert

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

    freddie-o MDL Expert

    Jul 29, 2009
    1,606
    2,676
    60
    #96 freddie-o, Aug 6, 2019
    Last edited: Aug 6, 2019
    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
    
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  17. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,606
    2,676
    60
    #97 freddie-o, Aug 7, 2019
    Last edited: Aug 7, 2019
    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
    
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  18. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,606
    2,676
    60
    #99 freddie-o, Aug 7, 2019
    Last edited: Aug 7, 2019
    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.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  19. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,606
    2,676
    60
    Updated the OP with Windows Installation Disk v1.3
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...