Russell Smith specializes in the management and security of Microsoft-based IT systems. In addition to blogging about Windows and Active Directory for the Petri IT Knowledgebase, Russell is a Contributing Editor at CDW’s Biztech Magazine. Russell has more than 15 years of experience in IT, has written a book on Windows security, co-authored one for Microsoft’s Official Academic Course (MOAC) series and has delivered several courses for Pluralsight.

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:

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:

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

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:

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)

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

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:

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.

Related Topics:

  • Microsoft Azure

    Don't have a login but want to join the conversation? Sign up for a Petri Account