Scripting Diskpart with PowerShell

On my projects I often require many mailbox databases on the Exchange mailbox servers when I do my designs. With many databases I typically also require many volumes to host them on the servers. Creating many volumes is not fun and a very laborious task. So I would naturally look at ways to script this process to save time.

This does mean I have to learn how to script it and write the script, which can take more time than actually just doing it. However my philosophy is: always automate, because you never know when you have to do it again.

Windows provides a command line tool to do disk management called Diskpart. Diskpart can use a text file as input to script the command sequence using the /s parameter.

So the first step would be to create the script file for Diskpart and the next would be to run Diskpart.

Say for example you want to create 16 volumes on your server and they are presented as Disk 0 to Disk 15 in your disk subsystem. You can very easily use Windows PowerShell to create the file.
Creating a loop of 16 instances is as easy as typing: 1..16 in PowerShell. We can pipe that result to another process to generate the file. The easiest process I found is the Foreach-Object loop, because it has a “Begin” parameter, which you can use to initialize your code snippet and an “End” parameter which you can use to generate the final file.

Another issue I had was adding an “Enter” at the end of each line. I first wanted to use an array to achieve that, but each instance of the 16 volumes would require more than one line. That is why I add the [char][int](13)+[char][int](10) characters behind each line I generate.

It is important to note that Diskpart only reads ASCII encoded files and by default PowerShell would create Unicode files. So it is important to specify the –Encoding ASCII parameter when saving the file.

In my example I mount all the volumes under the C:\MDBDATA folder as MDB01 to MDB16. To create those folders you can run:

mkdir C:\MDBDATA
1..16 | ForEach-Object { mkdir $("C:\MDBDATA\MDB"+$("0"+$_).substring($($_).tostring().length-1,2)) }

Then to create the Diskpart text script file:

1..16 | ForEach-Object -Begin {$a = $null} -Process { $a += $("select disk "+$($_-1)+[char][int](13)+[char][int](10)) ; $a += "create partition primary"+[char][int](13)+[char][int](10) ; $a += $("assign mount=C:\MDBDATA\MDB"+$("0"+$_).substring($($_).tostring().length-1,2)+[char][int](13)+[char][int](10)) ; $a += $("format FS=NTFS LABEL=MDB"+("0"+$_).substring($($_).tostring().length-1,2)+" UNIT=64K QUICK"+[char][int](13)+[char][int](10)) } -End { $a | Set-Content diskpart.txt -Encoding ASCII }

WARNING: Before you run Diskpart, make sure the steps in the file will execute the right commands on the right volumes. If you don’t check the file, you could lose important data.

To run the commands in the text file type (Again, only if you are sure that it will do the right thing):

Diskpart /s diskpart.txt

I hope this method will save you time also!

Martin Coetzer

I practice the art of consulting by fusing the needs of clients with technology to create collaboration solutions.

martincoetzer has 11 posts and counting.See all posts by martincoetzer

Pin It on Pinterest