Build a PowerShell-Enabled Windows PE Key: Part 3

Doctor Scripto

Summary: Identify available USB keys and make them bootable devices.

Honorary Scripting Guy, Sean Kearney, is here to continue delving into having a cool PowerShell experience in a Windows PE environment. Today I’m going to show you how to use Windows PowerShell to identify USB keys and make them bootable devices.

   Note   This is a five-part series that includes the following posts:

With Windows PowerShell finding a USB key is actually quite easy. We just use the Get-Disk cmdlet. This will show us all disks on the computer. There is a property we can filter on called BusType. A USB key is on the BusType called USB, conveniently enough. We can filter it in this way:

Get-Disk | Where-Object { $_.BusType –eq ‘USB’ }

Now let’s play my favorite game, “what if.” What if there was more than one USB key or drive attached? How could you choose?

We can pipe the output to Out-Gridview and use the –passthru parameter. This will output a simple list of all the USB keys, including their sizes, to allow you to choose.

Get-Disk | Where-Object { $_.BusType –eq ‘USB’ } | Out-Gridview –passthru

Image of command output

Now that we have an easy way to select the USB keys, we can store this away in a PowerShell object we’ll call $Disk:

$Disk= Get-Disk | Where-Object { $_.BusType –eq ‘USB’ } | Out-Gridview –passthru

The next task is to clear away any partitions on the key. Normally Clear-Disk should work, but I’ve experienced some issues in which I’ve had to purge the partition table first. To properly eradicate all data on the USB key, I use a combination of Clear-Disk and Remove-Partition. I add SilentlyContinue to ErrorAction in case the key has already been purged.

Get-Disk -Number ($Disk.number) | Get-Partition | Remove-partition -confirm:$false -ErrorAction SilentlyContinue
Clear-Disk -Number $Disk.Number -RemoveData -RemoveOEM -confirm:$false -ErrorAction SilentlyContinue

Now that the key has been purged of data, we can begin creating a bootable key. We’re going to assign a temporary drive letter of Z to the USB key.

We first initialize the USB key we selected earlier and stored in $Disk:

Initialize-Disk -Number $Disk.Number -PartitionStyle MBR -ErrorAction SilentlyContinue

We then create a single partition to store the data. The –UseMaximum parameter will assign all available space to the partition on the USB key. –IsActive marks it as an active partition for booting.

$OSDrive=’Z’
$Partition=New-Partition -DiskNumber $Disk.Number -DriveLetter $OSDrive -UseMaximumSize –IsActive

We format this as Fat32 because we our planning for the ability of booting in a modern UEFI platform. UEFI does not recognize NTFS as a filesystem for booting.

Format-Volume -Partition $Partition -FileSystem FAT32 -NewFileSystemLabel 'Windows'

Now we have the ability to create a bootable USB key with a selectable menu. Neat, eh?

Pop in tomorrow as we look into starting to build out the file structure needed for the Windows PE environment!

I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to them at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then always remember that with great PowerShell comes great responsibility.

Sean Kearney, Honorary Scripting Guy, Cloud and Datacenter Management MVP

0 comments

Discussion is closed.

Feedback usabilla icon