GD Bloggers

This is the blog site for Microsoft Global Delivery Communities focused in sharing the technical knowledge about devices, apps and cloud.
Follow Us On Twitter! Subscribe To Our Blog! Contact Us

Windows Server 8 – Disk Management with PowerShell 3.0

Windows Server 8 – Disk Management with PowerShell 3.0

  • Comments 5
  • Likes

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.

image

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 : 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.

1-1

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.

1

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.

2

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

3

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.

4

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.

6

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

8

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

7

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.

9

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

10

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

11

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.

14

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)

15

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

16

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

image

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

18

19

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.

20

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.

Anıl ERDURAN

Comments
  • tnx.!

  • Is it possibe to get these new capabillites on 2008 R2 SP1 by installing Windows Management Framework 3.0?

    Thank you.

  • What are the new PowerShell equivalents to ImageManagementService.CreateDynamicVirtualHardDisk?

  • I have a 3TB USB disk.  I "shrunk" the first (and only) partition by 1TB.  Now I have a 2TB drive with 1TB unallocated.  Windows 8 will not let me do anything with the unallocated space.  I wanted to create another partition but it says I can't go over 2TB!  WTF?  I can't create a new partition and I can't expand it back to 3TB..  Its like I just lost access to a third of my drive by a Windows 8 "gotcha".  Any thoughts?

  • Bob,

    This is a Windows Gotcha. You need to convert the disk from MBR to GPT. You may need to find a utility to do this.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment