Increase VMware VM Disk Size with PowerShell

In this post, I will cover how to use PowerShell to increase the disk size of a Windows 10 VM running in a VMware vCenter environment. Automating simple but time consuming tasks like this can save precious time for your support staff or yourself. There are two parts to accomplishing this task; first we need to increase the disk size of the VM and then we will expand the partition inside the Windows 10 OS.


PowerShell Remoting will need to be enabled on the Windows 10 VM that you want to increase the disk on. I am not going to go into detail on how to accomplish that in this post, there are several great resources to help if the Invoke-Command gives you an error. Having admin rights on the Windows 10 VM will also be needed to modify the partition. In addition, you will need rights to run commands against the vCenter server. We also need to install VMware PowerCLI. To do so, you can install it from the PowerShell Gallery with the below command.

Install-Module VMware.PowerCLI



We will start out by declaring the variables needed to perform the automated tasks outlined in this post. In this example, I am using the GUI text box that I wrote about in a previous post. I am using the text box as one method you could use to accept a list of VM names. The $Target_Disk variable is the final size that you want the disk to be. For example, if the disk is currently 160 GB and you want to expand it by 20 GB, then you would enter 180. The vCenter server can be hard coded in a variable if that is more convenient for you. For this example I used read-host to allow for more flexibility.

### Launch GUI textbox to accept input from user
$VMs = GUI_TextBox "VM Name(s):" ### This function was introduced in previous blog post
$VM_Count = $VMs | Measure-Object | % {$_.Count}
If ($VM_Count -eq 0){ ### If nothing was inputed, the script will not continue
    Write-Host "Nothing was inputed, script is exiting..." -BackgroundColor Red -ForegroundColor White
    $Target_Disk = Read-Host "Enter the target disk size in GB (ie: 180)"
    $vCenter = Read-Host "Enter the vCenter FQDN you want to connect to"

This section of code is used as the final confirmation and to ensure the information entered was typed accurately.

Write-Host "Below are the variables that will be fed into the script:" -ForegroundColor Black -BackgroundColor Yellow
Write-Host "Number of VMs to increase disk:" $VM_Count -ForegroundColor White -BackgroundColor Blue
Write-Host "vCenter to connect to:" $vCenter -ForegroundColor White -BackgroundColor Blue
Write-Host "Target disk size in GB:" $Target_Disk -ForegroundColor White -BackgroundColor Blue
$final_confirm = (Read-Host "Do you want to Continue? (Y/N)").ToUpper()

Next we will connect to the vCenter server specified in the variable. In order to connect to the vCenter we need to provide the credentials to use. Here we are using Get-Credential to manually input the username and password we want to utilize. There are other methods to securely passing credentials, but for simplicity, I went this route.

$VIcred = Get-Credential
Write-Host "Please wait while the script connects to vCenter: $vCenter" -BackgroundColor Cyan -ForegroundColor Black
Connect-VIServer -Server $vCenter -Credential $VIcred -ErrorAction Stop -Force

Finally, we get to the section of code that resizes the VM disk and expands the OS partition for the given list of VMs. You may need to adjust the name of the VM disk for your environment. For this example I use “Hard Disk 1” which is how my VMs are configured. Also, if the drive letter you want to expand is something other than C, that will have to be modified in your code.

If ($final_confirm -eq "Y") { ### executes if the user entered y or Y on the final confiramtion question
    Foreach ($VM in $VMs){ ### going through each VM in the VMs variable
        Write-Host $VM "- Increasing disk to $Target_Disk GB" -BackgroundColor Yellow -ForegroundColor Black
            ### Resize the VM disk through VMware PowerCLI
            Get-HardDisk -Server $vCenter -VM $VM | Where-Object {$_.Name -eq "Hard Disk 1"} | Set-HardDisk -Server $vCenter -CapacityGB $Target_Disk -Confirm:$false
            ### Utilize invoke-command to expand the C partition within windows 10
            $ScriptBlock = { ### The commands that will be executed on the remote windows 10 VM
                $MaxSize = (Get-PartitionSupportedSize -DriveLetter C).Sizemax  ### This grabs the max size which should now be larger than the current partition
                Resize-Partition -DriveLetter C -Size $MaxSize ### command to expand the parition
            Invoke-Command -ComputerName $VM -ScriptBlock $ScriptBlock ### execution of the scriptblock commands on the remote windows 10 VM
        Catch [Exception]{ ### If the command inside the try statement fails the error will be outputted
            $errormessage = $_.Exception.Message
            Write-Host $errormessage -BackgroundColor Red -ForegroundColor White
        Write-Host "$VM - Process Complete" -BackgroundColor Cyan -ForegroundColor Black
Else{Write-Host "You selected Cancel or Nothing Present" -BackgroundColor Red -ForegroundColor White}

To keep things clean we disconnect from the vCenter.

Disconnect-VIServer -Server $vCenter -Confirm:$false

That’s it for now, thanks for reading!

Check out my other blog posts:

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s