How to kill an unresponsive VM with PowerCLI

Sometimes a VM is stuck and can’t be shutdown/rebooted with vSphere Client.

On ESX, you could login and use command kill –9 <PID> to kill the VM. Same can be done from PowerCLI also. This functionality has been added to PowerCLI in version 4.1 Update 1.

Open PowerCLI and connect directly to the Esxi host.

# Connect-VIServer -Server <Hostname> -Credential $(Get-Credential -Credential root)

It will ask you for the root password.

image_thumb2

Fetch the ESXCLI object with the Get-ESXCLI cmdlet.

$esxcli = Get-EsxCli -Server <Hostname>

Run the following method of $esxcli to list all the running VMs and their World ID.

$esxcli.vms.vm.list() | Format-Table -Property DisplayName, WorldID 

Search the WorldID of the VM you want to kill and run the following command to kill the VM.

$esxcli.vms.vm.kill(“soft”, <WorldID>) 

If the above command reports back with true it means the VM was killed successfully.

If you get false, try replacing soft by hard and finally by force.  Use force only as a last resort.

If none of the above mentioned commands works then Esxi host reboot is the last option left.