GUI for Powershell script to create a Windows installation disk

0

I have this powershell script that creates a Windows installation disk.

-For both Legacy BIOS and the native UEFI boot process.

-For systems that use UEFI with Secure Boot

-For Windows ISOs that have Install.WIMs that are over 4GB (Due to the 4GB limitation of FAT32).

What the script does is:

-It creates 2 partitions on your USB disk. A FAT32 and an NTFS partition.

-Mounts the Windows ISO.

-Copies the entire contents of the ISO to the NTFS partition of the installation disk.

-Then copies all the boot files from the ISO to the FAT32 partition of the installation disk. (“boot” folder, “efi” folder, “bootmgr”, “bootmgr.efi” and “boot.wim”)

Script:

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

I'm trying to modify the script so as to have just a GUI to create the Windows installation disk. But I have a very limited knowledge of scripting, so this is as far as I got.

This is the script I have so far:

@echo off
(
echo #
echo #  This script must be executed with admin privilege
echo #
echo #  Test Administrator privileges
echo If ^(-NOT ^([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent^(^)^).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"^)^)
echo {
echo #  Restart the script to get Administrator privileges and exit
echo    Start-Process 'powershell.exe' -Verb runAs -ArgumentList ^("-NoLogo -WindowStyle Normal -noprofile -file " + """" + $PSCommandPath + """"^)
echo    exit
echo }
echo #  We have Administrator privileges
echo #
echo [console]::ForegroundColor = "Yellow"
echo [console]::BackgroundColor = "blue"
echo #
echo Add-Type -AssemblyName System.Windows.Forms # Load the class System.Windows.Forms 
echo # Filebrowser dialog
echo $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
echo    Multiselect = $false # One file can be chosen
echo    Filter = 'ISO images ^(*.iso^)^|*.iso' # Select iso files
echo }
echo #

echo clear-host
echo #  Select the USB disk
echo $USBDiskForm=New-Object System.Windows.Forms.Form # Create the screen form ^(window^)
echo # $USBDiskForm ^|gm;pause
echo # Set the title and size of the window:
echo $USBDiskForm.Text='Windows installation disk^'
echo $USBDiskForm.Width=490 ; $USBDiskForm.Height=210
echo $USBDiskForm.AutoSize=$true # AutoSize property to make the form automatically stretch, if the elements on the form are out of bounds
echo # Create a drop-down list and fill it
echo $USB=$Null 
echo $USBDisks=@^(^) # array with USB disk number
echo $Disks=Get-Disk ^| Where-Object {^($_.BusType -eq "USB"^) -and ^($_.OperationalStatus -eq "Online"^)}

echo $USBDiskList=New-Object System.Windows.Forms.ComboBox
echo # $USBDiskList^|gm
echo $USBDiskList.Width=450
echo Foreach ^($USBDisk in $Disks^) {
echo    $FriendlyName=^($USBDisk.FriendlyName^).PadRight^(30," "^).substring^(0,30^)    
echo    $Partitions = Get-Partition ^| Where-Object { $_.DiskNumber -eq $USBDisk.DiskNumber}
echo    If ^($Partitions^) {
echo        Foreach ^($Partition in $Partitions^) {
echo        $Volumes=get-volume ^| Where-Object {$Partition.AccessPaths -contains  $_.path }
echo        Foreach ^($Volume in $Volumes^) {
echo            $USBDisks+=$USBDisk.DiskNumber
echo            $USBDiskList.Items.Add^(^(" {0,-30}| {1,1}:| {2,-30}| {3:n2} GB" -f $FriendlyName, ^($Partition.DriveLetter^), $Volume.FileSystemLabel.PadRight^(30," "^).substring^(0,30^), ^($Partition.Size/1GB^)^)^)^|out-null
echo            }       
echo        }
echo    } Else {
echo        $USBDisks+=$USBDisk.DiskNumber
echo        $USBDiskList.Items.Add^(^(" {0,-30}| {1,1}| {2,-30}| {3:n2} GB" -f $FriendlyName, " ", " ",^($USBDisk.Size/1GB^)^)^)^|out-null
echo    }
echo }

echo $SelectUSBDisk=New-Object System.Windows.Forms.Label # Put the SelectUSBDisk label on the form
echo $SelectUSBDisk.Location=New-Object System.Drawing.Size^(10,20^)
echo $SelectUSBDisk.Text="Select USB disk";$SelectUSBDisk.Font='Segoe UI,10'
echo $SelectUSBDisk.width=200;$SelectUSBDisk.height=20;
echo $USBDiskForm.Controls.Add^($SelectUSBDisk^)

echo $USBDiskList.DropDownStyle=[System.Windows.Forms.ComboBoxStyle]::DropDownList;
echo $USBDiskList.Location=New-Object System.Drawing.Point^(10,40^)
echo $USBDiskList.width=450;$USBDiskList.height=20 
echo $USBDiskList.backColor="blue" ; $USBDiskList.ForeColor="White"
echo $USBDiskList.Font='Segoe UI,10'
echo $USBDiskForm.Controls.Add^($USBDiskList^)

echo $SelectISOButton=New-Object System.Windows.Forms.Button # Put the SelectISO button on the form
echo $SelectISOButton.Location=New-Object System.Drawing.Size^(10,100^)
echo $SelectISOButton.Text="Select ISO";$SelectISOButton.Font='Segoe UI,10'
echo $SelectISOButton.width=80;$SelectISOButton.height=60;
echo $USBDiskForm.Controls.Add^($SelectISOButton^)
echo $SelectISOButton.Add_Click^({$global:SelectISO=$true;$USBDiskForm.close^(^)}^)
echo $USBDiskForm.Controls.Add^($SelectISO^)

echo $CreateUSBDiskButton=New-Object System.Windows.Forms.Button # Put the SelectISO button on the form
echo $CreateUSBDiskButton.Location=New-Object System.Drawing.Size^(350,130^)
echo $CreateUSBDiskButton.Text="Create USB Disk";$CreateUSBDiskButton.Font='Segoe UI,10'
echo $CreateUSBDiskButton.width=110;$CreateUSBDiskButton.height=30;
echo $USBDiskForm.Controls.Add^($CreateUSBDiskButton^)
echo $CreateUSBDiskButton.Add_Click^({$global:SelectISO=$true;$USBDiskForm.close^(^)}^)
echo $USBDiskForm.Controls.Add^($SelectISO^)

echo $CancelButton=New-Object System.Windows.Forms.Button # Put the Cancel button on the form
echo $CancelButton.Location=New-Object System.Drawing.Size^(260,130^)
echo $CancelButton.Size=New-Object System.Drawing.Size^(120,20^)
echo $CancelButton.Text="Cancel";$CancelButton.Font='Segoe UI,10'
echo $CancelButton.width=60;$CancelButton.height=30;
echo $USBDiskForm.Controls.Add^($CancelButton^)
echo $CancelButton.Add_Click^({$global:SelectISO=$False;$USBDiskForm.close^(^)}^)
echo [void]$USBDiskForm.ShowDialog^(^) # Display the form on the screen
echo if ^(!$global:SelectISO^){$wshell.Popup^("Operation cancelled",0,"Error"^) ^| Out-Null;exit}
echo # 
echo [void]$FileBrowser.ShowDialog^(^)
echo #
echo $ImagePath = $FileBrowser.FileName;
echo $ISO=":"
echo If^($FileBrowser.FileNames -like "*\*"^) {
echo #  Check if iso already mounted
echo    $ISO = ^(Get-DiskImage -ImagePath $ImagePath ^| Get-Volume^).DriveLetter 
echo #  Mount iso
echo    IF ^(!$ISO^) {Mount-DiskImage -ImagePath $ImagePath -StorageType ISO ^|out-null
echo    $ISO = ^(Get-DiskImage -ImagePath $ImagePath ^| Get-Volume^).DriveLetter}
echo    $ISO=$ISO+":"
echo }
echo if ^($ISO -eq ":"^) {$wshell.Popup^("No ISO image mounted or operation cancelled",0,"Error"^) ^| Out-Null;exit}
echo $USB=$USBDisks[$USBDiskList.SelectedIndex]
echo #  Clear the USB stick
echo Clear-Disk $usb -RemoveData -RemoveOEM -Confirm:$false 
echo Stop-Service ShellHWDetection
echo #  Create the fat32 boot partition
echo $usbfat32=^(New-Partition -DiskNumber $usb -Size 1GB -AssignDriveLetter -IsActive ^| Format-Volume -FileSystem FAT32 -NewFileSystemLabel "BOOT"^).DriveLetter + ":"
echo #  Create the ntfs intall partition
echo $usbntfs=^(New-Partition -DiskNumber $usb -UseMaximumSize -AssignDriveLetter ^| Format-Volume -FileSystem NTFS -NewFileSystemLabel "INSTALL"^).DriveLetter + ":"
echo Start-Service ShellHWDetection
echo # Read-Host "Eject the iso if it is mounted. When ready press Enter"
echo # $Volumes = ^(Get-Volume^).Where^({$_.DriveLetter}^).DriveLetter
echo # Read-Host "Mount the iso. When ready press Enter"
echo # $ISO = ^(Compare-Object -ReferenceObject $Volumes -DifferenceObject ^(Get-Volume^).Where^({$_.DriveLetter}^).DriveLetter^).InputObject
echo # $ISO = ^(get-volume^| Where-Object {^($_.DriveType -eq "CD-ROM"^) -and ^($_.filesystemlabel -ne ""^) -and ^($_.OperationalStatus -eq "OK"^)} ^|Out-GridView -Title 'Select Cd-Rom image' -OutputMode Single^).DriveLetter + ":"
echo # Copy-Item -Path $ISO\* -Destination "$($usbntfs)" -Recurse -Verbose
echo # Copy-Item -Path $ISO"\bootmgr" -Destination $usbfat32"\" -Verbose 
echo # Copy-Item -Path $ISO"\bootmgr.efi" -Destination $usbfat32"\" -Verbose 
echo # Copy-Item -Path $ISO"\boot" -Destination $usbfat32"\boot" -Recurse  -Verbose 
echo # Copy-Item -Path $ISO"\efi" -Destination $usbfat32"\efi" -Recurse  -Verbose 
echo # new-item $usbfat32"\sources" -itemType Directory
echo # Copy-Item -Path $ISO"\sources\boot.wim" -Destination $usbfat32"\sources\boot.wim"  -Verbose 
echo robocopy $iso $usbntfs /e
echo robocopy $iso"\" $usbfat32"\" bootmgr bootmgr.efi
echo robocopy $iso"\boot" $usbfat32"\boot" /e
echo robocopy $iso"\efi" $usbfat32"\efi" /e
echo robocopy $iso"\sources" $usbfat32"\sources" boot.wim
echo # Eject the mounted iso image
echo # ^(New-Object -ComObject Shell.Application^).Namespace^(17^).ParseName^($ISO^).InvokeVerb^("Eject"^)
echo DisMount-DiskImage -ImagePath $ImagePath ^|out-null
echo Remove-item ^($env:TEMP + "\script.ps1"^)  
)>"%temp%\script.ps1"
powershell "%temp%\script.ps1" -ExecutionPolicy Bypass -WindowStyle Normal

Can someone help with the script? Thanks

freddie-o

Posted 2019-07-30T06:29:14.193

Reputation: 65

No answers