How-to start and stop Azure VMs via PowerShell

With PowerShell it is fast and convenient to manage my development and test servers running on Windows Azure. It is just easier to use command line tools than logging into the Azure management portal shutting down each VM. To set up PowerShell:

  1. Install the Azure PowerShell cmdlets
  2. Start the Azure PowerShell (do not start the regular PowerShell as it is not preconfigured with the Azure PowerShell cmdlets)
  3. Authorize Azure PowerShell to access your Azure subscriptions by typing in the Azure PowerShell shell:
    Add-AzureAccount
    

    In the sign-in window, provide your Microsoft credentials for the Azure account.

If you like me have multiple Azure subscriptions – change the default subscription with:

Select-AzureSubscription [-SubscriptionName]

To start an Azure VM the syntax is:

Start-AzureVM [–Name] [-ServiceName]

To start a VM named vs2015 in the cloud service lybCloudService requires as little as:

Start-AzureVM vs2015 lybCloudService

To stop the VM is just as easy

Stop-AzureVM [-Name] [-ServiceName]

If it is the last running VM in the cloud service, then you will be asked if you want to deallocate the cloud service or not, as the cloud service will release the public IP address. That is not a problem if you access your VM via DNS name – which most people do.
You can override the question by appending –Force like this:

Stop-AzureVM vs2015 lybCloudService –Force

There are many useful Azure PowerShell cmdlets to use. To list all Azure PowerShell cmdlets:

Help Azure

Get details on Azure PowerShell cmdlet:

Man <cmdlet name>

List all VMs:

Get-AzureVM

Get details of a specific VM:

Get-AzureVM [–Name] [-ServiceName]

The PowerShell prompt is just like a normal command prompt, so you can use tab completion and F7 to show all executed commands.

  • Pingback: How-to start and stop Azure VMs via PowerShell - Lybecker - Site Home - MSDN Blogs()

  • Mark Smith

    Hi Anders,

    Nice article, just worth noting that we have developed some software that does all this without powershell.

    You can also use the Azure Virtual Machine scheduler from SmiKar Software. AVMS as it is known will connect to your Azure Subscriptions and allow you to select the VMs and a power on or down schedule to suit.

    Works with V1 and V2 Azure Virtual Machines

    http://www.smikar.com/automate-scheduled-power-azure-vms/

  • Cristiam Felipe Moreno

    Nice work. Just a contribution. If you have many virtual machines that belong to the same resource / service, and you need to start / stop them, simply start / stop-AzureVM -ServiceName *
    The * will take all the resources that he has under that service