So we now have a pile of bootable USB keys.  Woohoo!

Now what we need to do is get ROBOCOPY to transfer data to them.   Automating THAT should be quite easy.   For that we have some very simple requirements.

  • All Data is in a folder called “C:MDTImageContent”
  • We are going to dump the data to all blank USB drives that fall into the same parameters as before (larger than 8GB, smaller than 64GB)

For this we COULD play with DISKPART some more, pull up the DETAIL on each drive and match the VOLUMES somehow, or I could cheat.

You see the keys we are going to use fit two key criteria.  They are USB and have been VERY recently formatted (like about 4 minutes) ago.  So they should have a very high free space count.   For this task we can leverage WMI and simply ask the computer the following question.

“Oh Great and Mighty Deep Thought!  Please show us all USB Drives with only One partition! We need to see the ones where the free space is over 99%, the drive is larger than 8gb and smaller than 64gb! And Please…. No.  Don’t tell us it will take a Billion Years in a computer called the Earth!”

Well had this been any OTHER giant computer, it might have.  But we can leverage a script similar to the first by accessing the “DETAIL” of each removable USB disk to find it’s correct Volume Letter.

But if I go into DISKPART and pull up the DETAIL DISK on a recently formatted drive I can see what I need is there already


Since we are already pulling information from DETAIL DISK, we can modify the original script and have it access the drive Letter in the final line and incorporate it into the original script.

In our original script (in part 1) we parsed DETAIL DISK with the following lines.


Since we are still accessing the output from $detail, we can add in one more PowerShell variable.  Specifically we want the Last line (which contains the volume information we are looking for) and a VERY specific element, the Driver letter.


And we can just modify our [pscustomobject] to send that property back as an additional piece of info.


Now armed with a more improved script we can ask Windows PowerShell to talk to his friend DISKPART to get the Drive Letter for the Single Volume on all Removable USB Drives larger than 8GB and smaller than 64GB.

$letters=(./QUERYDISKPARTV2.PS1 | Where { $-.Type –eq $Type –and $-.Disksize –gt $Min –and $-.Disksize –lt $Max } | SELECT-Object DriveLetter)

Now armed with this information we can have Robocopy do the rest of the work for us.


$letters { FOREACH { $Destination=$-.DriveLetter+”`:”; START-PROCESS ROBOCOPY.EXE –ArgumentList “$Source $Destination /E /S”} }

So now if we tie all these pieces together we could build some kinda of cool crazy automatic USB key creator for MDT that does NOT involve showing somebody the inner workings of DISKPART and ROBOCOPY.

We can turn it into a “Click Here” solution.

Kinda cool eh?

Remember, the Power of Shell is in YOU

The Energized Tech