Automating DiskPart with Windows PowerShell: Part 4

Doctor Scripto

Summary: Use Windows PowerShell to build scripts to automate DiskPart.

Hey, Scripting Guy! Question Hey, Scripting Guy! Can we use Windows PowerShell to return information from DiskPart as an object?


Hey, Scripting Guy! Answer Hello SH,

Honorary Scripting Guy, Sean Kearney, here. I’m filling in for our good friend, Ed Wilson. He’s got tie’s everywhere! Ties all over his property! The problem is they’re running amok! You see, he’s turned into a “tie” fighter (and may the farce be with you).

Today we’re going to pull all of this together into a function that we’re going to call Get-DiskPartInfo. We will pull all of these scripts together to automatically parse through the disks in DiskPart, pull their data, and return it as a [pscustomobject].

Note   This is Part 4 in a series. If you are behind, please read:

So shall we get started? First we define our function, but we’ll attach the appropriate parameters and make this a true advanced function:




Now let’s add our first DiskPart script to get the number of drives:

NEW-ITEM –name listdisk.txt –itemtype file –force | OUT-NULL
ADD-CONTENT –path listdisk.txt “LIST DISK”

Then we can loop through each disk, grabbing the DiskID and its physical size:

for ($d=0;$d -le $TOTALDISK;$d++)


$SIZE=$LISTDISK[-1-$d].substring(25,9).replace(” “,””)

Now that we have the DiskID, we can write a simple script for DiskPart to call up that disk and grab it’s Detail:

NEW-ITEM -Name detail.txt -ItemType file -force | OUT-NULL
ADD-CONTENT -Path detail.txt “DETAIL DISK”

And now with the Detail for that disk in hand, we run through our Detail parsing to pull the information we need from there:


Now we take a few minutes to convert that drive size to a real integer that we can use to compare the size of the removable disk:


  KB { $MULT = 1KB }
  MB { $MULT = 1MB }
  GB { $MULT = 1GB }


Then all we need do now is wrap it up neatly as a custom object in Windows PowerShell:


Pull this advanced function together and you can turn the following information…

Image of command output

…into useful output like the following in Windows PowerShell:

Image of command output

Now we can even do something like this:

GET-DISKPARTINFO | WHERE { $_.Type –eq ‘USB’ –and $_.DiskSize -lt16GB }

Here’s where things are going to be interesting tomorrow. With all this information as an object, I’ll bet you’re wondering if I can go the next level, and automatically format those USB keys. Check in tomorrow for our final segment where we’ll tie all of this into that and automatically build a key from Microsoft Deployment Tootkit (MDT) 2012!

I invite you to follow us on Twitter and Facebook. If you have any questions, send email to Ed at, 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


Discussion is closed.

Feedback usabilla icon