Mass Reconfigure of Virtual Machine vCPU Sockets/Cores using VMware PowerCLI


Increasing the amount of vCPUs in ESXi is a simple admin task when working with a single or low number of virtual machines. However, performing this on a large number of virtual machines can be an intensive task.

This article is about my use case and how I resolved this with a script, which can be found at the bottom.

Use Case

In my case, a customer was expanding their VDI cluster and increasing virtual machine vCPU capacity from 2 to 4. Besides of this, the configuration required a 2 socket – 2 core (4 vCPU) setup.

The script I wrote for this, assumes the virtual machines you want to modify are powered off and modifies all virtual machines on the selected ESXi host. Of course you can modify the parameters to have a selection based on virtual machine name or to exclude specific virtual machines.

The script logs any actions to the $LogFile file, for historical/troubleshooting references.

Make sure you test the script on a non-production environment first. I am not responsible for any negative outcomes by using this script.


#Import PowerCLI commandlets
Import-Module VMware.PowerCLI

#Parameters - Customizable
$vCenterServer = "" #This is the vCenter Server instance you want to work with
$ESXHost = "" #Use the ESXi host name as shown in vSphere Web Client
$LogFile = "D:TempBulkSetVcpu.log"

Stop-Transcript | Out-Null

$ErrorActionPreference = "Continue"
Start-Transcript -path $LogFile -append

#Make sure no existing vCenter Server connections are active
Write-Host "Disconnecting any existing vCenter Server connections..."
Disconnect-VIServer * -Force -Confirm:$false

#Connect to vCenter Server
Write-Host "Connecting to $vCenterServer..."
Connect-VIServer $vCenterServer

#Get current VM configuration for logging purposes
Write-Host "Getting current VM configuration"
Get-VMhost $ESXHost | Get-VM | FT

#Get Power-On VMs (as they will not be able to change vCPU configuration while running)
Write-Host "These VMs are currently not powered off and will not be reconfigured"
Get-VMhost $ESXHost | Get-VM | Where {$_.PowerState -notmatch "PoweredOff"} | FT

#Setting new vCPU settings for powered off VMs
Write-Host "Reconfiguring powered off VMs with 4 vCPUs"
$VMSelection = Get-VMhost $ESXHost | Where {$_.PowerState -match "PoweredOff"}

#Start configuring Permissions
Write-Host "Configuring new vCPUs..."
ForEach ($VM in $VMSelection)


                $VMSpec = New-Object -Type VMware.Vim.VirtualMAchineConfigSpec -Property @{"NumCoresPerSocket" = 2}
                Get-VM $VM | Set-VM -NumCPU 4 -Confirm:$false


#Disconnect from vCenter Server
Write-Host "Disconnecting from $vCenterServer..."
Disconnect-VIServer * -Force -Confirm:$false

#Stop Logging


3 thoughts on “Mass Reconfigure of Virtual Machine vCPU Sockets/Cores using VMware PowerCLI

  1. Pingback: Mass Reconfigure of Virtual Machine vCPU Sockets/Cores using VMware PowerCLI — SnowVM | VirtualRol

  2. Since this use of VirtualMAchineConfigSpec pops up in a lot of search results, I just wanted to add that nowadays Set-VM has gained a simple -CoresPerSocket parameter.

    As an example, this one-liner now works:

    Get-VM -Name xxx | Set-VM -NumCpu 2 -CoresPerSocket 2


Leave a Reply

Fill in your details below or click an icon to log in: Logo

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

Facebook photo

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

Connecting to %s