In the last years, since Windows Server 2012 and Windoes 8, Microsoft implemented a cool Powershell module used to manage storage. This module is called Storage and it’s CmdLets implement most of DISKPART’s features. Common tasks like initializing disks, creating partitions, formatting volumes, resizing partitions, assigning drive letters and more are very intuitive with Powershell’s verb-noun approach. You can get a list with all commands from this module by running:
Get-Command -Module Storage
Getting info with the Storage Module
I prepared a Windows Server 2012 R2 server with 3 disks: 1 is online and contains 1 partition with the OS and the other 2 are offline and uninitialized. Let’s see how to manipulate the disks with the Storage Powershell module.
Let’s see how to get some basic information about our storage situation. First I want to see a list of all disks on my system (physical and virtual).
The output shows some interesting things like the disk number used to reffer to a specific disk when performing any action on it, operational status, total size and the partition style (GPT or MBR). More info about each disk is available but this is just a default view.
For a list of only the actual physical disks we need to run the following command:
The number at the end of the friendly name corresponds to the disk number found in the previous output. Since the disks can be partitioned let’s get a list of all partitions. In my case only disk 0 is configured so it is the only one that has partitions.
Like with disks, partitions have numbers; the difference is that these numbers are local to each disk. This means that to alter the C partition we need to tell Powershell to find the partition number 4 on disk number 0. The last thing of interest to see is a list of volumes.
This CmdLet offers as info the size and remaining size of a volume and the file system with which it was formatted. Another useful thing is the File System Label and Drive Type.
Making changes with the Storage Module
It’s time now to create, resize and remove some partitions. Each disk needs to be initialized before being used. The initialization is the point in which you specify what partition style to use (GPT or MBR). Initialization is done with just a command and it’s very straight forward.
Initialize-Disk -Number 1 -PartitionStyle MBR
In this example I initialized disk 1 and made the partition style MBR. The next logical thing to do is to partition the size (a partition is just a delimitation of space, it is not a formatted volume with a file system and used/free space). You can specify how big to make the partition and can assing it a drive letter or let the OS assing the next available one for you. Just to keep things simple I will use all the space available on the disk and let the OS give the partition a letter.
New-Partition -DiskNumber 1 -UseMaximumSize -AssignDriveLetter
By now Windows for sure gave you the pop-up about formatting the new partition. Cancel it because we will do it next. When formatting you can now use the Drive Letter to refference the partition. Besides choosing a file system like NTFS, you can also specify a Label.
Format-Volume -DriveLetter E -FileSystem NTFS -NewFileSystemLabel "Data"
A common operation is resizing partitions. This can also be done easily with the help of a CmdLet. When resizing just say the Disk Number and Partition Number and also the new size. Size is specified using a number followed by the unit size (MB, GB, TB).
Resize-Partition -DiskNumber 1 -PartitionNumber 1 -Size 5GB
If you are really mad you can also remove the partition from the disk. I am pretty sure you already figured out what CmdLet we need for this task so let’s just do it.
Remove-Partition -DiskNumber 1 -PartitionNumber 1
There is also the possibility to clean the whole disk. This means removing all it’s partitions and uninitializing it. You have to use the -RemoveData switch if you have partitions on it.
Clear-Disk -Number 1 -RemoveData
And here is the result:
One last thing I want to cover is how to make modifications like drive letter to a volume or partition. This is done using a combination of the Get CmdLets and the Set CmdLets. Let’s take for example changing the letter of a partition. Just get it and pipe it to the set command:
Get-Partition -DiskNumber 1 -PartitionNumber 2 | Set-Partition -NewDriveLetter G
This was just a small intro to what can be done. Explore the CmdLets and see the rest of their functions; there are a lot.