Summary: Honorary Scripting Guy, Sean Kearney, talks about using Windows Powershell to build scripts to automate DiskPart.
Hey, Scripting Guy! I know I can create a bootable USB key by using DiskPart, but is there any way to have it automated so the computer does the work for me?
Honorary Scripting Guy, Sean Kearney, here. I’m filling in for our good friend, Ed Wilson, who’s building some giant contraption in his backyard to hold his giant collection of bow ties. So he’s a bit “tied up.”
So the answer is absolutely, “Yes! Yes! Yes!” You can automate the creation of bootable memory keys. Having this ability is great if you’re using utilities like MDT 2012, and you need bootable media on the fly.
So I sat down and poked through my shiny new Surface Pro to dig for cmdlets to automate creating bootable partitions on USB. There are so many new modules and cmdlets to choose from.
Then my jaw dropped. There were none (at least none that I could spy), and I wanted to fix that.
Creating a bootable USB key is pretty easy. Most of us could run this process with our hands behind our backs while singing a happy tune. The standard procedure for a bootable USB key is:
- Launch DiskPart as an Administrator.
- Use List Disk to find your USB key (or keys if you’re being efficient).
- Use Select Disk to choose your USB key.
- Clean (to wipe out the MBR and delete the partition entries).
- Run Create Partition Primary to create a new primary partition on the USB flash drive.
- Format FS=FAT32 Quick to lay down the file system as Fat32.
- Assign (to give our friend a drive letter).
- Active to make it bootable.
So if your USB key always came up as DISK 2, you might have a little text file called “bootme.txt” with the following commands:
- Select DISK 2.
- Create Partition Primary.
- Format FS=FAT32 Quick.
You could happily run:
DISKPART /s .bootme.txt
….and whoosh! All done.
But wouldn’t it be far nicer to have something a bit more dynamic with the ability to figure out most of the work for you? Then you actually could do this with a touch of your fingers!
The ironic thing about DiskPart is that it works great, but it doesn’t have a native way to export the information it has.
But I know of something that can help. Wait for it…
Starts with a “P”…
Has a cool theme song…
Yes! Of course! PowerShell can aid here! We can actually have Windows PowerShell create scripts for DiskPart and leverage the output of DiskPart to create newer scripts for it!
Sounds like magic doesn’t it? Nope. Just a feature of Windows PowerShell that has been used before to automate applications like HANDLE.exe from Sysinternals.
So our first challenge is to create a text file for DiskPart to literally “List the disks.”
We could cheat and simply type something in Notepad, but if we use a script (or better yet, build a function), the solution will be self-contained.
So first—oh mighty PowerShell—make me a text file. We could do something really fancy and elegant, but sometimes simplicity is best. Simple means easy to modify and easy to understand. Here we go:
NEW-ITEM –name listdisk.txt –itemtype file –force | OUT-NULL
ADD-CONTENT –path listdisk.txt “LIST DISK”
Now we can automate DiskPart with this new mini script. But we’ll want to capture the output:
$LISTDISK=(DISKPART /S LISTDISK.TXT)
So what we’ve done here is capture all the output DiskPart was sending to the screen as a Windows PowerShell object. Now we can do all sorts of really neat stuff—like parse the data for what we want.
So the first thing we need to know from “LIST DISK” is, well, how many disks we’re looking at. Believe it or not, this is easier than you imagine. First off, the number of lines in the output is a built-in property:
Now there are a lot of funky ways to figure this out. But honestly, all we have to know is how many of the lines in the output do not contain the information we want—the lines with the disk information. That number happens to be 9. (Yes, I smudged up my screen and counted 1….2…..3….ketchup stain.)
So we can use Windows PowerShell to calculate this:
But we have a ways to go. We need to find out the assigned number for the drive if it’s a USB flash drive…whether it’s pink or green.
But we’ll come back tomorrow and show more about how we can parse the information from DiskPart!
I invite you to follow us on Twitter and Facebook. If you have any questions, send email to Ed at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Sean Kearney (filling in for our good friend Ed Wilson),
Honorary Scripting Guy, Windows PowerShell MVP
…and good personal friend of the BATCHman