While I was in the Windows team I have been confronted several timers to partition all disks on new delivered servers that were about to be used by the applications. I have decided to ease my life by writing a small script in Powershell. All what I found on the Internet was only working with Windows 2012 for which the disk management has been reviewed and eased with the help of new commands. At that time our infra was still running under Windows 2003 and Windows 2008 the nothing was fitting. So I did with the tools I had and this version is working on Windows 2003 and 2008 but also on Windows 2012 . 

 This script has to be only used assuming that all drives except the system one have to be partitioned. 

What this script is doing is pretty basic  : 

  1. Preparation of a directory in the directory c:\temp
  2. Creation of a file "listdisk.txt" in the directory "DISK"
  3. Listing of all disks attached to the server and writing them in the file "listdisk.txt"
  4. Calculation of the disk amount that have to be partitioned, aligned and formatted based on the fact that the 7 first lines of DISKPART are comments and that the 8nd line is the one forst the disk 0 or system disk that we won't touch.
  5. Entering into a loop in order to write the partitioning, alignement and format commands in "listdisk.txt" for each disk.
  6. Finally we launch DISKPART with the "listdisk.txt" file as attribute so he will use the command line in this file.

 The Powershell console needs to be Run as Administrator. You can either copy and paste those lines in order to create a .PS1 file or then copy and paste those lines in your command console.

 

#DISK PARTITIONING FOR SQL SERVERS (SQL NEEDS A SPECIFIC DISK ALIGNEMENT) 

cd c:\temp

NEW-ITEM -name disk -itemtype directory -force

cd disk

NEW-ITEM –name listdisk.txt –itemtype file –force | OUT-NULL

ADD-CONTENT –path listdisk.txt “LIST DISK”

$LISTDISK=(DISKPART /S LISTDISK.TXT)   

$TOTALDISK=$LISTDISK.count - 8

 

$i = 1

while( $i -le $TOTALDISK)

{

ADD-CONTENT –path listdisk.txt "SELECT DISK $i"

ADD-CONTENT –path listdisk.txt "ONLINE DISK"

ADD-CONTENT –path listdisk.txt "ATTRIBUTES DISK CLEAR READONLY"

ADD-CONTENT –path listdisk.txt "CLEAN"

ADD-CONTENT –path listdisk.txt "CREATE PARTITION PRIMARY ALIGN=1024"

ADD-CONTENT –path listdisk.txt "SELECT PART 1"

ADD-CONTENT –path listdisk.txt "FORMAT FS=NTFS LABEL=NewVolume UNIT=64k QUICK"

ADD-CONTENT –path listdisk.txt "ASSIGN"

$i++

}

 

DISKPART /S listdisk.txt

 

 

#DISK PARTINIONG FOR USUAL SERVERS

cd c:\temp

NEW-ITEM -name disk -itemtype directory -force

cd disk

NEW-ITEM –name listdisk.txt –itemtype file –force | OUT-NULL

ADD-CONTENT –path listdisk.txt “LIST DISK”

$LISTDISK=(DISKPART /S LISTDISK.TXT)   

$TOTALDISK=$LISTDISK.count - 8

 

$i = 1

while( $i -le $TOTALDISK)

{

ADD-CONTENT –path listdisk.txt "SELECT DISK $i"

ADD-CONTENT –path listdisk.txt "ONLINE DISK"

ADD-CONTENT –path listdisk.txt "ATTRIBUTES DISK CLEAR READONLY"

ADD-CONTENT –path listdisk.txt "CLEAN"

ADD-CONTENT –path listdisk.txt "CREATE PARTITION PRIMARY"

ADD-CONTENT –path listdisk.txt "SELECT PART 1"

ADD-CONTENT –path listdisk.txt "FORMAT FS=NTFS LABEL=NewVolume QUICK"

ADD-CONTENT –path listdisk.txt "ASSIGN"

$i++

}

 

DISKPART /S listdisk.txt