And now the fun begins.   Getting DISKPART and Powershell to do all the dirty work for us.  Cue the “Muah ha ha haaaaa!”

So automating DISKPART is actually very easy.   You have a text file that contains all the commands you need to run and you execute it in the following manner.

DISKPART /S Script.txt

That’s it!

But the challenge when creating a bootable USB key (at least for automating) is the the Disk number for removable devices may not always be the same number, or that you MIGHT have in an external hard drive when you do this.

So now that we have the PowerShell script from the previous articles which pulls out this data from Diskpart, we just need to build a text file.

Now we need to put some thought into this.  If I were just building ONE USB key everytime, I’d just ‘Go for broke’ and target a single key.  

But my goal is I would like to drop between one (1) and four (4) keys into a computer and have the following occur.

  • Identify the USB key
  • ERASE and CLEAN it within DISKPART
  • Make the USB key ACTIVE and as such bootable
  • Eventually tie in Robocopy to transfer a specified “CONTENT” folder from MDT directly onto said keys.

So we know I can use the new script QUERYDISKPART.PS1 to pull out all of the data.  But let’s imagine I have a bunch of USB keys like on this computer.  I would like to have a DISKPART script that will automatically format all of them as bootable USB devices.


So if you don’t already know the sequence to create a bootable USB key in DISKPART it is.

SELECT DISK Num (Where Num is the indexed numbered assigned the Disk for DiskPart)

CLEAN (Which wipes out the Partition table)

CREATE PARTITION PRIMARY (Which creates a New Primary Partition on the Selected Disk)

FORMAT FS=FAT32 QUICK (Which creates a FAT32 partition)

ASSIGN (which assigns a drive letter to the partition)

ACTIVE (which makes the partition active and bootable)

so a simple text file script for DISKPART to make a bootable USB key could look like this


So really all we will need to do is loop through the information, create a text file with this sequence of commands for each disk, and change the “SELECT DISK 1” with the appropriate value from DiskNum.

Now I could do things really fancy (and if you’d like to improve, by all means dive in!) but I’m going to be very brutish and manual in how I create the text file.

First we launch the “QUERYDISKPARTv2.PS1” script and capture the objects that are USB and within the Size we’d like.




$DRIVELIST=(.QueryDiskpartv2.ps1 | where { $-.Type –eq $Type –and $-.Size –gt $Min and $Size –lt $Max })

Now that we have a list of drives we can Build a single script to handle them all

NEW-ITEM -Path bootemup.txt -ItemType file -force | OUT-NULL


ADD-CONTENT –path bootemup.txt –Value “SELECT DISK $DISKNUM”
ADD-CONTENT –path bootemup.txt –Value “CLEAN”
ADD-CONTENT –path bootemup.txt –Value “FORMAT FS=FAT32 QUICK”
ADD-CONTENT –path bootemup.txt –Value “ASSIGN”
ADD-CONTENT –path bootemup.txt –Value “ACTIVE”


Once this process is complete we will have the script called ‘bootemup.txt’ which we can launch from DISKPART like this

DISKPART /S bootemup.txt

Let that run through and all the keys attached will become bootable.  But our next challenge, having Robocopy do the transfers to all those drives!

Well that we’ll talk about next time.

Remember, the Power of Shell is in YOU.

The Energized Tech