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
    More changes to the combo box
    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 = "Script by: rpo/abbodi1406/BAU/freddie-o [forums.mydigitallife.net]"
    [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.4'
    $Form.Width=420 ; $Form.Height=180
    $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(20,35)
    $USBDiskList.Width=365;$USBDiskList.height=20;$USBDiskList.Font='Courier New,9'
    $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("Plug in your USB disk first, then click ""OK"", to continue. `r`n`r`nTo close this script, click ""Cancel"".",0,"Plug in the USB disk",1+64)
            if ($result -eq 2) {Out-Null;exit}
        $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    }
    Foreach ($USBDisk in $Disks) {
        $FriendlyName=($USBDisk.FriendlyName).PadRight(36," ").substring(0,36)
        $USBDisks+=$USBDisk.DiskNumber
        $USBDiskList.Items.Add(("{0,-38}{1:n2} GB" -f $FriendlyName,($USBDisk.Size/1GB)))|out-null
        $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,-2}({1,1}:){2,-25}[{3:n2} GB]" -f " ", ($Partition.DriveLetter), $Volume.FileSystemLabel.PadRight(25," ").substring(0,25), ($Partition.Size/1GB)))|out-null
                }     
            }
        }
    }
    $form.Controls.Add($USBDiskList)
    $USBDiskList.SelectedIndex=0
    
    $SelectUSB=New-Object System.Windows.Forms.Label # Put the SelectUSB label on the form
    $SelectUSB.Location=New-Object System.Drawing.Size(20,18)
    $SelectUSB.Text="Select USB disk"
    $SelectUSB.width=380;$SelectUSB.height=15;
    $Form.Controls.Add($SelectUSB)
    
    $SelectISOButton=New-Object System.Windows.Forms.Button # Put the SelectISO button on the form
    $SelectISOButton.Location=New-Object System.Drawing.Size(295,100)
    $SelectISOButton.Text="Select ISO"
    $SelectISOButton.Size=New-Object System.Drawing.Size(120,20)
    $SelectISOButton.width=90;$SelectISOButton.height=25;
    $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(190,100)
    $CancelButton.Text="Cancel"
    $CancelButton.Size=New-Object System.Drawing.Size(120,20)
    $CancelButton.width=90;$CancelButton.height=25;
    $Form.Controls.Add($CancelButton)
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    #
    if ($form.ShowDialog() -eq "Cancel"){$wshell.Popup("The script was cancelled.",2,"Cancel script",0+64) | Out-Null;exit}
    #
    $USB=$USBDisks[$USBDiskList.SelectedIndex]
    #
    if ($FileBrowser.ShowDialog() -eq "Cancel"){$wshell.Popup("The script was cancelled.",2,"Cancel script",0+64) | Out-Null;exit}
    #
    $ImagePath = $FileBrowser.FileName;
    if ($ImagePath -eq ""){$wshell.Popup("The script was cancelled.",2,"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.",2,"Cancel script",0+64) | Out-Null;exit}
    #
    $Result=$wshell.Popup("Warning! Your USB disk will be converted to MBR scheme, repartitioned and reformatted. All data will be lost. `r`n`r`nAre you sure you want to continue? `r`n`r`nTo continue, click ""Yes"". To close this script, click ""No"".",0,"Create the Windows installation disk",4+48)
    if ($result -eq 7) {DisMount-DiskImage -ImagePath $ImagePath |out-null;$wshell.Popup("The script was cancelled.",2,"Cancel script",0+64) | Out-Null;exit}
    #    Clear the USB stick
    Clear-Disk $usb -RemoveData -RemoveOEM -Confirm:$false
    set-disk $usb -partitionstyle mbr
    Stop-Service ShellHWDetection |out-null
    #    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
    
    # Eject the mounted iso image
    DisMount-DiskImage -ImagePath $ImagePath |out-null
    $wshell.Popup("The Windows Installation Disk is complete.",0,"Complete",0+64) | Out-Null;exit
    #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...
  2. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,606
    2,676
    60
    Updated the script in the OP with latest changes
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. rpo

    rpo MDL Expert

    Jan 3, 2010
    1,504
    1,514
    60
    #163 rpo, Aug 22, 2019
    Last edited: Aug 22, 2019
    (OP)
    Following is the script i use for my own usage.
    Code:
    <# : standard way of doing hybrid batch + powershell scripts
    @title Windows Installation Disk v?.? by: rpo/abbodi1406/BAU/freddie-o [forums.mydigitallife.net]
    @set "ScriptPath=%~f0" &set "__ARGS__=%*" &powershell -noprofile -c "iex(Get-Content('%~f0') -raw)" &exit/b
    #>
    #
    #    This script must be executed with admin privilege
    #
    #    Test Administrator privileges
    If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
    {
    #    Restart the script to get Administrator privileges and exit
         Start-Process cmd -ArgumentList ("/c call `"$Env:ScriptPath`" $Env:__ARGS__") -Verb runAs
         exit
    }
    #    We have Administrator privileges
    $Env:ScriptPath=$Env:__ARGS__=""  # delete environment variables
    #
    $pswindow = $host.ui.rawui          # create a reference to the console’s UI.RawUI child object
    # $newsize = $pswindow.buffersize   # set the buffer size
    # $newsize.height = 3000            # 3000 lines
    # $newsize.width = 150                # 150 characters
    # $pswindow.buffersize = $newsize    # set the BufferSize properties
    # $newsize = $pswindow.buffersize   # assign the BufferSize properties to a variable named $newsize
    # $pswindow.buffersize = $newsize    # assign the values to the actual console window
    # $newsize = $pswindow.windowsize    # set the window size
    # $newsize.height = [Math]::Min(50, $host.ui.rawui.MaxWindowSize.height)
    # $newsize.width = [Math]::Min(150, $host.ui.rawui.MaxWindowSize.Width)
    # $pswindow.windowsize = $newsize
    $pswindow.windowtitle = "Windows Installation Disk v?.? by: rpo/abbodi1406/BAU/freddie-o [forums.mydigitallife.net]"
    [console]::ForegroundColor = "Yellow"
    [console]::BackgroundColor = "blue"
    #
    Add-Type -AssemblyName System.Windows.Forms # Load the class System.Windows.Forms
    #
    Function Get-FileName
    {  
    # Filebrowser dialog
    $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
       Multiselect = $false # One file can be chosen
       title="Select your ISO image file"
    #   ShowHelp = $True
        Filter = 'ISO images (*.iso)|*.iso' # Select only iso files
    }
    IF ($FileBrowser.ShowDialog() -eq "Cancel"){Cancel}
    $FileBrowser.filename    # return the file name
    }
    $wshell=New-Object -ComObject Wscript.Shell
    #
    # .Net methods for hiding/showing the console in the background
    Add-Type -Name Window -Namespace Console -MemberDefinition '
    [DllImport("Kernel32.dll")]
    public static extern IntPtr GetConsoleWindow();
    
    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
    '
    # Hide = 0,
    # ShowNormal = 1,
    # ShowMinimized = 2,
    # ShowMaximized = 3,
    # Maximize = 3,
    # ShowNormalNoActivate = 4,
    # Show = 5,
    # Minimize = 6,
    # ShowMinNoActivate = 7,
    # ShowNoActivate = 8,
    # Restore = 9,
    # ShowDefault = 10,
    # ForceMinimized = 11
    #
    function Show-Console
    {
        $consolePtr = [Console.Window]::GetConsoleWindow()
        [Console.Window]::ShowWindow($consolePtr, 1)
    }
    function Hide-Console
    {
        $consolePtr = [Console.Window]::GetConsoleWindow()
        [Console.Window]::ShowWindow($consolePtr, 0)
    }
    Function Cancel
    {
        $wshell.Popup("The script was cancelled.",2,"Cancel script",0+64) | Out-Null
        [Environment]::Exit(1)
    }
    clear-host
    #
    $Global:ImagePath=""
    $USB=$Null
    $Form=New-Object System.Windows.Forms.Form # Create the screen form (window)
    $Form.TopMost = $True
    # Set the size of the window:
    # $Form.FormBorderStyle = "none"
    $Form.ControlBox = $False
    $Form.Text="Select USB disk and ISO image file"
    $Form.Width=490 ; $Form.Height=250
    $Form.AutoSize=$true # AutoSize property to make the form automatically stretch, if the elements on the form are out of bounds
    $Form.KeyPreview = $True
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Enter"){$OKButton.PerformClick()}})         # Enter = click OK button
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Escape"){$CancelButton.PerformClick()}})    # Escape = click on Cancel button
    $Form.BackColor = "Blue"
    
    $OKButton=New-Object System.Windows.Forms.Button # Put the SelectISO button on the form
    $OKButton.Location=New-Object System.Drawing.Size(295,175)
    $OKButton.Text="OK"
    $OKButton.Font='Default Font,9'
    $OKButton.Size=New-Object System.Drawing.Size(120,20)
    $OKButton.width=130
    $OKButton.height=25
    $OKButton.Backcolor = [System.Drawing.Color]::LightGray
    $OKButton.Add_MouseHover({$OKButton.backcolor = [System.Drawing.Color]::LightGreen})
    $OKButton.Add_MouseLeave({$OKButton.backcolor = [System.Drawing.Color]::LightGray})
    $Form.Controls.Add($OKButton)
    $OKButton.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(155,175)
    $CancelButton.Text="Cancel"
    $CancelButton.Font='Default Font,9'
    $CancelButton.Size=New-Object System.Drawing.Size(120,20)
    $CancelButton.width=130
    $CancelButton.height=25;
    $CancelButton.Backcolor = [System.Drawing.Color]::LightGray
    $CancelButton.Add_MouseHover({$CancelButton.backcolor = [System.Drawing.Color]::Red})
    $CancelButton.Add_MouseLeave({$CancelButton.backcolor = [System.Drawing.Color]::LightGray})
    $Form.Controls.Add($CancelButton)
    # $CancelButton.Add_Click({$Form.Close();cancel})
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    
    $SelectUSBDisk=New-Object System.Windows.Forms.Label # Put the SelectUSBDisk label on the form
    $SelectUSBDisk.Location=New-Object System.Drawing.Size(30,15)
    $SelectUSBDisk.Font='Consolas,10'
    $SelectUSBDisk.width=400
    $SelectUSBDisk.height=30
    $SelectUSBDisk.ForeColor = [System.Drawing.Color]::White
    $Form.Controls.Add($SelectUSBDisk)
    
    $SelectISO=New-Object System.Windows.Forms.Label # Put the SelectUSBDisk label on the form
    $SelectISO.Location=New-Object System.Drawing.Size(30,110)
    $SelectISO.Font='Consolas,10'
    $SelectISO.width=400
    $SelectISO.height=30
    $SelectISO.text="Select the ISO image file :"
    $SelectISO.ForeColor = [System.Drawing.Color]::White
    
    $SelectISOButton=New-Object System.Windows.Forms.Button # Put the SelectISO button on the form
    $SelectISOButton.Location=New-Object System.Drawing.Size(415,140)
    $SelectISOButton.Text="ISO"
    $SelectISOButton.Font='Default Font,7'
    $SelectISOButton.Size=New-Object System.Drawing.Size(20,20)
    $SelectISOButton.width=30
    $SelectISOButton.height=20;
    $SelectISOButton.Backcolor = [System.Drawing.Color]::LightGray
    $SelectISOButton.Add_MouseHover({$SelectISOButton.backcolor = [System.Drawing.Color]::LightGreen})
    $SelectISOButton.Add_MouseLeave({$SelectISOButton.backcolor = [System.Drawing.Color]::LightGray})
    
    $ISOFile=New-Object System.Windows.Forms.Label # Put the ISO file name on the form
    $ISOFile.Location=New-Object System.Drawing.Size(30,140)
    $ISOFile.Text=" "
    $ISOFile.Font='Courier New,10'
    $ISOFile.Size=New-Object System.Drawing.Size(20,20)
    $ISOFile.width=385
    $ISOFile.height=20;
    $ISOFile.Backcolor = [System.Drawing.Color]::White
    $ISOFile.ForeColor = [System.Drawing.Color]::Black
    
    $Warning=New-Object System.Windows.Forms.Label
    $Warning.Location=New-Object System.Drawing.Size(30,15)
    $Warning.Font='Consolas,10'
    $Warning.width=430
    $Warning.height=75
    $Warning.text="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?"
    $Warning.ForeColor = [System.Drawing.Color]::White
    
    #
    #    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,45)
    $USBDiskList.Width=415
    $USBDiskList.height=20
    $USBDiskList.Font='Courier New,10'
    $Form.Controls.Add($SelectUSBDiskList)
    $USBDisks=@() # array with USB disk number
    $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    Hide-Console # Hide console display
    
    While (!$Disks) {
        $SelectUSBDisk.Text="Please mount USB disk. Click ""OK"" when ready`r`nor ""Cancel"" to exit."
        if ($form.ShowDialog() -eq "Cancel"){cancel}
        $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    }
    
    $Form.Controls.Add($SelectISO)
    $Form.Controls.Add($ISOFile)
    $Form.Controls.Remove($OKButton)
    $SelectISOButton.Add_Click(
        {$Global:ImagePath = Get-FileName # get the iso image path with file browser dialog
         $ImagePath= Split-Path -Path $Global:ImagePath -leaf # extract filename and extension (iso)
         $ISOFile.Text = $ImagePath.PadRight(100," ").substring(0,43)+"..."
         $OKButton.Text="Create USB disk"
         $Form.Controls.Add($OKButton)})
       
    Foreach ($USBDisk in $Disks) {
        $FriendlyName=($USBDisk.FriendlyName).PadRight(40," ").substring(0,40)
        $USBDisks+=$USBDisk.DiskNumber
        $USBDiskList.Items.Add(("{0,-41}|{1:n2} GB" -f $FriendlyName,($USBDisk.Size/1GB)))|out-null
        $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,-7}({1,1}:){2,-3}|{3:n2} GB" -f " ", ($Partition.DriveLetter), $Volume.FileSystemLabel.PadRight(30," ").substring(0,30), ($Partition.Size/1GB)))|out-null
                }    
            }
        }
    }
    $form.Controls.Add($USBDiskList)
    $USBDiskList.SelectedIndex=0
    
    $SelectUSBDisk.Text="Select the USB disk/partition :"
    #
    $Form.Controls.Add($SelectISOButton)
    #
    if ($form.ShowDialog() -eq "Cancel") {Cancel}
    #
    # At this point the mounted USB disk and the iso image file path are defined
    #
    $USB=$USBDisks[$USBDiskList.SelectedIndex]
    #
    # Mount the image file if not already mounted and get the drive letter
    #
    if (ImagePath -eq ""){Cancel} # this can't happen, but keep the test
    #  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 ":") {Cancel}
    #
    # Ready to create the USB disk. Ask for confirmation
    #
    $Form.Controls.Remove($USBDiskList)
    $Form.Controls.Remove($SelectUSBDisk)
    $Form.Controls.Remove($SelectISOButton)
    $Form.Controls.Remove($ISOFile)
    $Form.Controls.Remove($SelectISO)
    $OKButton.Text="Continue"
    $Form.Controls.Add($OKButton)
    $Form.Controls.Add($Warning)
    if ($form.ShowDialog() -eq "Cancel") {DisMount-DiskImage -ImagePath $ImagePath |out-null;Cancel}
    Clear-Host
    $pswindow.windowtitle = "Create Windows Installation Disk"
    Show-Console | Out-Null # Restore console display
    #    Clear the USB stick
    Clear-Disk $usb -RemoveData -RemoveOEM -Confirm:$false
    set-disk $usb -partitionstyle mbr
    Stop-Service ShellHWDetection  |out-null
    #    Create the fat32 boot partition
    $usbfat32=(New-Partition -DiskNumber $usb -Size  1GB -AssignDriveLetter | Format-Volume -FileSystem FAT32 -NewFileSystemLabel "BOOT").DriveLetter + ":"
    #    Create the ntfs intall partition
    $usbntfs=(New-Partition -DiskNumber $usb -UseMaximumSize -AssignDriveLetter -IsActive | Format-Volume -FileSystem NTFS -NewFileSystemLabel "INSTALL").DriveLetter + ":"
    Start-Service ShellHWDetection |out-null
    #
    robocopy $iso $usbntfs /e /xf $iso\boot\bcd $iso\efi\microsoft\boot\bcd
    robocopy $iso"\" $usbfat32"\" bootmgr bootmgr.efi
    robocopy $iso"\boot" $usbfat32"\boot" /e /xf $iso\boot\bcd
    robocopy $iso"\efi" $usbfat32"\efi" /e /xf $iso\efi\microsoft\boot\bcd
    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
    $wshell.Popup("The Windows Installation Disk is complete.",0,"Complete",0+64) | Out-Null
    #done - keep a comment on the last line so the previous useful crlf never gets eaten
    
     
  4. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,606
    2,676
    60
    Fancy :good3:
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,606
    2,676
    60
    I just added a few modifications (pop-ups) to the original script in the OP.

    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 = "Script by: rpo/abbodi1406/BAU/freddie-o [forums.mydigitallife.net]"
    [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.4'
    $Form.Width=420 ; $Form.Height=180
    $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(20,35)
    $USBDiskList.Width=365;$USBDiskList.height=20;$USBDiskList.Font='Courier New,9'
    $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("Please plug in your USB disk.",0,"Plug in the USB disk",1+64)
            if ($result -eq 2) {$wshell.Popup("The script was cancelled.",2,"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(36," ").substring(0,36)
        $USBDisks+=$USBDisk.DiskNumber
        $USBDiskList.Items.Add(("{0,-39}{1:n2} GB" -f $FriendlyName,($USBDisk.Size/1GB)))|out-null
        $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,-2}({1,1}:){2,-27}[{3:n2} GB]" -f " ", ($Partition.DriveLetter), $Volume.FileSystemLabel.PadRight(25," ").substring(0,25), ($Partition.Size/1GB)))|out-null
                }     
            }
        }
    }
    $form.Controls.Add($USBDiskList)
    $USBDiskList.SelectedIndex=0
    
    $SelectUSB=New-Object System.Windows.Forms.Label # Put the SelectUSB label on the form
    $SelectUSB.Location=New-Object System.Drawing.Size(20,18)
    $SelectUSB.Text="Select USB disk"
    $SelectUSB.width=380;$SelectUSB.height=15;
    $Form.Controls.Add($SelectUSB)
    
    $SelectISOButton=New-Object System.Windows.Forms.Button # Put the SelectISO button on the form
    $SelectISOButton.Location=New-Object System.Drawing.Size(295,100)
    $SelectISOButton.Text="Select ISO"
    $SelectISOButton.Size=New-Object System.Drawing.Size(120,20)
    $SelectISOButton.width=90;$SelectISOButton.height=25;
    $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(190,100)
    $CancelButton.Text="Cancel"
    $CancelButton.Size=New-Object System.Drawing.Size(120,20)
    $CancelButton.width=90;$CancelButton.height=25;
    $Form.Controls.Add($CancelButton)
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    #
    if ($form.ShowDialog() -eq "Cancel"){$wshell.Popup("The script was cancelled.",2,"Cancel script",0+64) | Out-Null;exit}
    #
    $USB=$USBDisks[$USBDiskList.SelectedIndex]
    #
    if ($FileBrowser.ShowDialog() -eq "Cancel"){$wshell.Popup("The script was cancelled.",2,"Cancel script",0+64) | Out-Null;exit}
    #
    $ImagePath = $FileBrowser.FileName;
    if ($ImagePath -eq ""){$wshell.Popup("The script was cancelled.",2,"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.",2,"Cancel script",0+64) | Out-Null;exit}
    #
    $Result=$wshell.Popup("Do you want to begin creating the Windows Installation Disk?",0,"Create the Windows Installation Disk",4+64)
    if ($result -eq 7) {DisMount-DiskImage -ImagePath $ImagePath |out-null;$wshell.Popup("The script was cancelled.",2,"Cancel script",0+64) | Out-Null;exit}
    $Result=$wshell.Popup("Warning! Your USB disk will be converted to MBR scheme, repartitioned and reformatted. All data will be lost. `r`n`r`nAre you sure you want to continue?",0,"Create the Windows Installation Disk",4+48)
    if ($result -eq 7) {DisMount-DiskImage -ImagePath $ImagePath |out-null;$wshell.Popup("The script was cancelled.",2,"Cancel script",0+64) | Out-Null;exit}
    #    Clear the USB stick
    Clear-Disk $usb -RemoveData -RemoveOEM -Confirm:$false
    set-disk $usb -partitionstyle mbr
    Stop-Service ShellHWDetection |out-null
    #    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
    
    # Eject the mounted iso image
    DisMount-DiskImage -ImagePath $ImagePath |out-null
    $wshell.Popup("The Windows Installation Disk is complete.",0,"Complete",0+64) | Out-Null;exit
    #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...
  6. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,606
    2,676
    60
    #166 freddie-o, Aug 23, 2019
    Last edited: Aug 23, 2019
    I have a couple of ideas for your script.
    Use pop-ups for the "Please mount USB disk" notification, "Select the ISO" / open the file browser and the final "Warning" message.
    The System.Windows.Forms is just to "Create USB disk"
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,606
    2,676
    60
    #167 freddie-o, Aug 25, 2019
    Last edited: Aug 27, 2019
    Modified the looks of your script.

    Code:
    <# : standard way of doing hybrid batch + powershell scripts
    @title Script by: rpo/abbodi1406/BAU/freddie-o [forums.mydigitallife.net]
    @set "ScriptPath=%~f0" &set "__ARGS__=%*" &powershell -noprofile -c "iex(Get-Content('%~f0') -raw)" &exit/b
    #>
    #
    #    This script must be executed with admin privilege
    #
    #    Test Administrator privileges
    If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
    {
    #    Restart the script to get Administrator privileges and exit
         Start-Process "$Env:ScriptPath" -ArgumentList "$Env:__ARGS__" -Verb runAs; exit
    }
    #    We have Administrator privileges
    #
    $Env:ScriptPath=""; $Env:ScriptPath=""  # delete environment variables
    #
    $pswindow = $host.ui.rawui          # create a reference to the console’s UI.RawUI child object
    # $newsize = $pswindow.buffersize   # set the buffer size
    # $newsize.height = 3000            # 3000 lines
    # $newsize.width = 150                # 150 characters
    # $pswindow.buffersize = $newsize    # set the BufferSize properties
    # $newsize = $pswindow.buffersize   # assign the BufferSize properties to a variable named $newsize
    # $pswindow.buffersize = $newsize    # assign the values to the actual console window
    # $newsize = $pswindow.windowsize    # set the window size
    # $newsize.height = [Math]::Min(50, $host.ui.rawui.MaxWindowSize.height)
    # $newsize.width = [Math]::Min(150, $host.ui.rawui.MaxWindowSize.Width)
    # $pswindow.windowsize = $newsize
    $pswindow.windowtitle = "Script by: rpo/abbodi1406/BAU/freddie-o [forums.mydigitallife.net]"
    [console]::ForegroundColor = "yellow"
    [console]::BackgroundColor = "blue"
    #
    Add-Type -AssemblyName System.Windows.Forms # Load the class System.Windows.Forms
    #
    Function Get-FileName
    {
    # Filebrowser dialog
    $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
       Multiselect = $false # One file can be chosen
       title="Selected ISO image"
    #   ShowHelp = $True
        Filter = 'ISO images (*.iso)|*.iso' # Select only iso files
    }
    IF ($FileBrowser.ShowDialog() -eq "Cancel"){Cancel}
    $FileBrowser.filename    # return the file name
    }
    $wshell=New-Object -ComObject Wscript.Shell
    #
    # .Net methods for hiding/showing the console in the background
    Add-Type -Name Window -Namespace Console -MemberDefinition '
    [DllImport("Kernel32.dll")]
    public static extern IntPtr GetConsoleWindow();
    
    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
    '
    # Hide = 0,
    # ShowNormal = 1,
    # ShowMinimized = 2,
    # ShowMaximized = 3,
    # Maximize = 3,
    # ShowNormalNoActivate = 4,
    # Show = 5,
    # Minimize = 6,
    # ShowMinNoActivate = 7,
    # ShowNoActivate = 8,
    # Restore = 9,
    # ShowDefault = 10,
    # ForceMinimized = 11
    #
    function Show-Console
    {
        $consolePtr = [Console.Window]::GetConsoleWindow()
        [Console.Window]::ShowWindow($consolePtr, 1)
    }
    function Hide-Console
    {
        $consolePtr = [Console.Window]::GetConsoleWindow()
        [Console.Window]::ShowWindow($consolePtr, 0)
    }
    Function Cancel
    {
        $wshell.Popup("The script was cancelled.",2,"Cancel script",0+64) | Out-Null
        [Environment]::Exit(1)
    }
    clear-host
    #
    $Global:ImagePath=""
    $USB=$Null
    $Form=New-Object System.Windows.Forms.Form # Create the screen form (window)
    $Form.TopMost = $True
    # Set the size of the window:
    # $Form.FormBorderStyle = "none"
    $Form.ControlBox = $True
    $Form.Text="Windows Installation Disk v1.5"
    $Form.Width=405 ; $Form.Height=240
    $Form.StartPosition = "CenterScreen"
    $Form.AutoSize=$true # AutoSize property to make the form automatically stretch, if the elements on the form are out of bounds
    $Form.KeyPreview = $True
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Enter"){$OKButton.PerformClick()}})         # Enter = click OK button
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Escape"){$CancelButton.PerformClick()}})    # Escape = click on Cancel button
    
    $OKButton=New-Object System.Windows.Forms.Button # Put the OK button on the form
    $OKButton.Location=New-Object System.Drawing.Size(280,155)
    $OKButton.Text="OK"
    $OKButton.Font='Segoe UI,9'
    $OKButton.Size=New-Object System.Drawing.Size(120,20)
    $OKButton.width=90
    $OKButton.height=30
    $Form.Controls.Add($OKButton)
    $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    
    $SelectUSBDisk=New-Object System.Windows.Forms.Label # Put the SelectUSBDisk label on the form
    $SelectUSBDisk.Location=New-Object System.Drawing.Size(20,20)
    $SelectUSBDisk.Font='Segoe UI,9'
    $SelectUSBDisk.width=350
    $SelectUSBDisk.height=20
    $Form.Controls.Add($SelectUSBDisk)
    
    $SelectISO=New-Object System.Windows.Forms.Label # Put the SelectISO label on the form
    $SelectISO.Location=New-Object System.Drawing.Size(20,85)
    $SelectISO.Font='Segoe UI,9'
    $SelectISO.width=350
    $SelectISO.height=20
    $SelectISO.text="Selected ISO image:"
    
    $SelectISOButton=New-Object System.Windows.Forms.Button # Put the SelectISO button on the form
    $SelectISOButton.Location=New-Object System.Drawing.Size(180,155)
    $SelectISOButton.Text="Select ISO"
    $SelectISOButton.Font='Segoe UI,9'
    $SelectISOButton.Size=New-Object System.Drawing.Size(120,20)
    $SelectISOButton.width=90
    $SelectISOButton.height=30;
    
    $ISOFile=New-Object System.Windows.Forms.Label # Put the ISO file name on the form
    $ISOFile.Location=New-Object System.Drawing.Size(20,110)
    $ISOFile.Text=" "
    $ISOFile.Font='Courier New,9'
    $ISOFile.Size=New-Object System.Drawing.Size(20,20)
    $ISOFile.width=350
    $ISOFile.height=20;
    $ISOFile.Backcolor = [System.Drawing.Color]::White
    $ISOFile.ForeColor = [System.Drawing.Color]::Black
    $ISOFile.BorderStyle = [System.Windows.Forms.BorderStyle]::Fixed3D
    
    $Warning=New-Object System.Windows.Forms.Label # Put the Warning label on the form
    $Warning.Location=New-Object System.Drawing.Size(20,30)
    $Warning.Font='Segoe UI,9'
    $Warning.width=360
    $Warning.height=80
    $Warning.text="Warning! Your USB disk will be converted to MBR scheme, repartitioned and reformatted. All the data will be lost. `r`n`r`nAre you sure you want to continue?"
    
    #
    #    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(20,45)
    $USBDiskList.Width=350
    $USBDiskList.height=20
    $USBDiskList.Font='Courier New,9'
    $Form.Controls.Add($SelectUSBDiskList)
    $USBDisks=@() # array with USB disk number
    $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    Hide-Console # Hide console display
    
    While (!$Disks) {
        $SelectUSBDisk.Text="Please plug in your USB disk."
        if ($form.ShowDialog() -eq "Cancel"){cancel}
        $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    }
    
    $CancelButton=New-Object System.Windows.Forms.Button # Put the Cancel button on the form
    $CancelButton.Location=New-Object System.Drawing.Size(20,155)
    $CancelButton.Text="Cancel"
    $CancelButton.Font='Segoe UI,9'
    $CancelButton.Size=New-Object System.Drawing.Size(120,20)
    $CancelButton.width=90
    $CancelButton.height=30;
    $Form.Controls.Add($CancelButton)
    # $CancelButton.Add_Click({$Form.Close();cancel})
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    
    $Form.Controls.Add($SelectISO)
    $Form.Controls.Add($ISOFile)
    $Form.Controls.Remove($OKButton)
    $SelectISOButton.Add_Click(
        {$Global:ImagePath = Get-FileName # get the iso image path with file browser dialog
         $ImagePath= Split-Path -Path $Global:ImagePath -leaf # extract filename and extension (iso)
         $ISOFile.Text = $ImagePath.PadRight(100," ").substring(0,43)+"..."
         $OKButton.Text="Create disk"
         $Form.Controls.Add($OKButton)})
     
    Foreach ($USBDisk in $Disks) {
        $FriendlyName=($USBDisk.FriendlyName).PadRight(40," ").substring(0,37)
        $USBDisks+=$USBDisk.DiskNumber
        $USBDiskList.Items.Add(("{0,-37}{1:n2} GB" -f $FriendlyName,($USBDisk.Size/1GB)))|out-null
        $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,-2}({1,1}:){2,-3}[{3:n2} GB]" -f " ", ($Partition.DriveLetter), $Volume.FileSystemLabel.PadRight(30," ").substring(0,30), ($Partition.Size/1GB)))|out-null
                }
            }
        }
    }
    $form.Controls.Add($USBDiskList)
    $USBDiskList.SelectedIndex=0
    
    $SelectUSBDisk.Text="Selected USB disk:"
    #
    $Form.Controls.Add($SelectISOButton)
    #
    if ($form.ShowDialog() -eq "Cancel") {Cancel}
    #
    # At this point the mounted USB disk and the iso image file path are defined
    #
    $USB=$USBDisks[$USBDiskList.SelectedIndex]
    #
    # Mount the image file if not already mounted and get the drive letter
    #
    if (ImagePath -eq ""){Cancel} # this can't happen, but keep the test
    #  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 ":") {Cancel}
    #
    # Ready to create the USB disk. Ask for confirmation
    #
    $Form.Controls.Remove($USBDiskList)
    $Form.Controls.Remove($SelectUSBDisk)
    $Form.Controls.Remove($SelectISOButton)
    $Form.Controls.Remove($ISOFile)
    $Form.Controls.Remove($SelectISO)
    $OKButton.Text="Continue"
    $Form.Controls.Add($OKButton)
    $Form.Controls.Add($Warning)
    if ($form.ShowDialog() -eq "Cancel") {DisMount-DiskImage -ImagePath $ImagePath |out-null;Cancel}
    Clear-Host
    $pswindow.windowtitle = "Windows Installation Disk v1.5"
    Show-Console | Out-Null # Restore console display
    #    Clear the USB stick
    Clear-Disk $usb -RemoveData -RemoveOEM -Confirm:$false
    set-disk $usb -partitionstyle mbr
    Stop-Service ShellHWDetection  |out-null
    #    Create the fat32 boot partition
    $usbfat32=(New-Partition -DiskNumber $usb -Size  1GB -AssignDriveLetter | Format-Volume -FileSystem FAT32 -NewFileSystemLabel "BOOT").DriveLetter + ":"
    #    Create the ntfs intall partition
    $usbntfs=(New-Partition -DiskNumber $usb -UseMaximumSize -AssignDriveLetter -IsActive | Format-Volume -FileSystem NTFS -NewFileSystemLabel "INSTALL").DriveLetter + ":"
    Start-Service ShellHWDetection |out-null
    #
    robocopy $iso $usbntfs /e /xf $iso\boot\bcd $iso\efi\microsoft\boot\bcd
    robocopy $iso"\" $usbfat32"\" bootmgr bootmgr.efi
    robocopy $iso"\boot" $usbfat32"\boot" /e /xf $iso\boot\bcd
    robocopy $iso"\efi" $usbfat32"\efi" /e /xf $iso\efi\microsoft\boot\bcd
    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
    $wshell.Popup("The Windows Installation Disk is complete.",0,"Complete",0+64) | Out-Null
    #done - keep a comment on the last line so the previous useful crlf never gets eaten
    
    



    Blue Theme:

    Code:
    <# : standard way of doing hybrid batch + powershell scripts
    @title Script by: rpo/abbodi1406/BAU/freddie-o [forums.mydigitallife.net]
    @set "ScriptPath=%~f0" &set "__ARGS__=%*" &powershell -noprofile -c "iex(Get-Content('%~f0') -raw)" &exit/b
    #>
    #
    #    This script must be executed with admin privilege
    #
    #    Test Administrator privileges
    If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
    {
    #    Restart the script to get Administrator privileges and exit
         Start-Process "$Env:ScriptPath" -ArgumentList "$Env:__ARGS__" -Verb runAs; exit
    }
    #    We have Administrator privileges
    #
    $Env:ScriptPath=""; $Env:ScriptPath=""  # delete environment variables
    #
    $pswindow = $host.ui.rawui          # create a reference to the console’s UI.RawUI child object
    # $newsize = $pswindow.buffersize   # set the buffer size
    # $newsize.height = 3000            # 3000 lines
    # $newsize.width = 150                # 150 characters
    # $pswindow.buffersize = $newsize    # set the BufferSize properties
    # $newsize = $pswindow.buffersize   # assign the BufferSize properties to a variable named $newsize
    # $pswindow.buffersize = $newsize    # assign the values to the actual console window
    # $newsize = $pswindow.windowsize    # set the window size
    # $newsize.height = [Math]::Min(50, $host.ui.rawui.MaxWindowSize.height)
    # $newsize.width = [Math]::Min(150, $host.ui.rawui.MaxWindowSize.Width)
    # $pswindow.windowsize = $newsize
    $pswindow.windowtitle = "Script by: rpo/abbodi1406/BAU/freddie-o [forums.mydigitallife.net]"
    [console]::ForegroundColor = "yellow"
    [console]::BackgroundColor = "blue"
    #
    Add-Type -AssemblyName System.Windows.Forms # Load the class System.Windows.Forms
    #
    Function Get-FileName
    {
    # Filebrowser dialog
    $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
       Multiselect = $false # One file can be chosen
       title="Selected ISO image"
    #   ShowHelp = $True
        Filter = 'ISO images (*.iso)|*.iso' # Select only iso files
    }
    IF ($FileBrowser.ShowDialog() -eq "Cancel"){Cancel}
    $FileBrowser.filename    # return the file name
    }
    $wshell=New-Object -ComObject Wscript.Shell
    #
    # .Net methods for hiding/showing the console in the background
    Add-Type -Name Window -Namespace Console -MemberDefinition '
    [DllImport("Kernel32.dll")]
    public static extern IntPtr GetConsoleWindow();
    
    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
    '
    # Hide = 0,
    # ShowNormal = 1,
    # ShowMinimized = 2,
    # ShowMaximized = 3,
    # Maximize = 3,
    # ShowNormalNoActivate = 4,
    # Show = 5,
    # Minimize = 6,
    # ShowMinNoActivate = 7,
    # ShowNoActivate = 8,
    # Restore = 9,
    # ShowDefault = 10,
    # ForceMinimized = 11
    #
    function Show-Console
    {
        $consolePtr = [Console.Window]::GetConsoleWindow()
        [Console.Window]::ShowWindow($consolePtr, 1)
    }
    function Hide-Console
    {
        $consolePtr = [Console.Window]::GetConsoleWindow()
        [Console.Window]::ShowWindow($consolePtr, 0)
    }
    Function Cancel
    {
        $wshell.Popup("The script was cancelled.",2,"Cancel script",0+64) | Out-Null
        [Environment]::Exit(1)
    }
    clear-host
    #
    $Global:ImagePath=""
    $USB=$Null
    $Form=New-Object System.Windows.Forms.Form # Create the screen form (window)
    $Form.TopMost = $True
    # Set the size of the window:
    # $Form.FormBorderStyle = "none"
    $Form.ControlBox = $True                 
    $Form.Text="Windows Installation Disk v1.5"
    $Form.Width=405 ; $Form.Height=240
    $Form.StartPosition = "CenterScreen"
    $Form.AutoSize=$true # AutoSize property to make the form automatically stretch, if the elements on the form are out of bounds
    $Form.KeyPreview = $True
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Enter"){$OKButton.PerformClick()}})         # Enter = click OK button
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Escape"){$CancelButton.PerformClick()}})    # Escape = click on Cancel button
    $Form.BackColor = "#3a96dd"
    
    $OKButton=New-Object System.Windows.Forms.Button # Put the OK button on the form
    $OKButton.Location=New-Object System.Drawing.Size(270,155)
    $OKButton.Text="OK"
    $OKButton.Font='Consolas,10'
    $OKButton.Size=New-Object System.Drawing.Size(120,20)
    $OKButton.width=100
    $OKButton.height=30
    $OKButton.ForeColor = [System.Drawing.Color]::White                                               
    $Form.Controls.Add($OKButton)
    $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    
    $SelectUSBDisk=New-Object System.Windows.Forms.Label # Put the SelectUSBDisk label on the form
    $SelectUSBDisk.Location=New-Object System.Drawing.Size(20,20)
    $SelectUSBDisk.Font='Consolas,10'
    $SelectUSBDisk.width=350
    $SelectUSBDisk.height=20
    $SelectUSBDisk.ForeColor = [System.Drawing.Color]::White                                                 
    $Form.Controls.Add($SelectUSBDisk)
    
    $SelectISO=New-Object System.Windows.Forms.Label # Put the SelectISO label on the form
    $SelectISO.Location=New-Object System.Drawing.Size(20,85)
    $SelectISO.Font='Consolas,10'
    $SelectISO.width=350
    $SelectISO.height=20
    $SelectISO.text="Selected ISO image:"
    $SelectISO.ForeColor = [System.Drawing.Color]::White                                             
    
    $SelectISOButton=New-Object System.Windows.Forms.Button # Put the SelectISO button on the form
    $SelectISOButton.Location=New-Object System.Drawing.Size(160,155)
    $SelectISOButton.Text="Select ISO"
    $SelectISOButton.Font='Consolas,10'
    $SelectISOButton.Size=New-Object System.Drawing.Size(120,20)
    $SelectISOButton.width=100
    $SelectISOButton.height=30;
    $SelectISOButton.ForeColor = [System.Drawing.Color]::White                                                       
                                                                                                                                                                                       
    $ISOFile=New-Object System.Windows.Forms.Label # Put the ISO file name on the form
    $ISOFile.Location=New-Object System.Drawing.Size(20,110)
    $ISOFile.Text=" "
    $ISOFile.Font='Courier New,9'
    $ISOFile.Size=New-Object System.Drawing.Size(20,20)
    $ISOFile.width=350
    $ISOFile.height=20;
    $ISOFile.Backcolor = [System.Drawing.Color]::White
    $ISOFile.ForeColor = [System.Drawing.Color]::Black
    $ISOFile.BorderStyle = [System.Windows.Forms.BorderStyle]::Fixed3D
    
    $Warning=New-Object System.Windows.Forms.Label # Put the Warning label on the form
    $Warning.Location=New-Object System.Drawing.Size(20,30)
    $Warning.Font='Consolas,10'
    $Warning.width=330
    $Warning.height=80
    $Warning.text="Warning! Your USB disk will be converted to MBR scheme, repartitioned and reformatted. All the data will be lost. `r`n`r`nAre you sure you want to continue?"
    $Warning.ForeColor = [System.Drawing.Color]::White                                           
    #
    #    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(20,45)
    $USBDiskList.Width=350
    $USBDiskList.height=20
    $USBDiskList.Font='Courier New,9'
    $Form.Controls.Add($SelectUSBDiskList)
    $USBDisks=@() # array with USB disk number
    $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    Hide-Console # Hide console display
    
    While (!$Disks) {
        $SelectUSBDisk.Text="Please plug in your USB disk."
        if ($form.ShowDialog() -eq "Cancel"){cancel}
        $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    }
    
    $CancelButton=New-Object System.Windows.Forms.Button # Put the Cancel button on the form
    $CancelButton.Location=New-Object System.Drawing.Size(20,155)
    $CancelButton.Text="Cancel"
    $CancelButton.Font='Consolas,10'
    $CancelButton.Size=New-Object System.Drawing.Size(120,20)
    $CancelButton.width=100
    $CancelButton.height=30;
    $CancelButton.ForeColor = [System.Drawing.Color]::White
    $Form.Controls.Add($CancelButton)
    # $CancelButton.Add_Click({$Form.Close();cancel})
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    
    $Form.Controls.Add($SelectISO)
    $Form.Controls.Add($ISOFile)
    $Form.Controls.Remove($OKButton)
    $SelectISOButton.Add_Click(
        {$Global:ImagePath = Get-FileName # get the iso image path with file browser dialog
         $ImagePath= Split-Path -Path $Global:ImagePath -leaf # extract filename and extension (iso)
         $ISOFile.Text = $ImagePath.PadRight(100," ").substring(0,43)+"..."
         $OKButton.Text="Create disk"
         $Form.Controls.Add($OKButton)})
     
    Foreach ($USBDisk in $Disks) {
        $FriendlyName=($USBDisk.FriendlyName).PadRight(40," ").substring(0,37)
        $USBDisks+=$USBDisk.DiskNumber
        $USBDiskList.Items.Add(("{0,-37}{1:n2} GB" -f $FriendlyName,($USBDisk.Size/1GB)))|out-null
        $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,-2}({1,1}:){2,-3}[{3:n2} GB]" -f " ", ($Partition.DriveLetter), $Volume.FileSystemLabel.PadRight(30," ").substring(0,30), ($Partition.Size/1GB)))|out-null
                }
            }
        }
    }
    $form.Controls.Add($USBDiskList)
    $USBDiskList.SelectedIndex=0
    
    $SelectUSBDisk.Text="Selected USB disk:"
    #
    $Form.Controls.Add($SelectISOButton)
    #
    if ($form.ShowDialog() -eq "Cancel") {Cancel}
    #
    # At this point the mounted USB disk and the iso image file path are defined
    #
    $USB=$USBDisks[$USBDiskList.SelectedIndex]
    #
    # Mount the image file if not already mounted and get the drive letter
    #
    if (ImagePath -eq ""){Cancel} # this can't happen, but keep the test
    #  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 ":") {Cancel}
    #
    # Ready to create the USB disk. Ask for confirmation
    #
    $Form.Controls.Remove($USBDiskList)
    $Form.Controls.Remove($SelectUSBDisk)
    $Form.Controls.Remove($SelectISOButton)
    $Form.Controls.Remove($ISOFile)
    $Form.Controls.Remove($SelectISO)
    $OKButton.Text="Continue"
    $Form.Controls.Add($OKButton)
    $Form.Controls.Add($Warning)
    if ($form.ShowDialog() -eq "Cancel") {DisMount-DiskImage -ImagePath $ImagePath |out-null;Cancel}
    Clear-Host
    $pswindow.windowtitle = "Windows Installation Disk v1.5"
    Show-Console | Out-Null # Restore console display
    #    Clear the USB stick
    Clear-Disk $usb -RemoveData -RemoveOEM -Confirm:$false
    set-disk $usb -partitionstyle mbr
    Stop-Service ShellHWDetection  |out-null
    #    Create the fat32 boot partition
    $usbfat32=(New-Partition -DiskNumber $usb -Size  1GB -AssignDriveLetter | Format-Volume -FileSystem FAT32 -NewFileSystemLabel "BOOT").DriveLetter + ":"
    #    Create the ntfs intall partition
    $usbntfs=(New-Partition -DiskNumber $usb -UseMaximumSize -AssignDriveLetter -IsActive | Format-Volume -FileSystem NTFS -NewFileSystemLabel "INSTALL").DriveLetter + ":"
    Start-Service ShellHWDetection |out-null
    #
    robocopy $iso $usbntfs /e /xf $iso\boot\bcd $iso\efi\microsoft\boot\bcd
    robocopy $iso"\" $usbfat32"\" bootmgr bootmgr.efi
    robocopy $iso"\boot" $usbfat32"\boot" /e /xf $iso\boot\bcd
    robocopy $iso"\efi" $usbfat32"\efi" /e /xf $iso\efi\microsoft\boot\bcd
    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
    $wshell.Popup("The Windows Installation Disk is complete.",0,"Complete",0+64) | Out-Null
    #done - keep a comment on the last line so the previous useful crlf never gets eaten
    
    



    Modern Black Theme:

    Code:
    <# : standard way of doing hybrid batch + powershell scripts
    @title Script by: rpo/abbodi1406/BAU/freddie-o [forums.mydigitallife.net]
    @set "ScriptPath=%~f0" &set "__ARGS__=%*" &powershell -noprofile -c "iex(Get-Content('%~f0') -raw)" &exit/b
    #>
    #
    #    This script must be executed with admin privilege
    #
    #    Test Administrator privileges
    If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
    {
    #    Restart the script to get Administrator privileges and exit
         Start-Process "$Env:ScriptPath" -ArgumentList "$Env:__ARGS__" -Verb runAs; exit
    }
    #    We have Administrator privileges
    #
    $Env:ScriptPath=""; $Env:ScriptPath=""  # delete environment variables
    #
    $pswindow = $host.ui.rawui          # create a reference to the console’s UI.RawUI child object
    # $newsize = $pswindow.buffersize   # set the buffer size
    # $newsize.height = 3000            # 3000 lines
    # $newsize.width = 150                # 150 characters
    # $pswindow.buffersize = $newsize    # set the BufferSize properties
    # $newsize = $pswindow.buffersize   # assign the BufferSize properties to a variable named $newsize
    # $pswindow.buffersize = $newsize    # assign the values to the actual console window
    # $newsize = $pswindow.windowsize    # set the window size
    # $newsize.height = [Math]::Min(50, $host.ui.rawui.MaxWindowSize.height)
    # $newsize.width = [Math]::Min(150, $host.ui.rawui.MaxWindowSize.Width)
    # $pswindow.windowsize = $newsize
    $pswindow.windowtitle = "Script by: rpo/abbodi1406/BAU/freddie-o [forums.mydigitallife.net]"
    [console]::ForegroundColor = "yellow"
    [console]::BackgroundColor = "blue"
    #
    Add-Type -AssemblyName System.Windows.Forms # Load the class System.Windows.Forms
    #
    Function Get-FileName
    {
    # Filebrowser dialog
    $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
       Multiselect = $false # One file can be chosen
       title="Selected ISO image"
    #   ShowHelp = $True
        Filter = 'ISO images (*.iso)|*.iso' # Select only iso files
    }
    IF ($FileBrowser.ShowDialog() -eq "Cancel"){Cancel}
    $FileBrowser.filename    # return the file name
    }
    $wshell=New-Object -ComObject Wscript.Shell
    #
    # .Net methods for hiding/showing the console in the background
    Add-Type -Name Window -Namespace Console -MemberDefinition '
    [DllImport("Kernel32.dll")]
    public static extern IntPtr GetConsoleWindow();
    
    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
    '
    # Hide = 0,
    # ShowNormal = 1,
    # ShowMinimized = 2,
    # ShowMaximized = 3,
    # Maximize = 3,
    # ShowNormalNoActivate = 4,
    # Show = 5,
    # Minimize = 6,
    # ShowMinNoActivate = 7,
    # ShowNoActivate = 8,
    # Restore = 9,
    # ShowDefault = 10,
    # ForceMinimized = 11
    #
    function Show-Console
    {
        $consolePtr = [Console.Window]::GetConsoleWindow()
        [Console.Window]::ShowWindow($consolePtr, 1)
    }
    function Hide-Console
    {
        $consolePtr = [Console.Window]::GetConsoleWindow()
        [Console.Window]::ShowWindow($consolePtr, 0)
    }
    Function Cancel
    {
        $wshell.Popup("The script was cancelled.",2,"Cancel script",0+64) | Out-Null
        [Environment]::Exit(1)
    }
    clear-host
    #
    $Global:ImagePath=""
    $USB=$Null
    $Form=New-Object System.Windows.Forms.Form # Create the screen form (window)
    $Form.TopMost = $True
    # Set the size of the window:
    # $Form.FormBorderStyle = "none"
    $Form.ControlBox = $True
    $Form.Text="Windows Installation Disk v1.5"
    $Form.Width=405 ; $Form.Height=240
    $Form.StartPosition = "CenterScreen"
    $Form.AutoSize=$true # AutoSize property to make the form automatically stretch, if the elements on the form are out of bounds
    $Form.KeyPreview = $True
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Enter"){$OKButton.PerformClick()}})         # Enter = click OK button
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Escape"){$CancelButton.PerformClick()}})    # Escape = click on Cancel button
    $Form.BackColor = "Black"
    
    $OKButton=New-Object System.Windows.Forms.Button # Put the OK button on the form
    $OKButton.Location=New-Object System.Drawing.Size(280,155)
    $OKButton.Text="OK"
    $OKButton.Font='Segoe UI,9'
    $OKButton.Size=New-Object System.Drawing.Size(120,20)
    $OKButton.width=90
    $OKButton.height=30
    $OKButton.ForeColor = [System.Drawing.Color]::White
    $OKButton.Flatstyle = [System.Windows.Forms.Flatstyle]::Flat
                                                
    $Form.Controls.Add($OKButton)
    $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    
    $SelectUSBDisk=New-Object System.Windows.Forms.Label # Put the SelectUSBDisk label on the form
    $SelectUSBDisk.Location=New-Object System.Drawing.Size(20,20)
    $SelectUSBDisk.Font='Segoe UI,9'
    $SelectUSBDisk.width=350
    $SelectUSBDisk.height=20
    $SelectUSBDisk.ForeColor = [System.Drawing.Color]::White                                                      
    $Form.Controls.Add($SelectUSBDisk)
    
    $SelectISO=New-Object System.Windows.Forms.Label # Put the SelectISO label on the form
    $SelectISO.Location=New-Object System.Drawing.Size(20,85)
    $SelectISO.Font='Segoe UI,9'
    $SelectISO.width=350
    $SelectISO.height=20
    $SelectISO.text="Selected ISO image:"
    $SelectISO.ForeColor = [System.Drawing.Color]::White                                                  
    
    $SelectISOButton=New-Object System.Windows.Forms.Button # Put the SelectISO button on the form
    $SelectISOButton.Location=New-Object System.Drawing.Size(180,155)
    $SelectISOButton.Text="Select ISO"
    $SelectISOButton.Font='Segoe UI,9'
    $SelectISOButton.Size=New-Object System.Drawing.Size(120,20)
    $SelectISOButton.width=90
    $SelectISOButton.height=30;
    $SelectISOButton.ForeColor = [System.Drawing.Color]::White                                                            
    $SelectISOButton.Flatstyle = [System.Windows.Forms.Flatstyle]::Flat
    
                                                                                                                                                                                            
    $ISOFile=New-Object System.Windows.Forms.Label # Put the ISO file name on the form
    $ISOFile.Location=New-Object System.Drawing.Size(20,110)
    $ISOFile.Text=" "
    $ISOFile.Font='Courier New,9'
    $ISOFile.Size=New-Object System.Drawing.Size(20,20)
    $ISOFile.width=350
    $ISOFile.height=20;
    $ISOFile.Backcolor = [System.Drawing.Color]::White
    $ISOFile.ForeColor = [System.Drawing.Color]::Black
    $ISOFile.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle
    
    $Warning=New-Object System.Windows.Forms.Label # Put the Warning label on the form
    $Warning.Location=New-Object System.Drawing.Size(20,30)
    $Warning.Font='Segoe UI,9'
    $Warning.width=360
    $Warning.height=80
    $Warning.text="Warning! Your USB disk will be converted to MBR scheme, repartitioned and reformatted. All the data will be lost. `r`n`r`nAre you sure you want to continue?"
    $Warning.ForeColor = [System.Drawing.Color]::White                                                
    #
    #    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.Flatstyle = [System.Windows.Forms.Flatstyle]::Flat
    $USBDiskList.Location=New-Object System.Drawing.Point(20,45)
    $USBDiskList.Width=350
    $USBDiskList.height=20
    $USBDiskList.Font='Courier New,9'
    $Form.Controls.Add($SelectUSBDiskList)
    $USBDisks=@() # array with USB disk number
    $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    Hide-Console # Hide console display
    
    While (!$Disks) {
        $SelectUSBDisk.Text="Please plug in your USB disk."
        if ($form.ShowDialog() -eq "Cancel"){cancel}
        $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    }
    
    $CancelButton=New-Object System.Windows.Forms.Button # Put the Cancel button on the form
    $CancelButton.Location=New-Object System.Drawing.Size(20,155)
    $CancelButton.Text="Cancel"
    $CancelButton.Font='Segoe UI,9'
    $CancelButton.Size=New-Object System.Drawing.Size(120,20)
    $CancelButton.width=90
    $CancelButton.height=30;
    $CancelButton.ForeColor = [System.Drawing.Color]::White
    $CancelButton.Flatstyle = [System.Windows.Forms.Flatstyle]::Flat
    $Form.Controls.Add($CancelButton)
    # $CancelButton.Add_Click({$Form.Close();cancel})
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    
    $Form.Controls.Add($SelectISO)
    $Form.Controls.Add($ISOFile)
    $Form.Controls.Remove($OKButton)
    $SelectISOButton.Add_Click(
        {$Global:ImagePath = Get-FileName # get the iso image path with file browser dialog
         $ImagePath= Split-Path -Path $Global:ImagePath -leaf # extract filename and extension (iso)
         $ISOFile.Text = $ImagePath.PadRight(100," ").substring(0,43)+"..."
         $OKButton.Text="Create disk"
         $Form.Controls.Add($OKButton)})
      
    Foreach ($USBDisk in $Disks) {
        $FriendlyName=($USBDisk.FriendlyName).PadRight(40," ").substring(0,37)
        $USBDisks+=$USBDisk.DiskNumber
        $USBDiskList.Items.Add(("{0,-37}{1:n2} GB" -f $FriendlyName,($USBDisk.Size/1GB)))|out-null
        $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,-2}({1,1}:){2,-3}[{3:n2} GB]" -f " ", ($Partition.DriveLetter), $Volume.FileSystemLabel.PadRight(30," ").substring(0,30), ($Partition.Size/1GB)))|out-null
                }
            }
        }
    }
    $form.Controls.Add($USBDiskList)
    $USBDiskList.SelectedIndex=0
    
    $SelectUSBDisk.Text="Selected USB disk:"
    #
    $Form.Controls.Add($SelectISOButton)
    #
    if ($form.ShowDialog() -eq "Cancel") {Cancel}
    #
    # At this point the mounted USB disk and the iso image file path are defined
    #
    $USB=$USBDisks[$USBDiskList.SelectedIndex]
    #
    # Mount the image file if not already mounted and get the drive letter
    #
    if (ImagePath -eq ""){Cancel} # this can't happen, but keep the test
    #  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 ":") {Cancel}
    #
    # Ready to create the USB disk. Ask for confirmation
    #
    $Form.Controls.Remove($USBDiskList)
    $Form.Controls.Remove($SelectUSBDisk)
    $Form.Controls.Remove($SelectISOButton)
    $Form.Controls.Remove($ISOFile)
    $Form.Controls.Remove($SelectISO)
    $OKButton.Text="Continue"
    $Form.Controls.Add($OKButton)
    $Form.Controls.Add($Warning)
    if ($form.ShowDialog() -eq "Cancel") {DisMount-DiskImage -ImagePath $ImagePath |out-null;Cancel}
    Clear-Host
    $pswindow.windowtitle = "Windows Installation Disk v1.5"
    Show-Console | Out-Null # Restore console display
    #    Clear the USB stick
    Clear-Disk $usb -RemoveData -RemoveOEM -Confirm:$false
    set-disk $usb -partitionstyle mbr
    Stop-Service ShellHWDetection  |out-null
    #    Create the fat32 boot partition
    $usbfat32=(New-Partition -DiskNumber $usb -Size  1GB -AssignDriveLetter | Format-Volume -FileSystem FAT32 -NewFileSystemLabel "BOOT").DriveLetter + ":"
    #    Create the ntfs intall partition
    $usbntfs=(New-Partition -DiskNumber $usb -UseMaximumSize -AssignDriveLetter -IsActive | Format-Volume -FileSystem NTFS -NewFileSystemLabel "INSTALL").DriveLetter + ":"
    Start-Service ShellHWDetection |out-null
    #
    robocopy $iso $usbntfs /e /xf $iso\boot\bcd $iso\efi\microsoft\boot\bcd
    robocopy $iso"\" $usbfat32"\" bootmgr bootmgr.efi
    robocopy $iso"\boot" $usbfat32"\boot" /e /xf $iso\boot\bcd
    robocopy $iso"\efi" $usbfat32"\efi" /e /xf $iso\efi\microsoft\boot\bcd
    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
    $wshell.Popup("The Windows Installation Disk is complete.",0,"Complete",0+64) | 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...
  8. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,606
    2,676
    60
    #168 freddie-o, Aug 26, 2019
    Last edited: Aug 26, 2019
    Updated the OP to Windows Installation Disk v1.5
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. drew84

    drew84 MDL Expert

    Mar 13, 2014
    1,407
    2,424
    60
    #169 drew84, Aug 26, 2019
    Last edited: Aug 26, 2019
    ... used your latest release (1.5) to create 18965 Boot USB
    Says operation completed sucessfully, but early on in the process.... this occured,
    Code:
    Start-Service : Service 'Shell Hardware Detection (ShellHWDetection)' cannot be started due to the following error: Cannot start service
    ShellHWDetection on computer '.'.
    At line:261 char:1
    + Start-Service ShellHWDetection |out-null
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service], ServiceCommandException
        + FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand
    
    ..unsure what script was trying to achieve at this point, or as to what impact the failure will have had on completed Boot USB

    ... do know that Windows Image Acquisition (WIA) service is dependent on above
     
  10. rpo

    rpo MDL Expert

    Jan 3, 2010
    1,504
    1,514
    60
    #170 rpo, Aug 26, 2019
    Last edited: Aug 27, 2019
    (OP)
    When an unformated disk is mounted and partitions created, windows sends a message inviting you to format the disk. For this reason a Stop-Service ShellHWDetection is executed. After the partitions are created and formated, the hardware detection is started again. There is not impact on the USB disk creation.
     
  11. rpo

    rpo MDL Expert

    Jan 3, 2010
    1,504
    1,514
    60
    Corrected some details :
    Code:
    <# : standard way of doing hybrid batch + powershell scripts
    @title Script by: rpo/abbodi1406/BAU/freddie-o [forums.mydigitallife.net]
    @set "ScriptPath=%~f0" &set "__ARGS__=%*" &powershell -noprofile -c "iex(Get-Content('%~f0') -raw)" &exit/b
    #>
    #
    #    This script must be executed with admin privilege
    #
    #    Test Administrator privileges
    If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
    {
    #    Restart the script to get Administrator privileges and exit
         Start-Process "$Env:ScriptPath" -ArgumentList "$Env:__ARGS__" -Verb runAs; exit
    }
    #    We have Administrator privileges
    #
    $Env:ScriptPath=""; $Env:ScriptPath=""  # delete environment variables
    #
    $pswindow = $host.ui.rawui          # create a reference to the console’s UI.RawUI child object
    # $newsize = $pswindow.buffersize   # set the buffer size
    # $newsize.height = 3000            # 3000 lines
    # $newsize.width = 150                # 150 characters
    # $pswindow.buffersize = $newsize    # set the BufferSize properties
    # $newsize = $pswindow.buffersize   # assign the BufferSize properties to a variable named $newsize
    # $pswindow.buffersize = $newsize    # assign the values to the actual console window
    # $newsize = $pswindow.windowsize    # set the window size
    # $newsize.height = [Math]::Min(50, $host.ui.rawui.MaxWindowSize.height)
    # $newsize.width = [Math]::Min(150, $host.ui.rawui.MaxWindowSize.Width)
    # $pswindow.windowsize = $newsize
    $pswindow.windowtitle = "Script by: rpo/abbodi1406/BAU/freddie-o [forums.mydigitallife.net]"
    [console]::ForegroundColor = "yellow"
    [console]::BackgroundColor = "blue"
    #
    Add-Type -AssemblyName System.Windows.Forms # Load the class System.Windows.Forms
    #
    Function Get-FileName
    {
    # Filebrowser dialog
    $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
       Multiselect = $false # One file can be chosen
       title="Selected ISO image"
    #   ShowHelp = $True
        Filter = 'ISO images (*.iso)|*.iso' # Select only iso files
    }
    IF ($FileBrowser.ShowDialog() -eq "Cancel"){Cancel}
    $FileBrowser.filename    # return the file name
    }
    $wshell=New-Object -ComObject Wscript.Shell
    #
    # .Net methods for hiding/showing the console in the background
    Add-Type -Name Window -Namespace Console -MemberDefinition '
    [DllImport("Kernel32.dll")]
    public static extern IntPtr GetConsoleWindow();
    
    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
    '
    # Hide = 0,
    # ShowNormal = 1,
    # ShowMinimized = 2,
    # ShowMaximized = 3,
    # Maximize = 3,
    # ShowNormalNoActivate = 4,
    # Show = 5,
    # Minimize = 6,
    # ShowMinNoActivate = 7,
    # ShowNoActivate = 8,
    # Restore = 9,
    # ShowDefault = 10,
    # ForceMinimized = 11
    #
    function Show-Console
    {
        $consolePtr = [Console.Window]::GetConsoleWindow()
        [Console.Window]::ShowWindow($consolePtr, 1)
    }
    function Hide-Console
    {
        $consolePtr = [Console.Window]::GetConsoleWindow()
        [Console.Window]::ShowWindow($consolePtr, 0)
    }
    Function Cancel
    {
        $wshell.Popup("The script was cancelled.",2,"Cancel script",0+64) | Out-Null
        [Environment]::Exit(1)
    }
    clear-host
    #
    $Global:ImagePath=""
    $USB=$Null
    $Form=New-Object System.Windows.Forms.Form # Create the screen form (window)
    $Form.TopMost = $True
    # Set the size of the window:
    # $Form.FormBorderStyle = "none"
    $Form.ControlBox = $True
    $Form.Text="Windows Installation Disk v1.5"
    $Form.Width=405 ; $Form.Height=240
    $Form.StartPosition = "CenterScreen"
    $Form.AutoSize=$true # AutoSize property to make the form automatically stretch, if the elements on the form are out of bounds
    $Form.KeyPreview = $True
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Enter"){$OKButton.PerformClick()}})         # Enter = click OK button
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Escape"){$CancelButton.PerformClick()}})    # Escape = click on Cancel button
    
    $OKButton=New-Object System.Windows.Forms.Button # Put the OK button on the form
    $OKButton.Location=New-Object System.Drawing.Point(280,155)
    $OKButton.Text="OK"
    $OKButton.Font='Segoe UI,9'
    $OKButton.Size=New-Object System.Drawing.Size(90,30)
    # $OKButton.Size=New-Object System.Drawing.Size(120,20)
    $Form.Controls.Add($OKButton)
    $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    
    $SelectUSBDisk=New-Object System.Windows.Forms.Label # Put the SelectUSBDisk label on the form
    $SelectUSBDisk.Location=New-Object System.Drawing.Point(20,20)
    $SelectUSBDisk.Font='Segoe UI,9'
    $SelectUSBDisk.Size=New-Object System.Drawing.Size(350,20)
    $Form.Controls.Add($SelectUSBDisk)
    
    $SelectISO=New-Object System.Windows.Forms.Label # Put the SelectISO label on the form
    $SelectISO.Location=New-Object System.Drawing.Point(20,85)
    $SelectISO.Font='Segoe UI,9' 
    $SelectISO.Size=New-Object System.Drawing.Size(350,20)
    $SelectISO.text="Selected ISO image:"
    
    $SelectISOButton=New-Object System.Windows.Forms.Button # Put the SelectISO button on the form
    $SelectISOButton.Location=New-Object System.Drawing.Point(180,155)
    $SelectISOButton.Text="Select ISO"
    $SelectISOButton.Font='Segoe UI,9' 
    $SelectISOButton.Size=New-Object System.Drawing.Size(90,30)
    # $SelectISOButton.Size=New-Object System.Drawing.Size(120,20)
    
    $ISOFile=New-Object System.Windows.Forms.Label # Put the ISO file name on the form
    $ISOFile.Location=New-Object System.Drawing.Point(20,110)
    $ISOFile.Text=" "
    $ISOFile.Font='Courier New,9'
    $ISOFile.Size=New-Object System.Drawing.Size(350,20)
    $ISOFile.Backcolor = [System.Drawing.Color]::White
    $ISOFile.ForeColor = [System.Drawing.Color]::Black
    $ISOFile.BorderStyle = [System.Windows.Forms.BorderStyle]::Fixed3D
    
    $Warning=New-Object System.Windows.Forms.Label # Put the Warning label on the form
    $Warning.Location=New-Object System.Drawing.Point(20,30)
    $Warning.Font='Segoe UI,9'
    $Warning.Size=New-Object System.Drawing.Size(360,80)
    $Warning.text="Warning! Your USB disk will be converted to MBR scheme, repartitioned and reformatted. All the data will be lost. `r`n`r`nAre you sure you want to continue?"
    
    #
    #    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(20,45)
    $USBDiskList.Size=New-Object System.Drawing.Size(350,20)
    $USBDiskList.Font='Courier New,9'
    $Form.Controls.Add($SelectUSBDiskList)
    $USBDisks=@() # array with USB disk number
    $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    Hide-Console # Hide console display
    
    While (!$Disks) {
        $SelectUSBDisk.Text="Please plug in your USB disk."
        if ($form.ShowDialog() -eq "Cancel"){cancel}
        $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    }
    
    $CancelButton=New-Object System.Windows.Forms.Button # Put the Cancel button on the form
    $CancelButton.Location=New-Object System.Drawing.Point(20,155)
    $CancelButton.Text="Cancel"
    $CancelButton.Font='Segoe UI,9'
    $CancelButton.Size=New-Object System.Drawing.Size(90,30)
    # $CancelButton.Size=New-Object System.Drawing.Size(120,20)
    $Form.Controls.Add($CancelButton)
    # $CancelButton.Add_Click({$Form.Close();cancel})
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    
    $Form.Controls.Add($SelectISO)
    $Form.Controls.Add($ISOFile)
    $Form.Controls.Remove($OKButton)
    $SelectISOButton.Add_Click(
        {$Global:ImagePath = Get-FileName # get the iso image path with file browser dialog
         $ImagePath= Split-Path -Path $Global:ImagePath -leaf # extract filename and extension (iso)
         $ISOFile.Text = $ImagePath
         if (($ISOFile.Text).length -gt 44) {$ISOFile.Text = $ImagePath.PadRight(100," ").substring(0,43)+"..."}
         $OKButton.Text="Create disk"
         $Form.Controls.Add($OKButton)})
     
    Foreach ($USBDisk in $Disks) {
        $FriendlyName=($USBDisk.FriendlyName).PadRight(40," ").substring(0,37)
        $USBDisks+=$USBDisk.DiskNumber
        $USBDiskList.Items.Add(("{0,-37}{1:n2} GB" -f $FriendlyName,($USBDisk.Size/1GB)))|out-null
        $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,-2}({1,1}:){2,-3}[{3:n2} GB]" -f " ", ($Partition.DriveLetter), $Volume.FileSystemLabel.PadRight(30," ").substring(0,30), ($Partition.Size/1GB)))|out-null
                }
            }
        }
    }
    $form.Controls.Add($USBDiskList)
    $USBDiskList.SelectedIndex=0
    
    $SelectUSBDisk.Text="Selected USB disk:"
    #
    $Form.Controls.Add($SelectISOButton)
    #
    if ($form.ShowDialog() -eq "Cancel") {Cancel}
    #
    # At this point the mounted USB disk and the iso image file path are defined
    #
    $USB=$USBDisks[$USBDiskList.SelectedIndex]
    #
    # Mount the image file if not already mounted and get the drive letter
    #
    if (ImagePath -eq ""){Cancel} # this can't happen, but keep the test
    #  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 ":") {Cancel}
    #
    # Ready to create the USB disk. Ask for confirmation
    #
    $Form.Controls.Remove($USBDiskList)
    $Form.Controls.Remove($SelectUSBDisk)
    $Form.Controls.Remove($SelectISOButton)
    $Form.Controls.Remove($ISOFile)
    $Form.Controls.Remove($SelectISO)
    $OKButton.Text="Continue"
    $Form.Controls.Add($OKButton)
    $Form.Controls.Add($Warning)
    if ($form.ShowDialog() -eq "Cancel") {DisMount-DiskImage -ImagePath $ImagePath |out-null;Cancel}
    Clear-Host
    $pswindow.windowtitle = "Windows Installation Disk v1.5"
    Show-Console | Out-Null # Restore console display
    #    Clear the USB stick
    Clear-Disk $usb -RemoveData -RemoveOEM -Confirm:$false
    set-disk $usb -partitionstyle mbr
    Stop-Service ShellHWDetection -erroraction silentlycontinue|out-null
    #    Create the fat32 boot partition
    $usbfat32=(New-Partition -DiskNumber $usb -Size  1GB -AssignDriveLetter | Format-Volume -FileSystem FAT32 -NewFileSystemLabel "BOOT").DriveLetter + ":"
    #    Create the ntfs intall partition
    $usbntfs=(New-Partition -DiskNumber $usb -UseMaximumSize -AssignDriveLetter -IsActive | Format-Volume -FileSystem NTFS -NewFileSystemLabel "INSTALL").DriveLetter + ":"
    Start-Service ShellHWDetection -erroraction silentlycontinue|out-null
    #
    robocopy $iso $usbntfs /e /xf $iso\boot\bcd $iso\efi\microsoft\boot\bcd
    robocopy $iso"\" $usbfat32"\" bootmgr bootmgr.efi
    robocopy $iso"\boot" $usbfat32"\boot" /e /xf $iso\boot\bcd
    robocopy $iso"\efi" $usbfat32"\efi" /e /xf $iso\efi\microsoft\boot\bcd
    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
    $wshell.Popup("The Windows Installation Disk is complete.",0,"Complete",0+64) | 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,606
    2,676
    60
    Removed pop-ups
    FlatStyle (Just cannot figure out how to add border to Combo box)

    Code:
    <# : standard way of doing hybrid batch + powershell scripts
    @title Script by: rpo/abbodi1406/BAU/freddie-o [forums.mydigitallife.net]
    @set "ScriptPath=%~f0" &set "__ARGS__=%*" &powershell -noprofile -c "iex(Get-Content('%~f0') -raw)" &exit/b
    #>
    #
    #    This script must be executed with admin privilege
    #
    #    Test Administrator privileges
    If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
    {
    #    Restart the script to get Administrator privileges and exit
         Start-Process "$Env:ScriptPath" -ArgumentList "$Env:__ARGS__" -Verb runAs; exit
    }
    #    We have Administrator privileges
    #
    $Env:ScriptPath=""; $Env:ScriptPath=""  # delete environment variables
    #
    $pswindow = $host.ui.rawui          # create a reference to the console’s UI.RawUI child object
    # $newsize = $pswindow.buffersize   # set the buffer size
    # $newsize.height = 3000            # 3000 lines
    # $newsize.width = 150                # 150 characters
    # $pswindow.buffersize = $newsize    # set the BufferSize properties
    # $newsize = $pswindow.buffersize   # assign the BufferSize properties to a variable named $newsize
    # $pswindow.buffersize = $newsize    # assign the values to the actual console window
    # $newsize = $pswindow.windowsize    # set the window size
    # $newsize.height = [Math]::Min(50, $host.ui.rawui.MaxWindowSize.height)
    # $newsize.width = [Math]::Min(150, $host.ui.rawui.MaxWindowSize.Width)
    # $pswindow.windowsize = $newsize
    $pswindow.windowtitle = "Script by: rpo/abbodi1406/BAU/freddie-o [forums.mydigitallife.net]"
    [console]::ForegroundColor = "yellow"
    [console]::BackgroundColor = "blue"
    #
    Add-Type -AssemblyName System.Windows.Forms # Load the class System.Windows.Forms
    #
    Function Get-FileName
    {
    # Filebrowser dialog
    $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
       Multiselect = $false # One file can be chosen
       title="Selected ISO image"
    #   ShowHelp = $True
        Filter = 'ISO images (*.iso)|*.iso' # Select only iso files
    }
    IF ($FileBrowser.ShowDialog() -eq "Cancel"){Cancel}
    $FileBrowser.filename    # return the file name
    }
    $wshell=New-Object -ComObject Wscript.Shell
    #
    # .Net methods for hiding/showing the console in the background
    Add-Type -Name Window -Namespace Console -MemberDefinition '
    [DllImport("Kernel32.dll")]
    public static extern IntPtr GetConsoleWindow();
    
    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
    '
    # Hide = 0,
    # ShowNormal = 1,
    # ShowMinimized = 2,
    # ShowMaximized = 3,
    # Maximize = 3,
    # ShowNormalNoActivate = 4,
    # Show = 5,
    # Minimize = 6,
    # ShowMinNoActivate = 7,
    # ShowNoActivate = 8,
    # Restore = 9,
    # ShowDefault = 10,
    # ForceMinimized = 11
    #
    function Show-Console
    {
        $consolePtr = [Console.Window]::GetConsoleWindow()
        [Console.Window]::ShowWindow($consolePtr, 1)
    }
    function Hide-Console
    {
        $consolePtr = [Console.Window]::GetConsoleWindow()
        [Console.Window]::ShowWindow($consolePtr, 0)
    }
    Function Cancel
    {
        [Environment]::Exit(1)
    }
    clear-host
    #
    $Global:ImagePath=""
    $USB=$Null
    $Form=New-Object System.Windows.Forms.Form # Create the screen form (window)
    $Form.TopMost = $True
    # Set the size of the window:
    # $Form.FormBorderStyle = "none"
    $Form.ControlBox = $True
    $Form.Text="Windows Installation Disk v1.5"
    $Form.Width=405 ; $Form.Height=240
    $Form.StartPosition = "CenterScreen"
    $Form.AutoSize=$true # AutoSize property to make the form automatically stretch, if the elements on the form are out of bounds
    $Form.KeyPreview = $True
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Enter"){$OKButton.PerformClick()}})         # Enter = click OK button
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Escape"){$CancelButton.PerformClick()}})    # Escape = click on Cancel button
    
    $OKButton=New-Object System.Windows.Forms.Button # Put the OK button on the form
    $OKButton.Location=New-Object System.Drawing.Point(280,160)
    $OKButton.Text="OK"
    $OKButton.Font='Segoe UI,9'
    $OKButton.Size=New-Object System.Drawing.Size(90,25)
    # $OKButton.Size=New-Object System.Drawing.Size(120,20)
    $OKButton.Flatstyle = [System.Windows.Forms.Flatstyle]::Flat                                                               
    $Form.Controls.Add($OKButton)
    $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    
    $SelectUSBDisk=New-Object System.Windows.Forms.Label # Put the SelectUSBDisk label on the form
    $SelectUSBDisk.Location=New-Object System.Drawing.Point(20,20)
    $SelectUSBDisk.Font='Segoe UI,9'
    $SelectUSBDisk.Size=New-Object System.Drawing.Size(350,20)
    $Form.Controls.Add($SelectUSBDisk)
    
    $SelectISO=New-Object System.Windows.Forms.Label # Put the SelectISO label on the form
    $SelectISO.Location=New-Object System.Drawing.Point(20,85)
    $SelectISO.Font='Segoe UI,9'
    $SelectISO.Size=New-Object System.Drawing.Size(350,20)
    $SelectISO.text="Selected ISO image:"
    
    $SelectISOButton=New-Object System.Windows.Forms.Button # Put the SelectISO button on the form
    $SelectISOButton.Location=New-Object System.Drawing.Point(180,160)
    $SelectISOButton.Text="Select ISO"
    $SelectISOButton.Font='Segoe UI,9'
    $SelectISOButton.Size=New-Object System.Drawing.Size(90,25)
    # $SelectISOButton.Size=New-Object System.Drawing.Size(120,20)
    $SelectISOButton.Flatstyle = [System.Windows.Forms.Flatstyle]::Flat                                                                       
    
    $ISOFile=New-Object System.Windows.Forms.Label # Put the ISO file name on the form
    $ISOFile.Location=New-Object System.Drawing.Point(20,110)
    $ISOFile.Text=" "
    $ISOFile.Font='Courier New,9'
    $ISOFile.Size=New-Object System.Drawing.Size(350,20)
    $ISOFile.Backcolor = [System.Drawing.Color]::White
    $ISOFile.ForeColor = [System.Drawing.Color]::Black
    $ISOFile.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle
    
    $Warning=New-Object System.Windows.Forms.Label # Put the Warning label on the form
    $Warning.Location=New-Object System.Drawing.Point(20,30)
    $Warning.Font='Segoe UI,9'
    $Warning.Size=New-Object System.Drawing.Size(360,80)
    $Warning.text="Warning! Your USB disk will be converted to MBR scheme, repartitioned and reformatted. All the data will be lost. `r`n`r`nAre you sure you want to continue?"
    
    #
    #    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.Flatstyle = [System.Windows.Forms.Flatstyle]::Flat
    $USBDiskList.Location=New-Object System.Drawing.Point(20,45)
    $USBDiskList.Size=New-Object System.Drawing.Size(350,20)
    $USBDiskList.Font='Courier New,9'
    $Form.Controls.Add($SelectUSBDiskList)
    $USBDisks=@() # array with USB disk number
    $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    Hide-Console # Hide console display
    
    $Complete=New-Object System.Windows.Forms.Label # Put the Complete label on the form
    $Complete.Location=New-Object System.Drawing.Point(20,30)
    $Complete.Font='Segoe UI,9'
    $Complete.Size=New-Object System.Drawing.Size(360,80)
    $Complete.text="The Windows Installation Disk completed successfully."
    #
    While (!$Disks) {
        $SelectUSBDisk.Text="Please plug in your USB disk."
        if ($form.ShowDialog() -eq "Cancel"){cancel}
        $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    }
    
    $CancelButton=New-Object System.Windows.Forms.Button # Put the Cancel button on the form
    $CancelButton.Location=New-Object System.Drawing.Point(20,160)
    $CancelButton.Text="Cancel"
    $CancelButton.Font='Segoe UI,9'
    $CancelButton.Size=New-Object System.Drawing.Size(90,25)
    # $CancelButton.Size=New-Object System.Drawing.Size(120,20)
    $CancelButton.Flatstyle = [System.Windows.Forms.Flatstyle]::Flat
    $Form.Controls.Add($CancelButton)
    # $CancelButton.Add_Click({$Form.Close();cancel})
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    
    $Form.Controls.Add($SelectISO)
    $Form.Controls.Add($ISOFile)
    $Form.Controls.Remove($OKButton)
    $SelectISOButton.Add_Click(
        {$Global:ImagePath = Get-FileName # get the iso image path with file browser dialog
         $ImagePath= Split-Path -Path $Global:ImagePath -leaf # extract filename and extension (iso)
         $ISOFile.Text = $ImagePath
         if (($ISOFile.Text).length -gt 44) {$ISOFile.Text = $ImagePath.PadRight(100," ").substring(0,43)+"..."}
         $OKButton.Text="Create disk"
         $Form.Controls.Add($OKButton)})
     
    Foreach ($USBDisk in $Disks) {
        $FriendlyName=($USBDisk.FriendlyName).PadRight(40," ").substring(0,37)
        $USBDisks+=$USBDisk.DiskNumber
        $USBDiskList.Items.Add(("{0,-37}{1:n2} GB" -f $FriendlyName,($USBDisk.Size/1GB)))|out-null
        $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,-2}({1,1}:){2,-3}[{3:n2} GB]" -f " ", ($Partition.DriveLetter), $Volume.FileSystemLabel.PadRight(30," ").substring(0,30), ($Partition.Size/1GB)))|out-null
                }
            }
        }
    }
    $form.Controls.Add($USBDiskList)
    $USBDiskList.SelectedIndex=0
    
    $SelectUSBDisk.Text="Selected USB disk:"
    #
    $Form.Controls.Add($SelectISOButton)
    #
    if ($form.ShowDialog() -eq "Cancel") {Cancel}
    #
    # At this point the mounted USB disk and the iso image file path are defined
    #
    $USB=$USBDisks[$USBDiskList.SelectedIndex]
    #
    # Mount the image file if not already mounted and get the drive letter
    #
    if (ImagePath -eq ""){Cancel} # this can't happen, but keep the test
    #  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 ":") {Cancel}
    #
    # Ready to create the USB disk. Ask for confirmation
    #
    $Form.Controls.Remove($USBDiskList)
    $Form.Controls.Remove($SelectUSBDisk)
    $Form.Controls.Remove($SelectISOButton)
    $Form.Controls.Remove($ISOFile)
    $Form.Controls.Remove($SelectISO)
    $OKButton.Text="Continue"
    $Form.Controls.Add($OKButton)
    $Form.Controls.Add($Warning)
    if ($form.ShowDialog() -eq "Cancel") {DisMount-DiskImage -ImagePath $ImagePath |out-null;Cancel}
    Clear-Host
    $pswindow.windowtitle = "Windows Installation Disk v1.5"
    Show-Console | Out-Null # Restore console display
    #    Clear the USB stick
    Clear-Disk $usb -RemoveData -RemoveOEM -Confirm:$false
    set-disk $usb -partitionstyle mbr
    Stop-Service ShellHWDetection -erroraction silentlycontinue|out-null
    #    Create the fat32 boot partition
    $usbfat32=(New-Partition -DiskNumber $usb -Size  1GB -AssignDriveLetter | Format-Volume -FileSystem FAT32 -NewFileSystemLabel "BOOT").DriveLetter + ":"
    #    Create the ntfs intall partition
    $usbntfs=(New-Partition -DiskNumber $usb -UseMaximumSize -AssignDriveLetter -IsActive | Format-Volume -FileSystem NTFS -NewFileSystemLabel "INSTALL").DriveLetter + ":"
    Start-Service ShellHWDetection -erroraction silentlycontinue|out-null
    #
    robocopy $iso $usbntfs /e /xf $iso\boot\bcd $iso\efi\microsoft\boot\bcd
    robocopy $iso"\" $usbfat32"\" bootmgr bootmgr.efi
    robocopy $iso"\boot" $usbfat32"\boot" /e /xf $iso\boot\bcd
    robocopy $iso"\efi" $usbfat32"\efi" /e /xf $iso\efi\microsoft\boot\bcd
    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
    #
    $Form.Controls.Remove($USBDiskList)
    $Form.Controls.Remove($SelectUSBDisk)
    $Form.Controls.Remove($SelectISOButton)
    $Form.Controls.Remove($OKButton)
    $Form.Controls.Remove($ISOFile)
    $Form.Controls.Remove($SelectISO)
    $Form.Controls.Remove($Warning)
    $CancelButton.Text="Close"
    $Form.Controls.Add($CancelButton)
    $Form.Controls.Add($Complete)
    if ($form.ShowDialog() -eq "Cancel") {Cancel}
    Clear-Host
    $pswindow.windowtitle = "Windows Installation Disk v1.5"
    Show-Console | Out-Null # Restore console display
    #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...
  13. rpo

    rpo MDL Expert

    Jan 3, 2010
    1,504
    1,514
    60
    Some paste/copy problems?
    The end of the script looks strange.
     
  14. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,606
    2,676
    60
    #174 freddie-o, Aug 28, 2019
    Last edited: Aug 29, 2019
    Modified
    Code:
    <# : standard way of doing hybrid batch + powershell scripts
    @title Script by: rpo/abbodi1406/BAU/freddie-o [forums.mydigitallife.net]
    @set "ScriptPath=%~f0" &set "__ARGS__=%*" &powershell -noprofile -c "iex(Get-Content('%~f0') -raw)" &exit/b
    #>
    #
    #    This script must be executed with admin privilege
    #
    #    Test Administrator privileges
    If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
    {
    #    Restart the script to get Administrator privileges and exit
         Start-Process "$Env:ScriptPath" -ArgumentList "$ Env:__ARGS__" -Verb runAs; exit
    }
    #    We have Administrator privileges
    #
    $Env:ScriptPath=""; $Env:ScriptPath=""  # delete environment variables
    #
    $pswindow = $host.ui.rawui          # create a reference to the console’s UI.RawUI child object
    # $newsize = $pswindow.buffersize   # set the buffer size
    # $newsize.height = 3000            # 3000 lines
    # $newsize.width = 150                # 150 characters
    # $pswindow.buffersize = $newsize    # set the BufferSize properties
    # $newsize = $pswindow.buffersize   # assign the BufferSize properties to a variable named $newsize
    # $pswindow.buffersize = $newsize    # assign the values to the actual console window
    # $newsize = $pswindow.windowsize    # set the window size
    # $newsize.height = [Math]::Min(50, $host.ui.rawui.MaxWindowSize.height)
    # $newsize.width = [Math]::Min(150, $host.ui.rawui.MaxWindowSize.Width)
    # $pswindow.windowsize = $newsize
    $pswindow.windowtitle = "Script by: rpo/abbodi1406/BAU/freddie-o [forums.mydigitallife.net]"
    [console]::ForegroundColor = "yellow"
    [console]::BackgroundColor = "blue"
    #
    Add-Type -AssemblyName System.Windows.Forms # Load the class System.Windows.Forms
    #
    Function Get-FileName
    {
    # Filebrowser dialog
    $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
       Multiselect = $false # One file can be chosen
       title="Selected ISO image"
    #   ShowHelp = $True
        Filter = 'ISO images (*.iso)|*.iso' # Select only iso files
    }
    IF ($FileBrowser.ShowDialog() -eq "Cancel"){Cancel}
    $FileBrowser.filename    # return the file name
    }
    $wshell=New-Object -ComObject Wscript.Shell
    #
    # .Net methods for hiding/showing the console in the background
    Add-Type -Name Window -Namespace Console -MemberDefinition '
    [DllImport("Kernel32.dll")]
    public static extern IntPtr GetConsoleWindow();
    
    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
    '
    # Hide = 0,
    # ShowNormal = 1,
    # ShowMinimized = 2,
    # ShowMaximized = 3,
    # Maximize = 3,
    # ShowNormalNoActivate = 4,
    # Show = 5,
    # Minimize = 6,
    # ShowMinNoActivate = 7,
    # ShowNoActivate = 8,
    # Restore = 9,
    # ShowDefault = 10,
    # ForceMinimized = 11
    #
    function Show-Console
    {
        $consolePtr = [Console.Window]::GetConsoleWindow()
        [Console.Window]::ShowWindow($consolePtr, 1)
    }
    function Hide-Console
    {
        $consolePtr = [Console.Window]::GetConsoleWindow()
        [Console.Window]::ShowWindow($consolePtr, 0)
    }
    Function Cancel
    {
        [Environment]::Exit(1)
    }
    clear-host
    #
    $Global:ImagePath=""
    $USB=$Null
    $Form=New-Object System.Windows.Forms.Form # Create the screen form (window)
    $Form.TopMost = $True
    # Set the size of the window:
    # $Form.FormBorderStyle = "none"
    $Form.ControlBox = $True
    $Form.Text="Windows Installation Disk v1.5"
    $Form.Width=405 ; $Form.Height=250
    $Form.StartPosition = "CenterScreen"
    $Form.AutoSize=$true # AutoSize property to make the form automatically stretch, if the elements on the form are out of bounds
    $Form.KeyPreview = $True
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Enter"){$OKButton.PerformClick()}})         # Enter = click OK button
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Escape"){$CancelButton.PerformClick()}})    # Escape = click on Cancel button
    $Form.BackColor = "#3A96DD"
    
    $OKButton=New-Object System.Windows.Forms.Button # Put the OK button on the form
    $OKButton.Location=New-Object System.Drawing.Point(270,170)
    $OKButton.Text="OK"
    $OKButton.Font='Courier New,9'
    $OKButton.Size=New-Object System.Drawing.Size(100,25)
    # $OKButton.Size=New-Object System.Drawing.Size(120,20)
    $OKButton.ForeColor = [System.Drawing.Color]::White
    $OKButton.Flatstyle = [System.Windows.Forms.Flatstyle]::Flat                 
    $Form.Controls.Add($OKButton)
    $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    
    $SelectUSBDisk=New-Object System.Windows.Forms.Label # Put the SelectUSBDisk label on the form
    $SelectUSBDisk.Location=New-Object System.Drawing.Point(20,24)
    $SelectUSBDisk.Font='Courier New,9'
    $SelectUSBDisk.Size=New-Object System.Drawing.Size(350,20)
    $SelectUSBDisk.ForeColor = [System.Drawing.Color]::White                       
    $Form.Controls.Add($SelectUSBDisk)
    
    $SelectISO=New-Object System.Windows.Forms.Label # Put the SelectISO label on the form
    $SelectISO.Location=New-Object System.Drawing.Point(20,89)
    $SelectISO.Font='Courier New,9'
    $SelectISO.Size=New-Object System.Drawing.Size(350,20)
    $SelectISO.text="Selected ISO image:"
    $SelectISO.ForeColor = [System.Drawing.Color]::White                                                               
    
    $SelectISOButton=New-Object System.Windows.Forms.Button # Put the SelectISO button on the form
    $SelectISOButton.Location=New-Object System.Drawing.Point(145,170)
    $SelectISOButton.Text="Select ISO"
    $SelectISOButton.Font='Courier New,9'
    $SelectISOButton.Size=New-Object System.Drawing.Size(100,25)
    # $SelectISOButton.Size=New-Object System.Drawing.Size(120,20)
    $SelectISOButton.ForeColor = [System.Drawing.Color]::White                                                             
    $SelectISOButton.Flatstyle = [System.Windows.Forms.Flatstyle]::Flat                         
    
    $ISOFile=New-Object System.Windows.Forms.Label # Put the ISO file name on the form
    $ISOFile.Location=New-Object System.Drawing.Point(20,110)
    $ISOFile.Text=" "
    $ISOFile.Font='Courier New,9'
    $ISOFile.Size=New-Object System.Drawing.Size(350,20)
    $ISOFile.Backcolor = [System.Drawing.Color]::White
    
    $ISOFile.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle
    
    $Warning=New-Object System.Windows.Forms.Label # Put the Warning label on the form
    $Warning.Location=New-Object System.Drawing.Point(20,30)
    $Warning.Font='Courier New,9'
    $Warning.Size=New-Object System.Drawing.Size(340,80)
    $Warning.text="Warning! Your USB disk will be converted to MBR scheme, repartitioned and reformatted. All the data will be lost. `r`n`r`nAre you sure you want to continue?"
    $Warning.ForeColor = [System.Drawing.Color]::White
    #
    #    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.Flatstyle = [System.Windows.Forms.Flatstyle]::Flat
    $USBDiskList.Backcolor = [System.Drawing.Color]::White       
    $USBDiskList.ForeColor = [System.Drawing.Color]::Black                                                     
    $USBDiskList.Location=New-Object System.Drawing.Point(20,45)
    $USBDiskList.Size=New-Object System.Drawing.Size(350,20)
    $USBDiskList.Font='Courier New,9'
    $Form.Controls.Add($SelectUSBDiskList)
    $USBDisks=@() # array with USB disk number
    $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    Hide-Console # Hide console display
    
    $Complete=New-Object System.Windows.Forms.Label # Put the Complete label on the form
    $Complete.Location=New-Object System.Drawing.Point(20,30)
    $Complete.Font='Courier New,9'
    $Complete.Size=New-Object System.Drawing.Size(360,80)
    $Complete.text="The Windows Installation Disk completed successfully."
    $Complete.ForeColor = [System.Drawing.Color]::White
    #                                                                                   
    While (!$Disks) {
        $SelectUSBDisk.Text="Please plug in your USB disk."
        if ($form.ShowDialog() -eq "Cancel"){cancel}
        $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    }
    
    $CancelButton=New-Object System.Windows.Forms.Button # Put the Cancel button on the form
    $CancelButton.Location=New-Object System.Drawing.Point(20,170)
    $CancelButton.Text="Cancel"
    $CancelButton.Font='Courier New,9'
    $CancelButton.Size=New-Object System.Drawing.Size(100,25)
    # $CancelButton.Size=New-Object System.Drawing.Size(120,20)
    $CancelButton.ForeColor = [System.Drawing.Color]::White
    $CancelButton.Flatstyle = [System.Windows.Forms.Flatstyle]::Flat                       
    $Form.Controls.Add($CancelButton)
    # $CancelButton.Add_Click({$Form.Close();cancel})
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    
    $Form.Controls.Add($SelectISO)
    $Form.Controls.Add($ISOFile)
    $Form.Controls.Remove($OKButton)
    $SelectISOButton.Add_Click(
        {$Global:ImagePath = Get-FileName # get the iso image path with file browser dialog
         $ImagePath= Split-Path -Path $Global:ImagePath -leaf # extract filename and extension (iso)
         $ISOFile.Text = $ImagePath
         if (($ISOFile.Text).length -gt 44) {$ISOFile.Text = $ImagePath.PadRight(100," ").substring(0,43)+"..."}
         $OKButton.Text="Create disk"
         $Form.Controls.Add($OKButton)})
    Foreach ($USBDisk in $Disks) {
        $FriendlyName=($USBDisk.FriendlyName).PadRight(40," ").substring(0,37)
        $USBDisks+=$USBDisk.DiskNumber
        $USBDiskList.Items.Add(("{0,-37}{1:n2} GB" -f $FriendlyName,($USBDisk.Size/1GB)))|out-null
        $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,-2}({1,1}:){2,-3}[{3:n2} GB]" -f " ", ($Partition.DriveLetter), $Volume.FileSystemLabel.PadRight(30," ").substring(0,30), ($Partition.Size/1GB)))|out-null
                }
            }
        }
    }
    $form.Controls.Add($USBDiskList)
    $USBDiskList.SelectedIndex=0
    
    $SelectUSBDisk.Text="Selected USB disk:"
    #
    $Form.Controls.Add($SelectISOButton)
    #
    if ($form.ShowDialog() -eq "Cancel") {Cancel}
    #
    # At this point the mounted USB disk and the iso image file path are defined
    #
    $USB=$USBDisks[$USBDiskList.SelectedIndex]
    #
    # Mount the image file if not already mounted and get the drive letter
    #
    if (ImagePath -eq ""){Cancel} # this can't happen, but keep the test
    #  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 ":") {Cancel}
    #
    # Ready to create the USB disk. Ask for confirmation
    #
    $Form.Controls.Remove($USBDiskList)
    $Form.Controls.Remove($SelectUSBDisk)
    $Form.Controls.Remove($SelectISOButton)
    $Form.Controls.Remove($ISOFile)
    $Form.Controls.Remove($SelectISO)
    $OKButton.Text="Yes"
    $CancelButton.Text="No"
    $Form.Controls.Add($OKButton)
    $Form.Controls.Add($Warning)
    if ($form.ShowDialog() -eq "Cancel") {DisMount-DiskImage -ImagePath $ImagePath |out-null;Cancel}
    Clear-Host
    $pswindow.windowtitle = "Windows Installation Disk v1.5"
    Show-Console | Out-Null # Restore console display
    #    Clear the USB stick
    Clear-Disk $usb -RemoveData -RemoveOEM -Confirm:$false
    set-disk $usb -partitionstyle mbr
    Stop-Service ShellHWDetection -erroraction silentlycontinue|out-null
    #    Create the fat32 boot partition
    $usbfat32=(New-Partition -DiskNumber $usb -Size  1GB -AssignDriveLetter | Format-Volume -FileSystem FAT32 -NewFileSystemLabel "BOOT").DriveLetter + ":"
    #    Create the ntfs intall partition
    $usbntfs=(New-Partition -DiskNumber $usb -UseMaximumSize -AssignDriveLetter -IsActive | Format-Volume -FileSystem NTFS -NewFileSystemLabel "INSTALL").DriveLetter + ":"
    Start-Service ShellHWDetection -erroraction silentlycontinue|out-null
    #
    robocopy $iso $usbntfs /e /xf $iso\boot\bcd $iso\efi\microsoft\boot\bcd
    robocopy $iso"\" $usbfat32"\" bootmgr bootmgr.efi
    robocopy $iso"\boot" $usbfat32"\boot" /e /xf $iso\boot\bcd
    robocopy $iso"\efi" $usbfat32"\efi" /e /xf $iso\efi\microsoft\boot\bcd
    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
    #
    $Form.Controls.Remove($USBDiskList)
    $Form.Controls.Remove($SelectUSBDisk)
    $Form.Controls.Remove($SelectISOButton)
    $Form.Controls.Remove($OKButton)
    $Form.Controls.Remove($ISOFile)
    $Form.Controls.Remove($SelectISO)
    $Form.Controls.Remove($Warning)
    $CancelButton.Text="Close"
    $Form.Controls.Add($CancelButton)
    $Form.Controls.Add($Complete)
    if ($form.ShowDialog() -eq "Cancel") {Cancel}
    #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...
  15. rpo

    rpo MDL Expert

    Jan 3, 2010
    1,504
    1,514
    60
    The script aborts when not started as admin.
    Replace at line 12 : " $Env:__ARGS__" by " $Env:__ARGS__" .The space between $ and E is mandatory when the script is started without argument.
     
  16. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,606
    2,676
    60
    #176 freddie-o, Aug 29, 2019
    Last edited: Aug 29, 2019
    Updated the OP with Windows Installation Disk 1.6.
    Made a correction to the script and a modification in the UI.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  17. rpo

    rpo MDL Expert

    Jan 3, 2010
    1,504
    1,514
    60
    Sorry, my bad. The space should be inserted between " and $. But in fact it doesn't matter since we don't process the content of the argument field.
     
  18. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,606
    2,676
    60
    It seems. I tested it and it worked.
     
    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
    #179 freddie-o, Aug 29, 2019
    Last edited: Aug 29, 2019
    Edit:
    But then for the sake of "correctness", I changed the spacing.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  20. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,606
    2,676
    60
    #180 freddie-o, Aug 29, 2019
    Last edited: Aug 29, 2019
    Your UI (modified) seems better.
    Code:
    <# : standard way of doing hybrid batch + powershell scripts
    @title Script by: rpo/abbodi1406/BAU/freddie-o [forums.mydigitallife.net]
    @set "ScriptPath=%~f0" &set "__ARGS__=%*" &powershell -noprofile -c "iex(Get-Content('%~f0') -raw)" &exit/b
    #>
    #
    #    This script must be executed with admin privilege
    #
    #    Test Administrator privileges
    If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
    {
    #    Restart the script to get Administrator privileges and exit
         Start-Process "$Env:ScriptPath" -ArgumentList " $Env:__ARGS__" -Verb runAs; exit
    }
    #    We have Administrator privileges
    #
    $Env:ScriptPath=""; $Env:ScriptPath=""  # delete environment variables
    #
    $pswindow = $host.ui.rawui          # create a reference to the console’s UI.RawUI child object
    # $newsize = $pswindow.buffersize   # set the buffer size
    # $newsize.height = 3000            # 3000 lines
    # $newsize.width = 150                # 150 characters
    # $pswindow.buffersize = $newsize    # set the BufferSize properties
    # $newsize = $pswindow.buffersize   # assign the BufferSize properties to a variable named $newsize
    # $pswindow.buffersize = $newsize    # assign the values to the actual console window
    # $newsize = $pswindow.windowsize    # set the window size
    # $newsize.height = [Math]::Min(50, $host.ui.rawui.MaxWindowSize.height)
    # $newsize.width = [Math]::Min(150, $host.ui.rawui.MaxWindowSize.Width)
    # $pswindow.windowsize = $newsize
    $pswindow.windowtitle = "Script by: rpo/abbodi1406/BAU/freddie-o [forums.mydigitallife.net]"
    [console]::ForegroundColor = "yellow"
    [console]::BackgroundColor = "blue"
    #
    Add-Type -AssemblyName System.Windows.Forms # Load the class System.Windows.Forms
    #
    Function Get-FileName
    {
    # Filebrowser dialog
    $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
       Multiselect = $false # One file can be chosen
       title="Select ISO image"
    #   ShowHelp = $True
        Filter = 'ISO images (*.iso)|*.iso' # Select only iso files
    }
    IF ($FileBrowser.ShowDialog() -eq "Cancel"){Cancel}
    $FileBrowser.filename    # return the file name
    }
    $wshell=New-Object -ComObject Wscript.Shell
    #
    # .Net methods for hiding/showing the console in the background
    Add-Type -Name Window -Namespace Console -MemberDefinition '
    [DllImport("Kernel32.dll")]
    public static extern IntPtr GetConsoleWindow();
    
    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
    '
    # Hide = 0,
    # ShowNormal = 1,
    # ShowMinimized = 2,
    # ShowMaximized = 3,
    # Maximize = 3,
    # ShowNormalNoActivate = 4,
    # Show = 5,
    # Minimize = 6,
    # ShowMinNoActivate = 7,
    # ShowNoActivate = 8,
    # Restore = 9,
    # ShowDefault = 10,
    # ForceMinimized = 11
    #
    function Show-Console
    {
        $consolePtr = [Console.Window]::GetConsoleWindow()
        [Console.Window]::ShowWindow($consolePtr, 1)
    }
    function Hide-Console
    {
        $consolePtr = [Console.Window]::GetConsoleWindow()
        [Console.Window]::ShowWindow($consolePtr, 0)
    }
    Function Cancel
    {
        [Environment]::Exit(1)
    }
    clear-host
    #
    $Global:ImagePath=""
    $USB=$Null
    $Form=New-Object System.Windows.Forms.Form # Create the screen form (window)
    $Form.TopMost = $True
    # Set the size of the window:
    # $Form.FormBorderStyle = "none"
    $Form.ControlBox = $True
    $Form.Text="Windows Installation Disk v1.7"
    $Form.Width=445 ; $Form.Height=250
    $Form.StartPosition = "CenterScreen"
    $Form.AutoSize=$true # AutoSize property to make the form automatically stretch, if the elements on the form are out of bounds
    $Form.KeyPreview = $True
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Enter"){$OKButton.PerformClick()}})         # Enter = click OK button
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Escape"){$CancelButton.PerformClick()}})    # Escape = click on Cancel button
    
    $OKButton=New-Object System.Windows.Forms.Button # Put the OK button on the form
    $OKButton.Location=New-Object System.Drawing.Point(310,170)
    $OKButton.Text="OK"
    $OKButton.Font='Courier New,9'
    $OKButton.Size=New-Object System.Drawing.Size(100,25)
    # $OKButton.Size=New-Object System.Drawing.Size(120,20)
    $Form.Controls.Add($OKButton)
    $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    
    $SelectUSBDisk=New-Object System.Windows.Forms.Label # Put the SelectUSBDisk label on the form
    $SelectUSBDisk.Location=New-Object System.Drawing.Point(20,25)
    $SelectUSBDisk.Font='Courier New,9'
    $SelectUSBDisk.Size=New-Object System.Drawing.Size(350,20)
    $Form.Controls.Add($SelectUSBDisk)
    
    $SelectISO=New-Object System.Windows.Forms.Label # Put the SelectISO label on the form
    $SelectISO.Location=New-Object System.Drawing.Point(20,90)
    $SelectISO.Font='Courier New,9'
    $SelectISO.Size=New-Object System.Drawing.Size(350,20)
    $SelectISO.text="Select ISO image:"
    
    $SelectISOButton=New-Object System.Windows.Forms.Button # Put the SelectISO button on the form
    $SelectISOButton.Location=New-Object System.Drawing.Point(375,110)
    $SelectISOButton.Text="..."
    $SelectISOButton.Font='Courier New,9'
    $SelectISOButton.Size=New-Object System.Drawing.Size(35,20)
    # $SelectISOButton.Size=New-Object System.Drawing.Size(35,20)
    
    $ISOFile=New-Object System.Windows.Forms.Label # Put the ISO file name on the form
    $ISOFile.Location=New-Object System.Drawing.Point(20,110)
    $ISOFile.Text=" "
    $ISOFile.Font='Courier New,9'
    $ISOFile.Size=New-Object System.Drawing.Size(350,20)
    $ISOFile.Backcolor = [System.Drawing.Color]::White
    $ISOFile.ForeColor = [System.Drawing.Color]::Black
    $ISOFile.BorderStyle = [System.Windows.Forms.BorderStyle]::Fixed3D
    
    $Warning=New-Object System.Windows.Forms.Label # Put the Warning label on the form
    $Warning.Location=New-Object System.Drawing.Point(20,30)
    $Warning.Font='Courier New,9'
    $Warning.Size=New-Object System.Drawing.Size(380,80)
    $Warning.text="Warning! Your USB disk will be converted to MBR scheme, repartitioned and reformatted. All the data will be lost. `r`n`r`nAre you sure you want to continue?"
    
    #
    #    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(20,45)
    $USBDiskList.Size=New-Object System.Drawing.Size(390,25)
    $USBDiskList.Font='Courier New,9'
    $Form.Controls.Add($SelectUSBDiskList)
    $USBDisks=@() # array with USB disk number
    $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    Hide-Console # Hide console display
    
    $Complete=New-Object System.Windows.Forms.Label # Put the Complete label on the form
    $Complete.Location=New-Object System.Drawing.Point(20,30)
    $Complete.Font='Courier New,9'
    $Complete.Size=New-Object System.Drawing.Size(380,80)
    $Complete.text="The Windows Installation Disk completed successfully."
    #                                                                                   
    While (!$Disks) {
        $SelectUSBDisk.Text="Please plug in your USB disk."
        if ($form.ShowDialog() -eq "Cancel"){cancel}
        $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    }
    
    $CancelButton=New-Object System.Windows.Forms.Button # Put the Cancel button on the form
    $CancelButton.Location=New-Object System.Drawing.Point(20,170)
    $CancelButton.Text="Cancel"
    $CancelButton.Font='Courier New,9'
    $CancelButton.Size=New-Object System.Drawing.Size(100,25)
    # $CancelButton.Size=New-Object System.Drawing.Size(120,20)
    $Form.Controls.Add($CancelButton)
    # $CancelButton.Add_Click({$Form.Close();cancel})
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    
    $Form.Controls.Add($SelectISO)
    $Form.Controls.Add($ISOFile)
    $Form.Controls.Remove($OKButton)
    $SelectISOButton.Add_Click(
        {$Global:ImagePath = Get-FileName # get the iso image path with file browser dialog
         $ImagePath= Split-Path -Path $Global:ImagePath -leaf # extract filename and extension (iso)
         $ISOFile.Text = $ImagePath
         if (($ISOFile.Text).length -gt 44) {$ISOFile.Text = $ImagePath.PadRight(100," ").substring(0,43)+"..."}
         $OKButton.Text="Create disk"
         $Form.Controls.Add($OKButton)})
    Foreach ($USBDisk in $Disks) {
        $FriendlyName=($USBDisk.FriendlyName).PadRight(40," ").substring(0,40)
        $USBDisks+=$USBDisk.DiskNumber
        $USBDiskList.Items.Add(("{0,-43}{1:n2} GB" -f $FriendlyName,($USBDisk.Size/1GB)))|out-null
        $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,-2}({1,1}:){2,-35}[{3:n2} GB]" -f " ", ($Partition.DriveLetter), $Volume.FileSystemLabel.PadRight(30," ").substring(0,30), ($Partition.Size/1GB)))|out-null
                }
            }
        }
    }
    $form.Controls.Add($USBDiskList)
    $USBDiskList.SelectedIndex=0
    
    $SelectUSBDisk.Text="Select USB disk:"
    #
    $Form.Controls.Add($SelectISOButton)
    #
    if ($form.ShowDialog() -eq "Cancel") {Cancel}
    #
    # At this point the mounted USB disk and the iso image file path are defined
    #
    $USB=$USBDisks[$USBDiskList.SelectedIndex]
    #
    # Mount the image file if not already mounted and get the drive letter
    #
    if (ImagePath -eq ""){Cancel} # this can't happen, but keep the test
    #  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 ":") {Cancel}
    #
    # Ready to create the USB disk. Ask for confirmation
    #
    $Form.Controls.Remove($USBDiskList)
    $Form.Controls.Remove($SelectUSBDisk)
    $Form.Controls.Remove($SelectISOButton)
    $Form.Controls.Remove($ISOFile)
    $Form.Controls.Remove($SelectISO)
    $OKButton.Text="Yes"
    $CancelButton.Text="No"                     
    $Form.Controls.Add($OKButton)
    $Form.Controls.Add($Warning)
    if ($form.ShowDialog() -eq "Cancel") {DisMount-DiskImage -ImagePath $ImagePath |out-null;Cancel}
    Clear-Host
    $pswindow.windowtitle = "Windows Installation Disk v1.7"
    Show-Console | Out-Null # Restore console display
    #    Clear the USB stick
    Clear-Disk $usb -RemoveData -RemoveOEM -Confirm:$false
    set-disk $usb -partitionstyle mbr
    Stop-Service ShellHWDetection -erroraction silentlycontinue|out-null
    #    Create the fat32 boot partition
    $usbfat32=(New-Partition -DiskNumber $usb -Size  1GB -AssignDriveLetter | Format-Volume -FileSystem FAT32 -NewFileSystemLabel "BOOT").DriveLetter + ":"
    #    Create the ntfs intall partition
    $usbntfs=(New-Partition -DiskNumber $usb -UseMaximumSize -AssignDriveLetter -IsActive | Format-Volume -FileSystem NTFS -NewFileSystemLabel "INSTALL").DriveLetter + ":"
    Start-Service ShellHWDetection -erroraction silentlycontinue|out-null
    #
    robocopy $iso $usbntfs /e /xf $iso\boot\bcd $iso\efi\microsoft\boot\bcd
    robocopy $iso"\" $usbfat32"\" bootmgr bootmgr.efi
    robocopy $iso"\boot" $usbfat32"\boot" /e /xf $iso\boot\bcd
    robocopy $iso"\efi" $usbfat32"\efi" /e /xf $iso\efi\microsoft\boot\bcd
    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
    #
    $Form.Controls.Remove($USBDiskList)
    $Form.Controls.Remove($SelectUSBDisk)
    $Form.Controls.Remove($SelectISOButton)
    $Form.Controls.Remove($OKButton)
    $Form.Controls.Remove($ISOFile)
    $Form.Controls.Remove($SelectISO)
    $Form.Controls.Remove($Warning)
    $CancelButton.Text="Close"
    $Form.Controls.Add($CancelButton)
    $Form.Controls.Add($Complete)
    if ($form.ShowDialog() -eq "Cancel") {Cancel}
    #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...