Use PowerShell to Create a Bootable USB Drive

Microsoft Scripting Guy, Ed Wilson, is here. Today, The Scripting Wife and others will be heading to Atlanta for the PowerShell Saturday #003 event that takes place tomorrow. Sadly, I woke up with no voice and a stuffy head amd am staying home.Jason Walker, a speaker at the event, is our guest blogger and shares with us a script and his blog about that script. Be sure to come to the Microsoft Alpharetta Office tomorrow to see Jason and several other Windows PowerShell speakers share their knowledge with you during Windows PowerShell Saturday.

Jason Walker is a Premier Field Engineer (PFE) at Microsoft who supports customers in the public sector arena. His primary job is supporting Exchange, but he jumps at the opportunity to flex his Windows PowerShell muscles to resolve any issue that may come up. It doesn’t matter if it’s Exchange-related or not. Jason also actively participates in the Charlotte PowerShell Users Group.

Twitter: AutomationJason

Here’s Jason …

With the release of Windows 8 and Windows Server 2012, there are thousands of additional new cmdlets at an IT Pro’s disposal—making our lives so much easier. The storage cmdlets are an example of this.

In the past, to create a bootable USB drive to install an operating system, you could download the Windows 7 USB tool, or you had to mount the ISO file with a non-Microsoft tool, copy the install bits, and then run a series of DiskPart commands.

Now, a bootable USB is easily created without needing DiskPart.exe or a non-Microsoft tool to mount the ISO file. Windows 8 has the ability to mount an ISO or VHD by simply double-clicking the file.

Here are the commands to accomplish this the old way:

From an elevated shell:

DiskPart.exe

List disk

Select disk 1—disk 1 being the USB drive

Clean

Create partition primary

Select partition 1—partition 1 being the new partition

Active

Format FS=NTFS

After you are past the series of DiskPart commands from the boot directory of the install bits, go to step 9.

bootsect.exe /NT60 G: – (G: being the USB drive)

Assuming the ISO is already mounted, now you can copy all the files from the ISO. Then, you are finally ready to install your operating system.

Now for the new Windows PowerShell way:

Get-Disk—this is needed to get the disk number of the USB

Clear-Disk –Number 1

New-Partition –DiskNumber 1 –UseMaximumSize –IsActive:$true

Format-Volume –FileSystem NTFS –DriveLetter G

bootsect.exe /NT60 G:

Use Copy-Item to copy your install bits to the USB drive and you’re done. Wait, there’s more! The storage module has a Mount-DiskImage cmdlet. Now the entire process can be scripted with all Microsoft tools. Here is my version with it all put together. You can download the complete script from the script repository. It vastly simplifies things and, not only that, it contains help, too.

Note The version of Windows PowerShell 3.0 for Windows 7 does not contain the Storage module at this time, so Windows 8 or Windows Server 2012 is a requirement.