Windows Server 8 – Disk Management with PowerShell 3.0

Before Windows Server 8 and PowerShell 3.0, to manage your local, virtual or remote disks there were no native PowerShell cmdlets. You had only below choices;

– Using Diskpart (Easy for basic tasks but not flexible)

– Using WMI (Flexible but hard to use)

Diskpart has its own arguments and does not permit you to merge with PowerShell commands.


And using PowerShell, only chance for disk management is to use WMI library. Here are two examples:

Get-WmiObject -query "Select * from Win32_logicaldisk" |Ft



$Item = @("DeviceId", "MediaType", "Size", "FreeSpace")      Clear-Host       Get-WmiObject -computer YourMachine -query `       "Select $([string]::Join(‘,’,$Item)) from Win32_logicaldisk `       Where MediaType=12" | sort MediaType, DeviceID `       | Format-Table $item –auto

Not very user friendly right? Smile

From now on, In Windows Server 8 and PowerShell 3.0, you have native disk management cmdlets for local, virtual and remote disks.

In this blog post we’ll cover some of these great commands with examples.

PowerShell 3.0’s new designed ISE has a right pane that shows all available commands in GUI. Here is my previous blog post covers that :

Using search function within this pane  can sort all required commands.

Typing “disk” brings all available commands related to disk management.


Let’s dig in some of these commands in PowerShell 3.0. These are native commands and don’t need importing a module.

First basic command is Get-Disk.

Get-Disk returns all available disks or filtered list based on the criteria you specified.

I used Get-Disk to list all disk with only Number, OperationalStatus, Size and Partition columns. That also helps you to manage disks with a great flexibility.


You can also use Get-Disk cmdlet with various parameters such as;

Number : Returns specific information for a disk which has a specified disk number.

BusType: It returns only disks connected via specific bus type.

Get-Disk | Where-Object –FilterScript {$_.Bustype -Eq "USB"}

Get-Disk | Where-Object –FilterScript {$_.BusType -Eq "iSCSI"}

Another useful cmdlet is Get-Volume. It returns a volume object or a set of Volume objects given parameters to be used to uniquely identify a volume, or parameters to identify a set of volumes that meet the given set of criteria.


Also you can use Get-Partition to get a list of all available partitions.


Get-Partition returns all partitions on all disks. To filter Get-Partition output, there are several useful parameters;

DiskNumber: Returns all partitions on specified disk.

DriveLetter: Returns partitions associated with the specified volume.

PartitionNumber: Specifies the number of partition.

There is no native parameter to specify disk type but you can use –FilterScript parameter to filter Type column.

Get-Partition | Where-Object –FilterScript {$_.Type -Eq "Basic"}

Other than getting detailed information for partitions, you can use New-Partition cmdlet to create new partitions on an existing Disk Object.


UseMaximumSize parameter uses the maximum available space and –AssignDriveLetter parameter automatically assigns a drive letter to the partition while creating partitions.

But creating a new partition does not mean that it will be formatted. To format an existing partitions you should use Format-Volume cmdlet.

In below example, I created a new partition on disk 4 and then pipe that information to the Format-Volume cmdlet.

Format-Volume cmdlet always asks you for confirmations.


To bypass confirmation step, just use –confirm parameter with $false value.


As you can create new partitions, you can also remove existing partitions with Remove-Partition cmdlet. It requires DiskNumber and PartitionNumber parameters.


Using PowerShell means you can do bulk operations with all available commands. Let me show you a basic example.

I created a text file which has a DiskNumber first line(Column) and disk numbers for rest of each lines. It’s located as C:Disk.txts on my computer.


And also I have 4 disks and all of them are offline.


To convert all that disks to online, I can use below one line PS command.


Firstly it loads C:Disks.txt file into the shell and than for each line it runs Set-Disk command. $_.DiskNumber matches DiskNumber column in text file and reads each values.

You can change drive letter for an existing partition with Set-Partition cmdlet.


Resizing existing partition is also very straightforward step.

I have a single partition on disk 2 which was partitioned with it’s all available maximum size.( 1021MB)


Now I can use resize-partition cmdlet and –size parameter to create and resize existing partition.


Get-PartitionSupportedSizes cmdlet returns information on supported partition sizes for the specified Disk object.

For disk 2, there is one single partition (Partition 1) that has a MaxSize 1072627712


I can assign this value to a variable ($a) and this helps me to resize a partition with its maximum available size value.



Another command is Repair-Volume.

The Repair-Volume cmdlet performs repairs on a volume. The sub-function specified will be called as follows.

Fix: Calls the legacy scan and fix behavior (chkdsk /f).     ● Scan: Calls the Pro-scan only, all detected corruptions will be added to the $corrupt system file (chkdsk /scan).     ● SpotFix: Calls the spot fix functionality by taking the volume offline and then spot fixes only issues that are logged in the $corrupt file, (chkdsk /spotfix).

Providing DriveLetter and repair type is enough to run.


Using DiskPart or
WMI objects were not useful to design complex scripts. But in PowerShell 3.0 there are all required cmdlets to manage your local, virtual or remote disks.

One thought on “Windows Server 8 – Disk Management with PowerShell 3.0

  1. İyi günler, Windows Server 2003 ve Server 2008 R2 arasındaki farkları içeren bilgiye ihtiyacım var. Yardımcı olursanız sevinirim. İnternetten derlemeye çalıştım fakat yeterli olmadı.

Leave a Reply

Your email address will not be published. Required fields are marked *

51 − = 49