diskpart.exe CommandLine Interface

The easiest way to connect to you iSCSI storage LUNs from Windows Server 2008 is by using the iSCSI initiator GUI. With the GUI you can easily make a connection with your iSCSI Storage by giving the ip-address and dynamically discover the LUNs (or Volumes) it has to offer.

As I’m working on scripts to automate the installation and configuration of Windows Server 2008 systems, I was also looking for a way to configure the local disks and volumes of a Server, without having to use the GUI and manual instructions. Luckily Windows offers some nice tools for scripting. The scripting and configuration of the local disks and volumes can best be done with diskpart.exe. Which offers it’s own commandline interface and the option to use a preconfigured script with all the commands.

 

This post offers step-by-step instructions for the diskpart.exe command.

 

This article is updated with additional diskpart command information, provided by Olav Meijer.
The additions to the diskpart sample scripts are marked in red.
 

 

Using the diskpart commandline

To use the diskpart commandline, you open a DOS Command Promp and type in diskpart.exe. This will start the diskpart commandline where you can enter different special commands. Type help or /? to to view the available commands, or [command] help or /? for the options available for the given command.

 

* Starting the diskpart commandline:

C:\>diskpart.exe

Microsoft DiskPart version 5.1.3565

Copyright (C) 1999-2003 Microsoft Corporation.
On computer: [computer-name]

DISKPART>rescan

Please wait while DiskPart scans your configuration...
..........
DiskPart has finished scanning your configuration.

DISKPART> list disk

  Disk ###  Status      Size     Free     Dyn  Gpt
  --------  ----------  -------  -------  ---  ---
  Disk 0    Online       186 GB      0 B
  Disk 2    Online       466 GB      0 B

DISKPART> exit

Leaving DiskPart...

C:\>

 

Using diskpart with a script file

* Using diskpart with a script:

diskpart.exe /s [script-file]

 
Which uses the following additional syntax:

  • script-file: A textbased file with a set of commands to be run by the diskpart.exe command.

 

* Sample script file for changing a drive letter of the CDROM drive:

rescan
list volume
select volume [volume-id]
assign letter=[drive-letter]
list volume

 
Which uses the following additional syntax:

  • volume-id: The volume number of the CDROM device (usually the value is 0).
  • drive-letter: The new drive letter you want to assign to the CDROM device.

 

* Sample script for first use of (newly connected) iSCSI LUN:

rescan
list disk
select disk [disk-id]
online disk
attributes disk
attributes disk clear readonly
create partition primary <b><font color="red">align=[offset-in-kb]</font></b>
list partition
select partition [partition-id]
format fs=NTFS <b><font color="red">unit=[file-allocation-unit]</font></b> label="disk-label" quick
assign letter=[drive-letter]
list volume

 
Which uses the following additional syntax:

  • disk-id: Number of the disk to select (active disk).
  • offset-in-kb: Offset value for the partition, usually 1024 is sufficient.
  • partition-id: Number of the partition you want to format (value is 1 of it is the first partition created on the disk).
  • file-allocation-unit: Cluster size (in kb) to be used with the NTFS file system, depends upon disk usage. For SQL 64 is preferable.
  • drive-letter: The drive letter you want to assign to the newly created and formatted partion.

Allocation Unit (or Cluster) size is important for the performance of the hosted application that used the disk space, like SQL Server. It holds a relation with the stripe unit size, but this depends upon the SAN brand and type used and the administration of the SAN.

In Windows 2008 the alignment is done automatically, but for previous Windows versions, we need to manually add these to the script, hence the update for the script.
 

 

* Sample script for mounting a vDisk:

select vdisk file="[vhd-file-path]"
attach vdisk

 
Which uses the following additional syntax:

  • vhd-file-path: the path and filename of the VHD file to be mounted.

 

* Sample script for resizing volumes on a Disk:

First we make sure the right disk is selected and changed to a dynamic disk.

select disk [disk-id]
attributes DISK CLEAR READONLY NoErr
online disk NoErr
convert dynamic NoErr

As we are adding new volumes (and drive letters), we make sure our CDROM drive letter is changed first.

select volume [volume-id]
assign letter= [drive-letter]

Next we select the volume we want to reduce in size on the selected disk and use the shrink command to downsize the volume.

select volume [volume-id]
shrink desired=[mb-size]

Now we create a new volume with the previously created free disk space (in this example we specify the volume size).

create volume simple size=[mb-size] disk=0
assign letter=[drive-letter]
format fs=ntfs QUICK

Finally we create the last new volume with the remaining free disk space.

create volume simple disk=0
assign letter=[drive-letter]
format fs=ntfs QUICK

 
Which uses the following additional syntax:

  • disk-id: The disk number of the physical disk we want to select (starts with id 0).
  • volume-id: The volume number of the CDROM device (usually the value is 0).
  • drive-letter: The new drive letter you want to assign to the CDROM device.
  • mb-size: The disk space to be assigned or used (specified in Mbytes).

 

NoErr can be used for scripting only. When an error is encountered it specifies that DiskPart continues to process commands as if the error did not occur.
Without the NoErr parameter, an error causes DiskPart to exit with an error code and any remaining script is not executed.

 

 

This article was originally posted at Apr 11, 2010 @ 21:42 *** Last update: Added additional command options, provided by Olav Meijer. ***

Esther Barthel
Solutions Architect at cognition IT

Esther has been working in different roles and functions as an IT consultant ever since she finished her Masters degree in Computer Science in 1997. She has worked as a web developer, database administrator, and server administrator until she discovered how Server-Based Computing ( SBC ) combined servers, desktops, and user experience in one solution. Esther has been specializing in virtualization solutions such as SBC, VDI, application, and server virtualization for over eight years now and is currently working as a Senior Consultant at PepperByte, where she designs and implements Citrix® solutions for both small-business and large-enterprise infrastructures scaling from 100 to 15,000 users.
In january 2014 her first book Citrix XenApp 6.5 Expert Cookbook was published by Packt Publishing.

Esther is awarded as a Citrix Technology Professional (CTP) from 2015 - 2017.
Esther is awarded as a Microsoft Most Valuable Professional (MVP) in 2017.

Esther is a Citrix Certified Expert – Virtualization (CCE-V), Citrix Certified Professional – Mobility (CCP-M), Citrix Certified Professional – Networking (CCP-N) and RES Software Certified Professional (RCP).

4 thoughts on “diskpart.exe CommandLine Interface

  1. Pingback: VirtuEs.IT

  2. Pingback: Esther

  3. Pingback: Erroneous DiskPart operation and Partition Recovery | Harddrive Data Recovery - Data Storage

  4. Pingback: VirtuEs.IT