How to Automate Windows Diskpart Commands in a Script

Being able to automate disk partitioning, volume creation, and filesystem formatting can be very useful. It is especially handy for imaging processes, e.g. from a WinPE boot disk.

The windows diskpart command can be automated using a simple text file. Unfamiliar with Diskpart? Open Windows Command Processor (cmd.exe) and type:

diskpart

help

If you want help with a specific command, type:

help <command>

Where <command> is the command that you want help with.

To automate diskpart, you simply need to create a text file with your diskpart commands, then call diskpart from your command prompt or script and pass it the name of the text file.

For example, let’s say that you have booted a computer to WinPE, and wish to wipe the local “C:” drive and create a fresh one. To do so, open notepad.exe, then type the following:

select disk 0

clean

create partition primary

assign letter=C

active

format fs=ntfs label=Windows quick

Now save that as WipeC.txt and close Notepad.

Then run it like so:

diskpart.exe /s WipeC.txt

Please note that this example will completely remove the contents of the local Disk 0!

More specifically, it will: wipe all partitions off of Disk 0, create a single primary partition which will occupy the whole disk, assign that partition the drive letter C:, set it as active so that you can boot from it, and then create a volume called “Windows” formatted quickly as NTFS.

The Conversation

Follow the reactions below and share your own thoughts.

One Response to “How to Automate Windows Diskpart Commands in a Script”

  1. December 09, 2010 at 12:42 am, jimmyselix said:

    Nice! Perfect for deployment and imaging solutions too!

    Reply

Leave a Reply

You may also like-

How to manually partition ("Boot Camp") your Mac for Windows 7How to manually partition ("Boot Camp") your Mac for Windows 7This Tech-Recipe will show you how to MANUALLY modify an existing Mac hard disk to allow a Windows installation. Have you ever wanted to ... Solaris: disk usage of all users on a filesystemSolaris: disk usage of all users on a filesystemThe quot command provides a quick measure of the disk usage of multple users on a filesystem. The quote command must be run as ...