Skip to content

VirtuallyThatGuy

Anything VMware , PowerCLI, PowerShell, Automation and some Windows

Menu
  • Home
  • PowerCLI
  • VMware
  • Automation
  • Windows
  • About
Menu

How to Expand VM Disk Using PowerCLI and Extend in Windows Guest – VirtuallyThatGuy

Posted on 25 April 20186 December 2022 by VirtuallyThatGuy

Imagine having to expand disk for more than 20 VMs and you sadly opt to do it the click next and next and next and click next way? Well you know where I am going with this. I must admit I use to do it this way until a colleague wowed me with a script which copies files to windows temp directory and auto expand from disk mgmt. without having to login to the box… this served me well during my time at my at an Online Betting Company. I picked the one liner from his by expanding it and below is what I came up with….

Expand VM Hard Disk 

 

#download pstools from https://docs.microsoft.com/en-us/sysinternals/downloads/psexec  open zip and copy PsExec.exe & PsExec64 to scripts folder

#create diskpart.txt in scripts folder and include below

 

rescan

select volume 2 

extend

 

# Run PowerCLI as domain admin account or enough privileges

# Path into script folder and save below as PS.1

 

$vCenter=”uk3p-vc01.lab.local”

$NewSizeGB=60

$vms=("Ron-Test01")

connect-viserver​​ $vCenter

foreach​​ ($vm​​ in​​ $vms) {

get-vm​​ $vm​​ |​​ get-harddisk​​ |​​ where​​ {$_.name​​ -eq​​ “Hard Disk 1”}​​ |​​ set-harddisk​​ -confirm:$false​​ -capacityGB​​ $NewSizeGB

copy​​ diskpart.txt​​ \\$vm\c$\windows\temp\

.\PsExec.exe​​ \\$vm​​ diskpart​​ /s​​ C:\windows\temp\diskpart.txt

}

disconnect-viserver​​ -Confirm:$false

 

diskpart.txt: # create diskpart.txt in scripts folder and​​ include below. This gets copied using the account specified above to temp folder of the server you’re expanding.

 

#you can use a csv if you’re exanpanding more than one VM by calling below

$VMlist​​ =​​ import-csv​​ –path​​ c:\scripts\expandVMS.csv

get-vm​​ $VMlist.Name​​ |​​ get-harddisk​​ |​​ where​​ .....​​ 

 

 

Imagine having to expand​​ the​​ disk for over 20VMs using the manual​​ approach. Clicking​​ next and next and next​​ and next? Well be my guest as per below….

Using the WebClient

  • Open VMware Infrastructure (VI) Client and connect to VirtualCenter or the ESX host.

  • Right-click the virtual machine.

  • Click Edit Settings.

  • Select Virtual Disk.

  • Increase the size of the disk.

    Note: If this option is greyed out, the disk may be running on snapshots or the disk may be at the maximum allowed size depending on the block size of the datastore.

  • Follow the steps in Increasing the size of a disk partition (1004071) so the guest operating system is aware of the change in disk size.

Below One Liner Can be used instead of the diskpart

Get-HardDisk -vm $vms | Where {$_.Name -eq "Hard disk 1"} | Set-HardDisk -CapacityGB 60 -ResizeGuestPartition -Confirm:$false

# Run PowerCLI as domain admin account or enough privileges
# Path into script folder and save below as PS.1

$vCenter=”uk3p-vc01.lab.local”
$NewSizeGB=60
$vms=("Ron-Test01")
connect-viserver $vCenter
foreach ($vm in $vms) {
get-vm $vm | get-harddisk | where {$_.name -eq “Hard Disk 1”} | set-harddisk -confirm:$false -capacityGB $NewSizeGB
copy diskpart.txt \\$vm\c$\windows\temp\
.\PsExec.exe \\$vm diskpart /s C:\windows\temp\diskpart.txt
}
disconnect-viserver -Confirm:$false

diskpart.txt: # create diskpart.txt in scripts folder and include below. This gets copied using the account specified above to temp folder of the server you’re expanding.

#you can use a csv if you’re exanpanding more than one VM by calling below
$VMlist = import-csv –path c:\scripts\expandVMS.csv
get-vm $VMlist.Name | get-harddisk | where ..... 

7 thoughts on “How to Expand VM Disk Using PowerCLI and Extend in Windows Guest – VirtuallyThatGuy”

  1. senthil kumar says:
    26 November 2020 at 05:29

    I have tried this script but disk expand only working in Vmware end not in server disk management. it shows disk unallocated space.

    Reply
  2. Jeremy says:
    15 January 2021 at 13:06

    Powershell has a commandlet called Invoke-VmScript you can use to do this without having to use 3rd party pstools.

    I love pstools and all, but psexec frequently fails if you don’t have SMB/CIFs to the box over the network, or if there are any RPC issues.

    The only requirement for Invoke-VmScript is that Vmware tools be installed and functional on the VM Guest.

    Reply
    1. VirtuallyThatGuy says:
      15 January 2021 at 18:52

      Thanks Jeremy for contributing to the community.

      Reply
    2. senthil kumar says:
      27 February 2021 at 04:05

      However I tried to modify the script but still its not working. it will working in Vmware end not in server disk management. it shows disk unallocated space. can you please share the exact script code please

      Reply
  3. VirtuallyThatGuy says:
    3 November 2021 at 19:54

    Get-HardDisk -vm $vms | Where {$_.Name -eq “Hard disk 1”} | Set-HardDisk -CapacityGB 38 -ResizeGuestPartition -Confirm:$false

    This can be used to extend the OS diskpart as well if needed

    Reply
    1. Sunil Kumar says:
      7 February 2022 at 12:06

      This is not supported on Windows 2019 servers

      Reply
      1. VirtuallyThatGuy says:
        9 February 2022 at 10:57

        I am yet to test it in 2019 Server. I will do and update where needed.

        Reply

Leave a Reply Cancel reply

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

When autocomplete results are available use up and down arrows to review and enter to go to the desired page. Touch device users, explore by touch or with swipe gestures.

Recent Posts

  • vROps: Management Pack Troubleshooting
  • Windows AD {Active Directory} (PowerShell) samples
  • Migrate VMs Between vCentres Using Powershell or PowerCLI
  • Set VM Tools to Update Automatically on VM Reboot using powershell
  • Windows Administrator Must Have Powershell Commands

Recent Comments

  • JB on Script: How to get VM with Tag Assignment and export results to csv using PowerCLI or Powershell
  • DL on How to change VCSA root password and bypass BAD PASSWORD: it is based on a dictionary word for vCenter VCSA root account warning
  • 360coolp on How to change VCSA root password and bypass BAD PASSWORD: it is based on a dictionary word for vCenter VCSA root account warning
  • Yogesh on ESXi 8.x, 7.x, 6.x Service sfcbd-watchdog Not Running / Fails to Start – VirtuallyThatGuy
  • VirtuallyThatGuy on ESXi 8.x, 7.x, 6.x Service sfcbd-watchdog Not Running / Fails to Start – VirtuallyThatGuy

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017

Categories

  • Automation
  • PowerCLI
  • VMware
  • Windows
© 2025 VirtuallyThatGuy | Powered by Superbs Personal Blog theme