ESXCLI and killing a stuck VM

With the release of vSphere 5.0 there have been some changes to ESXCLI to bring the fragmented commands into more of a formal and discoverable structure as part of this ESXCLI has been expanded, as a reminder ESXCLI can be used from both vCLI and also in technical support mode, a high level of the new ESXCLI 5.0 name spaces as can be seen below:


Kill a VM with ESXCLI

As you can see from above, the structure has clearly changed from VSphere 4.1, lets take a look at an example, if we wanted to kill a stuck VM which has been documented as part of KB Article 1004340 which can be found here.

We can do this by using the following ESXCLI 5.0 example:

Firstly lets list the VMs running on the host which I am connected to:


Now we have this list we can kill the VM using the following options:


So lets kill our VM1 with a soft kill:


There is no screen output and no tasks registered in my vCenter server but I can assure you the VM just died !

Kill a VM with PowerCLI

With PowerCLI version 4.1 VMware introduced a new cmdlet called Get-ESXCLI, this cmdlet allows us to replicate what can be done in ESXCLI and therefore we can do the above trick quite easily from within PowerCLI, and not for just 1 VM, what if we had 10 or 20 VMs which were stuck ?!

As I mentioned earlier, the structure of ESXCLI has changed from 4.1 to 5.0 so if you have a 4.1 infrastructure then you will need to adjust my code below to call the correct command, either that or look at this great blog post here which was pointed out to me during the recording of episode 26 of the Get-Scripting PowerShell Podcast.

If you do have 5.0 then you can use the advanced function I created below, the examples show how to kill both 1 VM or multiple VMs, it doesn’t even care which hosts these VMs are on:


PowerCLI Code

The below is the advanced function which can be used with vCenter 5.0 and ESXi 5.0 hosts:

Function Kill-VM {
			Kills a Virtual Machine.

			Kills a virtual machine at the lowest level, use when Stop-VM fails.

			The Virtual Machine to Kill.

		.PARAMETER  KillType
			The type of kill operation to attempt. There are three
    types of VM kills that can be attempted:   [soft,
    hard, force]. Users should always attempt 'soft' kills
    first, which will give the VMX process a chance to
    shutdown cleanly (like kill or kill -SIGTERM). If that
    does not work move to 'hard' kills which will shutdown
    the process immediately (like kill -9 or kill
    -SIGKILL). 'force' should be used as a last resort
    attempt to kill the VM. If all three fail then a
    reboot is required.

			PS C:\> Kill-VM -VM (Get-VM VM1) -KillType soft

			PS C:\> Get-VM VM* | Kill-VM

			PS C:\> Get-VM VM* | Kill-VM -KillType hard
	param (
		[Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
		$VM, $KillType
		if ($VM.PowerState -eq "PoweredOff") {
			Write-Host "$($VM.Name) is already Powered Off"
		} Else {
			$esxcli = Get-EsxCli -vmhost ($VM.Host)
			$WorldID = ($esxcli.vm.process.list() | Where { $_.DisplayName -eq $VM.Name}).WorldID
			if (-not $KillType) {
				$KillType = "soft"
			$result = $esxcli.vm.process.kill($KillType, $WorldID)
			if ($result -eq "true"){
				Write-Host "$($VM.Name) killed via a $KillType kill"
			} Else {

6 thoughts on “ESXCLI and killing a stuck VM

  1. Pingback: How to stop non-responsive VM with PowerCLI - Notes of a scripter

  2. Maara

    Hi Alan,
    I have powergui installed but i didn’t really know how to execute it, my vcenter is running on vsphere 5.5, could you please let me know how ?

    Many Thanks in Advance

  3. Phil

    This script just saved me a ton of time and worked perfectly to kill a VM that was hung. Another great script that saved my butt.

    Thanks Alan.

  4. Pingback: vSphere5: Zombie VM mit PowerCLI abschiessen | ElasticSky

  5. Manish Patel

    Please provide a feedback to the Kb I have written 1004340 to kill a stuck vm with both methods and if you permit I can do the same internally. Let me know thro email if that is OK with you.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.