Azure CLI Examples For Beginners

The Azure Command-Line Interface (CLI) is an essential tool for any cloud professional. Mastering these commands will significantly reduce your manual overhead. In this guide, I will walk you through the most important Azure CLI examples for beginners. Once you learn the “az” syntax, you’ll never want to go back to clicking through menus.

Azure CLI Examples For Beginners

What is Azure CLI and Why Should You Use It?

The Azure CLI is a cross-platform command-line tool used to create and manage Azure resources. It runs on Windows, macOS, and Linux, and even inside your browser via the Azure Cloud Shell.

Why I Prefer CLI Over the Portal:

  • Speed: Executing a command is often faster than waiting for a UI to load.
  • Automation: You can easily wrap these commands into scripts (Bash or PowerShell).
  • Consistency: Scripts ensure that you deploy the exact same environment every time.
  • Version Control: You can save your infrastructure logic in GitHub or GitLab.

Getting Started: Installation and Authentication

Before we dive into the examples, you need to have the environment ready. I always recommend checking your version first to ensure you are on the latest release.

1. Check Version

To see which version you are running, use:

az --version

2. Logging In

To connect your local terminal to your Azure account, run:

az login

Check out Az login –identity

Note: This will open a browser window asking you to authenticate with your Microsoft credentials.

3. Account Management

If you have multiple subscriptions (e.g., one for “Dev” and one for “Production”), you need to make sure you’re working in the right one.

  • List all subscriptions:
az account list --output table
  • Set active subscription:
az account set --subscription "My-Subscription-Name"

Core Resource Management: The Building Blocks

In Azure, everything starts with a Resource Group (RG). Think of an RG as a logical container for your resources—like a folder on your computer.

Creating a Resource Group

az group create --name MyResourceGroup --location eastus

Common Resource Group Commands

ActionCommand
List all groupsaz group list --output table
Show specific groupaz group show --name MyResourceGroup
Delete a groupaz group delete --name MyResourceGroup --yes --no-wait

Pro Tip: Use the --no-wait flag if you don’t want to wait for the deletion to finish. It sends the command to Azure and frees up your terminal immediately.

Working with Azure Virtual Machines (VMs)

Managing VMs via the CLI is incredibly powerful. You can spin up a Linux or Windows server in seconds without navigating through twenty different configuration screens.

1. Create a Linux VM

Here is a standard example to create a basic Ubuntu server:

az vm create --resource-group MyResourceGroup --name MyUbuntuVM --image Ubuntu2204 --admin-username azureuser --generate-ssh-keys

2. Managing VM Power States

If you’re looking to save costs, you should stop your VMs when they aren’t in use.

  • Start VM:
az vm start --resource-group MyResourceGroup --name MyUbuntuVM
  • Stop VM:
az vm stop --resource-group MyResourceGroup --name MyUbuntuVM
  • Deallocate VM: (This is the one that actually stops the billing!)
az vm deallocate --resource-group MyResourceGroup --name MyUbuntuVM

Check out Azure VM Stopped Deallocated

3. List and Monitor VMs

To see all VMs in your subscription and their current status:

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

Storage Account Examples

Storage is the backbone of almost every Azure solution. Whether you’re hosting a static website or storing logs, you’ll need to know these commands.

1. Create a Storage Account

Storage names must be globally unique. I usually append a random string or date to ensure there are no conflicts.

az storage account create --name mystorageacct2026 --resource-group MyResourceGroup --location eastus --sku Standard_LRS

Check out Create a storage account in Azure

2. Create a Blob Container

Once the account is ready, you need a container to hold your files.

az storage container create --name images --account-name mystorageacct2026

3. Uploading Files

To upload a local document (e.g., report.pdf) to your new container:

az storage blob upload --account-name mystorageacct2026 --container-name images --name my-report --file report.pdf

Networking Basics

Networking can be complex, but the CLI simplifies the creation of virtual networks (VNets) and security rules.

  • Create a VNet:
az network vnet create --resource-group MyResourceGroup --name MyVNet --address-prefix 10.0.0.0/16 --subnet-name MySubnet --subnet-prefix 10.0.1.0/24
  • Open a Port (NSG Rule):If you have a web server on a VM and need to open port 80 for public traffic:
az network nsg rule create --resource-group MyResourceGroup --nsg-name MyVMNSG --name AllowHTTP --priority 100 --destination-port-ranges 80 --access Allow --protocol Tcp

Advanced Tip: Using Queries and Output Formats

One of the most powerful features of the Azure CLI is the --query parameter. It uses JMESPath syntax to filter the JSON data that Azure returns.

Examples of Output Control:

  • Table View: Best for humans to read. (--output table)
  • JSON View: Best for scripts. (--output json)
  • TSV View: Best for passing values to other commands. (--output tsv)

Filter Example:

If I only want the IP address of a specific VM:

az vm list-ip-addresses --resource-group MyResourceGroup --name MyUbuntuVM --query "[0].virtualMachine.network.publicIpAddresses[0].ipAddress" --output tsv

Best Practices for Beginners

  1. Use Variables: Instead of typing MyResourceGroup twenty times, save it to a variable: RG="MyResourceGroup".
  2. Tag Everything: Always add tags to your resources for billing purposes. (--tags Environment=Dev Project=Alpha)
  3. The Help Command: If you get stuck, simply append --help to any command. For example: az vm create --help.
  4. Interactive Mode: If you’re just learning, try az interactive. It provides auto-completion and descriptions as you type.

Conclusion

The Azure CLI is more than just a tool; it’s a productivity multiplier. By mastering these basic examples—from resource groups to VMs and storage—you are setting yourself up for success in the cloud.

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!