Adding CPU'sSometimes you need to add CPUs to your virtual machines. Adding CPU’s is easy on a virtual machine. Of course you can do this from the vSphere Client, where you can select the number of CPUs and the number of cores per CPU. If you only need to change one virtual machine this isn’t a problem. When you have to change 50 virtual machines it gets somewhat tiresome.

Luckily we can use PowerCLI for that. With the cmdlet Set-VM you can change all kinds of parameters of the virtual machine. The command below gets a VM object and changes the number of CPUs.

get-VM -name MyVM | set-VM -NumCpu 2

sets the number of CPU’s for this VM to two. It asks you if you’re sure.

Proceed to configure the following parameters of the virtual machine with name 'dc01'?
New NumCpu: 2
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):

This way you add 2 CPU’s (sockets) to the virtual machine with each one core. If you want to add CPU’s with multiple cores you need to do something extra.

$VM=Get-VM -Name MyVM
$VMSpec=New-Object –Type VMware.Vim.VirtualMAchineConfigSpec –Property @{“NumCoresPerSocket” = 2}
$VM | Set-VM -NumCPU 2

Make sure that NumCPU is the total number of cores you want. If you want 2 CPU’s with 2 cores you set NumCPU to 4.

Removing the CPU’s

Of course you can also remove the CPU’s again from the virtual machine. You just set the number to a lower quantity. If you need to reduce the core count, use the $VMSpec part above with NumCoresPerSocket set to 1.

get-VM -name MyVM | set-VM -NumCpu 1