Start/Stop VM Using Azure CLI

The fastest way to gain control over your environment is through the Command Line Interface (CLI). In this article, I’m going to show you exactly how to Start and Stop Azure VMs using the Azure CLI like a pro.

Start/Stop VM Using Azure CLI

Understanding the “Stop” vs. “Deallocate” Trap

In Azure, “Stopped” does not always mean “Free.”

StateAction TriggerBilling StatusWhat Happens?
StoppedShutdown via Guest OSIncurring ChargesThe OS is off, but Azure still reserves the hardware and IP for you.
Stopped (Deallocated)Stopped via CLI/PortalNo Compute ChargesThe hardware is released. You only pay for storage (disks).

Pro Tip: Always stop your VMs via the CLI or Portal to ensure they reach the Deallocated state. If you just remote in and click “Shut Down” inside Windows or Linux, you’re still paying for that compute!

Step 1: Setting Up Your Environment

To follow along, you’ll need the Azure CLI installed on your machine.

  1. Check your version: Open your terminal and run az --version.
  2. Login: You must authenticate your session. Run az login. This will open a browser window; once you sign in with your corporate or personal account, you’re ready.
  3. Set your Subscription: If you have multiple subscriptions (e.g., Pay-As-You-Go and an Enterprise Agreement), make sure you are in the right context:az account set --subscription "Your-Subscription-Name"

Step 2: Starting an Azure VM via CLI

Starting a VM is the most straightforward operation. This is particularly useful for “warming up” environments before a team starts their workday.

Basic Start Command

To start a specific VM, you need two pieces of information: the Resource Group and the VM Name.

Code snippet

az vm start --resource-group demo --name AzureLessonsLinuxVM

After executing the command above, I received the expected output, as shown in the screenshot below.

Start Stop VM using Azure CLI

Starting Multiple VMs

If you want to start all VMs in a specific group, you don’t have to run the command 20 times. You can use a query to grab the IDs and pipe them:

  • Logic: List all VMs in the group, get their IDs, and start them.
  • Benefit: Zero manual clicking.

Step 3: Stopping and Deallocating VMs

As I mentioned earlier, our goal is usually to Stop and Deallocate to save money. The az vm stop command in the CLI actually triggers a deallocation by default unless you specify otherwise.

The Standard Stop Command

This is the command I use for my daily “End of Day” scripts for the dev team:

Code snippet

az vm stop --resource-group demo --name AzureLessonsLinuxVM

After executing the command above, I received the expected output, as shown in the screenshot below.