Increase multiple VMware guest OS drive sizes with PowerShell

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.

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:

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:

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:

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.

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

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.

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

5+
avataravatar
Share
4 Comments
  1. Dayanand Zure 3 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

    0

  2. Curtis Redkey 3 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.

    1+

  3. Pablo Rattin 2 years ago

    0

  4. Ravikumar P 2 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

    0

Leave a reply

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

*

© 4sysops 2006 - 2020

CONTACT US

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

Sending

Log in with your credentials

or    

Forgot your details?

Create Account