Recently, I had to increase the size of multiple Windows virtual drives running on VMware. In this post, I share the PowerShell script I used.
Alex Chaika

My task was to increase each virtual machine guest OS drive size by 20 GB in a VMware vSphere environment and then extend the partition inside Windows. First, I created a CSV file that contained a list of my VMs:

CSV file that lists my VMs

CSV file that lists my VMs

The first version of my script for extending VM virtual drives looked like this:

Import-Csv -Path 'C:\Temp\testvms.csv' | %{
   $VMx = $_.Name
   $vm = get-vm |?{$_.Name -eq $VMx}
   $HD = get-harddisk -VM $vm.Name -Name "Hard disk 1"
   $NewCap = [decimal]::round($HD.CapacityGB + 20)
   $HD | Set-harddisk -CapacityGB  $NewCap
}

As you can see, the script is very simple. I import the list of the VMs from the testvms.csv file and go through them using a ForEach-Object loop (shortcut %).

With the Get-VM cmdlet, I get all the VMs that match the names in my CSV file ("?" is a shortcut for where). Then I store hard disk object data in the $HD variable. In the next line, I read the HD.CapacityGB property and save the extended size to the $NewCap variable. Finally, I use the Set-Harddisk cmdlet to apply a new capacity to the VM hard disk.

The next part is a bit trickier, because we need to extend each partition inside the OS. Note that the method I used will work only for Windows Vista/Server 2008 or later. Earlier Windows versions didn’t support OS drive extension on the fly.

First, I have to get the disk and partition numbers on one of the VMs I’m working with. Just note again that I cloned all of my VMs from the same template, so I’m pretty sure all of them have the same configuration. At a command prompt, I run diskpart to check the OS disk and partition numbers:

Getting the disk and partition numbers with diskpart

Getting the disk and partition numbers with diskpart

As you can see, I have four disks, but I know that Windows is installed on the 40 GB disk, so the number I need is "0."

Now I need a partition number, so I select the disk number to list the disk's partitions:

select disk 0

Getting the partition numbers

Getting the partition numbers

I have two partitions on this drive, one with 100 MB and one with 39 GB. The first is the standard Windows recovery partition and the second one contains my Windows installation. Thus, the partition number I need is "2."

Why do I need these numbers? Diskpart doesn't offer an option to run the above commands from a text file. Hence, I have to hardcode the number in my diskpart.txt:

rescan
select disk 0
select partition 2
extend

I previously did almost the same thing manually, though I’m using the select option instead of a list. I’m also using rescan to make sure the OS is aware of the changes I applied to the virtual HDD.

Subscribe to 4sysops newsletter!

Next, I just need to put together the script that will copy my diskpart.txt to the VMs and then execute it:

Import-Csv -Path 'C:\Temp\testvms.csv' | %{
	$vmName = $_.Name
	$Dest = "\\" + $vmName + "\"+ "C$\Windows\temp"
	Copy-Item "C:\temp\diskpart.txt" -Destination $Dest -Force
	Invoke-VMScript -vm $vmName -ScriptText "C:\windows\system32\diskpart.exe /s c:\Windows\temp\diskpart.txt" -ScriptType BAT
}

As you can see, this script is very simple as well. I’m reading the same CSV file with my VM names and then looping through the file. Then I copy the file with the diskpart instructions to the target VM using Copy-Item and run it using the Invoke-VMScript cmdlet.

avataravatar

4 Comments
  1. Avatar
    Dayanand Zure 6 years ago

    i have 100 MV where i want to expand the disk, is dere any way where we can export the failed VM where we are not able to extend the disk becase any unknown reason

  2. Avatar
    Curtis Redkey 6 years ago

    Outstanding script! Exactly what I needed.

    Is there a reason why a SCSI controller 3 Virtual Device Node SCSI (3:X) Hard disk XX would not match? I have some servers with four SCSI controllers and only the first three SCSI controllers drives show up in the output. There are 17 drives on many servers and this helps my team determine quickly which one needs to be extended.

  3. Avatar
    Pablo Rattin 6 years ago
    Great Job! Do you know something script for do same but with replicación amable?

  4. Avatar
    Ravikumar P 6 years ago

    Hello Alex, Indeed nice post. Can we have any similar script for multiple Hyper-v guests??. Please suggest me the link or script or an article. Thank you.

    /Ravikumar. P

Leave a reply

Please enclose code in pre tags: <pre></pre>

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

*

© 4sysops 2006 - 2024

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending
WindowsUpdatePreventer

Log in with your credentials

or    

Forgot your details?

Create Account