How to create a bootable USB disk using PowerShell?

There are lot of ways to create a bootable USB disk, like using command prompt, windows tools, and many 3rd party software. In olden days we used to install operating system using CD/DVD, Those days were are gone now in modern days flash drive become popular Today in this post we are going to create a bootable USB disk using PowerShell. PowerShell is an advanced form of command prompt. It is extended with a huge set of ready-to-use cmdlets and comes with the ability to use .NET framework/C#. Which can be embedded within other applications. It automates batch processing and creates system management tools. It includes more than 130 standard command-line tools for functions and enables administrators to perform tasks on local and remote Windows systems through access to Component Object Model (COM) and Windows Management Instrumentation (WMI). So without wasting any time will jump into the topic. This post will guide you to create a bootable USB disk using PowerShell.

Table of Contents

Method 1:

  • Open Power shell in admin mode, copy and paste the below-mentioned command.
$Results = Get-Disk |

Where-Object BusType -eq USB |

Out-GridView -Title 'Select USB Drive to Format' -OutputMode Single |

Clear-Disk -RemoveData -RemoveOEM -Confirm:$false -PassThru |

New-Partition -UseMaximumSize -IsActive -AssignDriveLetter |

Format-Volume -FileSystem FAT32
create a bootable USB disk using PowerShell

Let’s see in detail about these commands.

$Results= Get-Disk : First this command will list out all the attached drives, its like diskpart command, In diskpart we use list disk to know the drive letters and attached usb drives.

Where-Object BusType -eq USB : After listing all connected drives this command will filter out the USB drive.

Out-GridView -Title ‘Select USB Drive to Format’ -OutputMode Single:  This command will give you the list of USB Drive in a grid view. Now you can choose the correct disk and click ok.

Clear-Disk -RemoveData -RemoveOEM -Confirm:$false –PassThru: This command clear all the data in the partition off the disk.

New-Partition -UseMaximumSize -IsActive –AssignDriveLetter: This command creates a new partition using all of the available space on the USB drive and assigns a drive letter to it.

Format-Volume -FileSystem FAT32: This command Format the USB drive in FAT 32 format. You can choose different format also, instead of FAT32 you can type NTFS or some other format. Here we use FAT32 because most of the laptop and server won’t boot the USB Drive in NTFS format. So as for now the USB Drive has been successfully formatted in FAT32 format and ready to copy to the OS files. Now will Jump to the next session.

$Volumes = (Get-Volume).Where({$_.DriveLetter}).DriveLetter

Mount-DiskImage -ImagePath D:\Software\Windows 10 Enterprise 1809 x64 - Integral Edition 2018.12.15 - SHA-1; 51820ffb35536e97ad533361f7f89f248208d01a.ISO

$ISO = (Compare-Object -ReferenceObject $Volumes -DifferenceObject (Get-Volume).Where({$_.DriveLetter}).DriveLetter).InputObject

This command get the ISO file path and copy the files to already mounted USB drive. Choose the ISO file path correctly.

Set-Location -Path "$($ISO):\boot"bootsect.exe /nt60 "$($Results.DriveLetter):"Copy-Item -Path "$($ISO):\*" -Destination "$($Results.DriveLetter):" -Recurse -Verbose

This command will make the USB Drive bootable.

Method 2:

I think compare to the first method the second one is the best, in this method we are going to use a script which is very handy to use it more, and also there is no need to memorize a bunch of command line, you can achieve making bootable USB in a single line of code. In my case the USB stick is associated with E: and the ISO is mounted on F:. The following command sets up a bootable Windows USB stick:

Open Power shell ISE in admin mode, copy and paste the below mention command.

Create-BootableUSBStick -USBDriveLetter D: -ImageFiles H:

function Create-BootableUSBStick {

[CmdletBinding()]

param

(

[Parameter()]

$USBDriveLetter,

[Parameter()]

$ImageFiles

)

$USBDriveLetterTrim=$USBDriveLetter.Trim(':')

Format-Volume -FileSystem NTFS -DriveLetter $USBDriveLetterTrim -Force

bootsect.exe /NT60 $USBDriveLetter

xcopy ($ImageFiles +'\') ($USBDriveLetter + '\') /e

Invoke-Item $USBDriveLetter

}

you have to create a folder in C:\Program Files\WindowsPowerShell\Modules. Name it Create-BootableUSBStick. Then Save this code whatever the name you want

Example : BootableUSBStick.psm1

Then open the power shell in the admin mode and type the below mention command.

Create-BootableUSBStick -USBDriveLetter D: -ImageFiles H:

Here you have to mention the USB drive Letter at the first and the image file path. Suppose if you mounter the iso in a separate drive you can mention the same path here. Try this method if you have any doubt or difficulties kindly ping me in the comments.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top