How to Resize VDI Virtual Disks with PowerCLI Script

How to Resize VDI Virtual Disks with PowerCLI Script

To resize virtual disk in multiple Virtual Machines, a scripted process is recommended as it will make the job quicker and manageable.

This document is based on the following technology:

  • VMware ESX 4.1.0
  • Windows 7 Enterprise 32-bit SP0 Virtual Machine

If different version is used, the manual process and scripted process might need to be adjusted as some PowerCLI functions might have been deprecated.

Tools for Scripted Process

The script has been created to help the whole process quicker and less error prone. The script is developed with VMware vSphere PowerCLI, sysinternal and DiskPart.

There are 2 major steps to resize a disk in Virtual Machine:

  1. Resize the virtual disk of the Virtual Machine
  2. Extend the Guest Operating Systems’ volume

Those 2 steps are explained in more details on the section below

Resize the virtual disk of the Virtual Machine

To resize multiple or potentially hundred or thousand Virtual Machine, doing the manual resizing can definitely lead to error, not to mention the time it is going to take to do it.
A Script has been created to do this particular task. The script is based on VMware vSphere PowerCLI and sysinternal tools. PowerCLI is used to call the vSphere Web Service API to resize the virtual disk on each Virtual Machine. Sysinternal is used to execute the diskpart utility remotely on each Virtual Machine
Script Requirements

To be able to execute the script, the following file(s) and application are required:

  1. vSphere PowerCLI installed on the machine that is going to be used to run the PowerCLI script
  2. PsExec.exe file, downloaded from
  3. A text file contains a list of the computer name per line
  4. A text file contains a list of command for DiskPart

Extend the Guest Operating Systems’ volume


PsExec.ext @C:\Temp\Computers.txt –u DOMAIN\Username –h diskpart /s \\computer\share\Diskpart.txt

list volume

Select Volume 2


vSphere PowerCLI VDI-Extend.PS1

[source language=”powershell”]

#Get the vCenter Server Name
[System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.VisualBasic’) | Out-Null
$vC = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the vCenter computer name", "Computer", "$env:computername")

#Connect to vCenter
Connect-VIServer -Server $vC

#Prompt File Function
function PromptFor-File
[String] $Type = "Open",
[String] $Title = "Select Computer File (One Computer Name per Line)",
[String] $Filename = $null,
[String[]] $FileTypes,
[switch] $RestoreDirectory,
[IO.DirectoryInfo] $InitialDirectory = $null


if ($FileTypes)
$FileTypes | % {
$filter += $_.ToUpper() + " Files|*.$_|"
$filter = $filter.TrimEnd("|")
$filter = "All Files|*.*"

switch ($Type)
$dialog = New-Object System.Windows.Forms.OpenFileDialog
$dialog.Multiselect = $false
$dialog = New-Object System.Windows.Forms.SaveFileDialog

$dialog.FileName = $Filename
$dialog.Title = $Title
$dialog.Filter = $filter
$dialog.RestoreDirectory = $RestoreDirectory
$dialog.InitialDirectory = $InitialDirectory.Fullname
$dialog.ShowHelp = $true

if ($dialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK)
return $dialog.FileName
return $null

#File Content
$file = PromptFor-File
$content = Get-Content $file

#Get Hard Disk Name
$hdd = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the VM Disk Name", "VM Disk Name", "Hard disk 1")

#Get Hard Disk Size
$hddsGb = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the Disk Size in GB", "VM Disk Size", "")
$hddsKb = [int]$hddsGb * 1024 * 1024

foreach($c in $content)
#Extend the vmdk file
Get-VM -Name $c | Get-HardDisk | Where-Object {$_.Name -eq $hdd} | Set-HardDisk -CapacityKB $hddsKb
Write-Host "This VM: " + $c + " is not recognized" -ForegroundColor Red


Leave a Reply