Automate Bootable USB creation with Powershell–Part 2

So the first challenge was the trickiest part.   Now for the fun!  Making some bootable USB keys!

Well not QUITE yet.   We want to be smart about this.  We’re going to define some parameters for a bootable USB key.

Think about it.  If I had a script that could just arbitrarily go out and take every disk attached and WIPE it as a bootable device, wouldn’t that be a little dangerous?  So we’re going to be smart about this.

First let’s define SOME parameters.   For this to be useful and somewhat safe we’re going to use the following parameters

  • USB device
  • No Smaller than 8 gigabytes
  • No Larger than 64 gigabytes

Now this may not define all possible combinations for safety, perhaps we’ll leverage using the “-whatif” parameter later on.  But this will be a start.  Anything smaller than 8gb won’t be a good candidate for and MDT Deployment key.   Anything larger than that is more than likely a removable hard disk.

So from our last script ‘QueryDiskPart.ps1’ we saw the output looked like this


And if you run a GET-MEMBER against the output you can see the types of useful data now, instead of just “Console output” from DISKPART


So we can now do a filter to show only Devices which are attached by USB, which is pretty cool

.\QueryDiskpartv2.ps1 | where { $_.Type –eq ‘USB’ }


This is not surprising.  In fact we COULD ask Windows for similar information from WMI using Win32_disk.

get-wmiobject -query “SELECT * from win32_diskdrive where Interfacetype = ‘USB'” | Select-object Model,InterfaceType,Size


What this is MISSING is the “Disk Number” which is only produced when the Disks are enumerated in DISKPART.

So this is why we must parse DISKPART.  It *IS* a very slow process but now that we have the “DiskNum” we can build the script.

Now of course I said the next thing we’ll want to do is Identify disks which are between a certain size.    At this point let’s define some PowerShell variables for the sizing and the Interface type.




This will allow us to run the query like so.

.\QueryDiskpartv2.ps1 | where { $_.Type –eq $Type –and $_.Size –gt $Min and $Size –lt $Max }

Now we can think about our next task.   Since we can identify Removable USB keys and even identify the enumerated number it contains in DISKPART, we should be able to build the DISKPART script.

And we will…. next time

Remember the Power of Shell is in You

The Energized Tech

One thought on “Automate Bootable USB creation with Powershell–Part 2

Comments are closed.