Share via


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.

http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-79-57-metablogapi/6761.image_5F00_thumb_5F00_009E864F.png

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? http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-79-57-metablogapi/1805.wlEmoticon_2D00_smile_5F00_08D2ABE6.png

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 : http://blogs.technet.com/b/meamcs/archive/2012/03/30/powershell-3-0-shell-from-future.aspx

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

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

http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-79-57-metablogapi/1884.1_2D00_1_5F00_thumb_5F00_79DB56FE.jpg

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.

http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-79-57-metablogapi/0410.1_5F00_thumb_5F00_6043A404.jpg

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.

http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-79-57-metablogapi/6685.2_5F00_thumb_5F00_50E01C28.jpg

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

http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-79-57-metablogapi/8322.3_5F00_thumb_5F00_5933CBB2.jpg

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.

http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-79-57-metablogapi/3157.4_5F00_thumb_5F00_137F1BC7.jpg

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.

http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-79-57-metablogapi/1488.6_5F00_thumb_5F00_10C63A49.jpg

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

http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-79-57-metablogapi/4135.8_5F00_thumb_5F00_7C504EF0.jpg

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

http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-79-57-metablogapi/8764.7_5F00_thumb_5F00_5003A23F.jpg

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.

http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-79-57-metablogapi/4456.9_5F00_thumb_5F00_3261A173.jpg

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

http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-79-57-metablogapi/6138.10_5F00_thumb_5F00_66A1DC87.jpg

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

http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-79-57-metablogapi/3173.11_5F00_thumb_5F00_0CD41013.jpg

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.

http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-79-57-metablogapi/2437.14_5F00_thumb_5F00_570EA4EB.jpg

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)

http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-79-57-metablogapi/2746.15_5F00_thumb_5F00_5CB998C4.jpg

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

http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-79-57-metablogapi/0777.16_5F00_thumb_5F00_76E9DC1B.jpg

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

http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-79-57-metablogapi/0602.image_5F00_thumb_5F00_58034270.png

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

http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-79-57-metablogapi/7624.18_5F00_thumb_5F00_403BE53D.jpg

http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-79-57-metablogapi/4405.19_5F00_thumb_5F00_4E4AAE6D.jpg

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.

http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-79-57-metablogapi/4186.20_5F00_thumb_5F00_7DD8A72C.jpg

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.


See Also