How to Force an Azure VM Shutdown

In this easy Ask the Admin, I’ll show you how to force an Azure virtual machine to shut down.

Azure VMs are generally reliable, but there can be occasions when they get stuck during the start process and cannot be further controlled using the web-based management portal. In this article, I’ll show you how to use PowerShell to resolve this problem so that you can have another, hopefully successful attempt, at starting your virtual machine.

A Common Reason to Force an Azure VM to Shut Down

There are different reasons why you might want to force an Azure VM to shut down, but one of the most common is if a VM hangs when starting. Before you run the commands below, you will need to have Microsoft Azure PowerShell installed on your management PC, and make a secure connection to your Azure subscription. For more information on managing Azure using PowerShell, see Setup Windows Azure PowerShell Management on the Petri IT Knowledgebase.

Once you’ve got Microsoft Azure PowerShell installed, and a publish settings file downloaded and imported for your Azure subscription, open a PowerShell prompt and select the Azure subscription that you want to use in the current session:

​Select-AzureSubscription –SubscriptionName Pay-As-You-Go

If you’re not sure what Azure subscriptions have been installed on the local management PC, run the command below and look for the SubscriptionName field in the results:

Get-AzureSubscription

You can confirm the subscription set in the current PowerShell session using Get-AzureSubscription:

​Get-AzureSubscription –Current

Stop an Azure Virtual Machine

Now that PowerShell is configured and the Azure subscription set in the current session, let’s look at forcing a shutdown. Using the Stop-AzureVM cmdlet as shown below, specify the VM’s name, contososrv1 in this example, and the related cloud service, which is also contososrv1:

​Stop-AzureVM –Name contososrv1 –ServiceName contososrv1 -Force

If you’re not sure of the cloud service name, use Get-AzureVM to list all the VMs in the subscription and return the cloud service names. You can pipe the results of the Get-AzureVM cmdlet to Where-Object, which will return information just about a specific virtual machine if you have a lot of VMs in the subscription:

Forcing an Azure VM to shut down using PowerShell (Image Credit: Russell Smith)
Forcing an Azure VM to shut down using PowerShell (Image Credit: Russell Smith)
​Get-AzureVM | where { $_.Name –eq 'contososrv1' }

Alternatively, you can pipe the results of the above command line straight to the Stop-AzureVM cmdlet, without manually typing the cloud service name:

​Get-AzureVM | where { $_.Name –eq 'contososrv1' } | Stop-AzureVM -Force

You’ll notice in the figure that the VM’s status is ReadyRole, which means that it has successfully booted. If your VM is hung during startup, the status might be RoleStateUnknown.

Once the VM has actually shut down the status will be StopDeallocated, at which point you can either return to the web-based management portal and attempt to start it again, or use PowerShell as shown here:

​Get-AzureVM | where { $_.Name –eq 'contososrv1' } | Start-AzureVM

For more information on managing Azure VMs using PowerShell, see Stop or Start VMs in an Windows Azure Subscription Using PowerShell Loop and Stop Microsoft Azure Virtual Machines in Parallel using PowerShell on the Petri IT Knowledgebase.