
.shock - Fotolia
Boost disk management in Windows efficiency with PowerCLI
Extend a Windows hard disk in VMware vSphere with commands and functions from PowerCLI. Automate the management of hard disks with these steps focused on Windows Server 2012 R2.
Eliminate the tedious task of Windows hard disk extension by investing time upfront into disk management in Windows...
Continue Reading This Article
Enjoy this article as well as all of our content, including E-Guides, news, tips and more.
techniques that include VMware vSphere and PowerCLI automation.
Prior to virtualization, the extension of a server's hard disk would have likely been impossible with a disk that already had all of its space allocated. Virtualized storage enables IT administrators to add space to a Windows server volume in minutes -- as long as the available storage has room.
A significant challenge to disk management in Windows is that doing this task from VMware vSphere web client requires a series of tedious steps. Admins have to do Windows hard disk extension in vSphere and on the guest VMs so it appears correctly to the guest server. To make this task more automatic, admins can use PowerCLI.
This disk management in Windows method assumes that the VMs have unique disk sizes, which differentiates the disks to extend, and are running at least Windows Server 2012 R2.
Incorporate PowerCLI cmdlets into disk management in Windows
To extend a Windows hard disk in vSphere web client, go into the VM settings and manually change the value of the disk size. In isolation, this disk management in Windows task isn't time-consuming, but repeating it is tedious.
To do this in PowerCLI, use two cmdlets: Get-VM and Set-HardDisk. The first objective is to see the current size of all the hard disks in a given VM. This makes it easier to find the disk that requires extension.
This example uses Get-VM and shows that there are three Windows hard disks associated with the VM. The sizes of these hard disks are 200,100 and 80.

Path CapacityGB FreeSpaceGB
---- ---------- -----------
D:\ 200 96
E:\ 100 96
C:\ 80 56
Examine the cmdlet to see how it gets this information. First, the Get-VM cmdlet gets all of the hard disks for the given VM.
C:\> Get-VM test-vm | ForEach-Object {$_.Guest.Disks | ForEach-Object {
Next, for each disk, select three properties: CapacityGB, FreeSpaceGB and Path. This command includes a PowerShell expression and rounds the capacity and free space property decimal values.
$_ | Select-Object -Property Path,@{
Name='CapacityGB';Expression={[math]::Round($_.CapacityGB)}},@{
Name='FreeSpaceGB';Expression={[math]::Round($_.FreespaceGB)}}}}
There's one major problem with this code as it is. To increase the size of a Windows hard disk, admins must know the hard disk name. The Get-VM command doesn't provide that, so admins must use Get-HardDisk and match the capacity of the disk with the disks Get-VM retrieves.

First, use a ForEach loop to iterate through the hard disks Get-HardDisk retrieved.
ForEach ($HardDisk in $HardDisks) {
$HardDiskCapacity = [math]::Round($HardDisk.CapacityGB)
If ($GuestCapacity -like $HardDiskCapacity) {
$HardDiskLabel = $HardDisk.Name
}
}
If a match is found, it's stored in the $HardDiskLabel variable.
Next, add the property HardDiskName and use that variable along with the name of the VM.
Name='HardDiskName';Expression={$HardDiskLabel}},@{
Name='VM';Expression={$VM}
The result displays something like the image below, which now adds the hard disk property and VM name.
Path CapacityGB FreeSpaceGB HardDiskName VM
---- ---------- ----------- ------------ --
D:\ 200 97 Hard disk 2 cove
E:\ 100 97 Hard disk 3 cove
C:\ 80 56 Hard disk 1 cove
Increase the size of the virtual disk by using Set-HardDisk. The initial step is to select which Windows hard disk to resize after setting the $IncreasedSpace variable.
$IncreaseSpace = '20'
$HardDisk = Read-Host -Prompt "Specify hard disk name to resize
Retrieve the current size of that Windows hard disk with Get-HardDisk again and store it in the $Size variable.
$Size = Get-HardDisk -Name $HardDisk -VM $VM | Select-Object -ExpandProperty CapacityGB
Lastly, pipe the output of Get-HardDisk to Set-HardDisk.
$IncreaseSpace = '20'
Get-HardDisk -Name $HardDisk -VM $VM | Set-HardDisk -CapacityGB ($Size + $IncreaseSpace) -Confirm:$True
Now that the virtual disk is increased, increase the partition on the Windows OS so the VM can take advantage of the new space. On a Windows server, rescan the local disks so the space is available to extend with the diskpart command.
"rescan" | diskpart
To view all of the current Windows partitions and their minimum and maximum sizes, use the PowerShell one-liner below.

This command cycles through the existing partitions, rounds the size properties and outputs results.
Drive SizeMin SizeMax
----- ------- -------
D 103 200
C 24 80
E 3 100
To extend a particular drive, contain the supported partition size value in a variable.
$size = (Get-PartitionSupportedSize -DriveLetter C)
After, use the Resize-Partition cmdlet to extend the Windows drive.
Resize-Partition -DriveLetter C -Size $size.SizeMax -Confirm:$False
Create a reusable PowerShell function
One of the great features of PowerShell is the ability to take code and place it into a function, which makes it easily reusable for disk management in Windows and other tasks. This example uses the commands that extended a VMware VM and a Windows drive into a function called Add-VMDiskSpace.
There are only three parameters the function requires: the name of the VM ($VM), the amount of space to increase a disk by ($IncreaseSpace) and the vCenter hostname ($vCenterServer). The only other interaction with the function that is necessary is to choose which disk to extend.
First, run the command Add-VMDiskSpace, which prompts a connection to vCenter server.

Next, the function displays the current hard disks, capacities and drive letters associated with those disks.

Finally, it prompts admins to specify which Windows hard disk to extend by name -- Hard disk 2, in this example. The VM disk is resized and PowerShell performs an Invoke-Command on the Windows OS to resize the partition.

Examine the function in its entirety below.

This is a lot of code to write for a simple disk management in Windows task, but it saves time when it's necessary to increase space on virtual disks in the future.