Resize Guest System Partition with PowerCLI

I recently needed to resize system partitions on several Windows 2008R2 virtual machines. To do this with Set-HardDisk the virtual machines must be powered off and you need a helper VM. I was looking for a way to do this without downtime, as that can be arranged when executing the steps manually (grow the disk, log into the guest, rescan disks and then extend the partition). I came up with the following workaround and thought it would be worth sharing. The idea is to set the hard disk to the new size with Set-HardDisk and then use Invoke-VMScript to run diskpart from within the VM. I included the -ResizeGuestPartition switch on Set-HardDisk as that appears to complete the task of re-scanning for disks within disk manager.

[cc lang=”powershell”]

Get-HardDisk -vm “newDiskTest” |
?{$ -eq “hard disk 1”} |
Set-HardDisk -CapacityKB ($newSizeGB*1MB) -ResizeGuestPartition -GuestUser $guestUser -GuestPassword $guestPass -confirm:$false -ErrorAction:SilentlyContinue

Invoke-VMScript -vm $guestName -ScriptText “echo select vol c > c:\diskpart.txt && echo extend >> c:\diskpart.txt && diskpart.exe /s c:\diskpart.txt” -GuestUser $guestUser -GuestPassword $guestPass -ScriptType BAT

This entry was posted in Scripting, Virtualization. Bookmark the permalink.

4 Responses to Resize Guest System Partition with PowerCLI

  1. Adam Stahl says:

    I had to do this very same thing a few days ago against 52 VMs. I came up with something very similar and just wanted to share it. My method works without storing username/password in the script. It’s not pure powershell, but it worked nicely for me.


    connect-viserver $vCenter

    foreach ($vm in $vms) {
    get-vm $vm | get-harddisk | where {$ -eq “Hard Disk 1”} | set-harddisk -confirm:$false -capacityGB $NewSizeGB
    copy diskpart.txt \\$vm\c$\windows\temp\
    psexec \\$vm diskpart /s C:\windows\temp\diskpart.txt

    disconnect-viserver -Confirm:$false

    select volume 2

  2. @Adam, thanks for your comment. I didn’t know about the ‘rescan’ command inside diskpart — I’ve actually made use of it a couple times since I saw your post. Thanks for the tip!

    Just for reference, the Invoke-VMScript cmdlet does not require the username and password to be stored in the script. By default the cmdlet works kind of like psexec does in your example, where the current running user credentials are used. In my scenario the user running the script didn’t have admin rights to the VMs that were being extend and alternate credentials were needed.

  3. daisy-love says:

    Try partition software is much better than disk management.

    Resizing system partition requires careful operation for wrong action might cause system crash.

    You’d better first back up your system and then resize system partition with partition softare

  4. George G. says:

    Hi guys,
    I’ve been working with this script lately and works just fine.
    Problem is, that in a very diverse VM landscape, there are many different HDD capacity configs (e.g. some come with 40, 60, 80, 200 GB etc) and therefore I have to manually check each VMs config to decide how much storage I’m going to assign.
    I was thinking if it were possible to add a percentage handle (e.g. -CapacityGB 10% ) instead of a fixed value.
    That would simplify the capacity-assignment process tremendously!
    Any ideas? 🙂

Leave a Reply

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


Notify me of followup comments via e-mail. You can also subscribe without commenting.