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.

azure start stop vm

Using the –no-wait Flag

If you are stopping a large VM (like a GPU-heavy N-series instance), the CLI might “hang” while it waits for the deallocation to complete. I prefer to fire-and-forget:

Code snippet

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

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

start stop vm azure automation

The --no-wait flag returns you to the command prompt immediately, while Azure handles the shutdown in the background.

Step 4: Advanced Batch Operations

This is where you show your true authority as an Azure administrator. Let’s say you want to stop every VM in a specific subscription that is currently running, perhaps as part of an emergency cost-cutting measure.

Stopping All Running VMs

I often use this snippet in my automation scripts:

  1. Find running VMs: az vm list -d --query "[?powerState=='VM running'].name" -o tsv
  2. Stop them: Combine the list with the stop command.

Identifying VM States

Before you act, you should always verify the current status. Use the --show-details flag (or -d for short) to see the Power State:

Code snippet

az vm list -d --query "[].{Name:name, State:powerState}" --output table

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

how to automatically start and stop vm in azure
az vm get-instance-view --name AzureLessonsLinuxVM --resource-group demo --query "instanceView.statuses[1].displayStatus" -o tsv

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

how to start and stop azure vm automatically

Typical Output:

  • VM running
  • VM deallocated
  • VM starting

Step 5: Automating with Auto-Shutdown

While manual CLI commands are great, sometimes you want the platform to do the work for you. You can actually configure the Auto-Shutdown schedule via the CLI. This is a lifesaver for those of us who forget to run our scripts on Friday afternoons.

Code snippet

az vm auto-shutdown --resource-group Sales-RG --name Prospect-DB --time 1800 --timezone "Eastern Standard Time"

In the command above:

  • --time 1800 sets the shutdown for 6:00 PM.
  • --timezone ensures it aligns with your local office hours (e.g., “Pacific Standard Time” for Cali folks).

Best Practices

When managing Azure resources, there are a few “unwritten rules” I always follow to maintain professional standards:

  • Tagging: Always tag your VMs with an Owner and Environment (Dev, Prod, Staging). Before you stop a VM, your script should check if Environment == 'Prod'. Never stop a production server via an automated script!
  • Resource Groups: Organize your VMs by project or department (e.g., HR-Systems-RG). This makes batch-stopping via CLI much safer.
  • Service Principals: For automated scripts (like those running on a local server in Dallas), don’t use your personal login. Create a Service Principal with “Virtual Machine Contributor” rights.
  • Logging: Always pipe the output of your CLI commands to a log file so you have an audit trail if a server doesn’t come back up as expected.

Video Tutorial

Summary

Mastering the az vm start and az vm stop Commands are the first step toward becoming a Cloud Power User. By moving away from the Portal and into the CLI, you’re not just saving time—you’re building the foundation for scalable, automated infrastructure.

Quick Review Table

GoalCommand
Start a VMaz vm start -g [RG] -n [Name]
Stop & Deallocateaz vm stop -g [RG] -n [Name]
Check Statusaz vm list -d -o table
Force Stop (No Wait)az vm stop -g [RG] -n [Name] --no-wait

You may also like the following articles:

Azure Virtual Machine

DOWNLOAD FREE AZURE VIRTUAL MACHINE PDF

Download our free 25+ page Azure Virtual Machine guide and master cloud deployment today!