Overview
This tutorial walks you through everything you need to know to use the Azure Command-Line Interface (Azure CLI) effectively — from installation to managing real Azure resources. Whether you're preparing for the AZ-104 Azure Administrator exam or working in a production environment, the Azure CLI is a foundational tool you'll use daily.
By the end of this guide you will be able to install and configure the Azure CLI, authenticate to Azure, manage resource groups and virtual machines, and write reusable CLI scripts.
Prerequisites
- An active Azure subscription (free tier works)
- Basic familiarity with a terminal (bash or PowerShell)
- Administrator or sudo access on your local machine
Why This Matters for AZ-104
The AZ-104 exam tests your ability to manage Azure resources using both the portal and the CLI. Many scenario-based questions assume you know equivalent CLI commands for tasks like creating VMs, managing storage accounts, and configuring networking. Mastering the CLI also makes you faster in real deployments.
Getting Started
Installing the Azure CLI
The Azure CLI is cross-platform and runs on Linux, macOS, and Windows. Choose the method that matches your environment.
Ubuntu / Debian:
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bashmacOS (Homebrew):
brew update && brew install azure-cliWindows (winget):
winget install -e --id Microsoft.AzureCLIAfter installation, verify the version:
az --versionYou should see output listing the CLI version, Python version, and installed extensions. If the command is not found, restart your terminal and try again.
Authenticating to Azure
The most common authentication method for interactive use is device code flow or browser-based login:
az loginThis opens a browser window where you sign in with your Azure credentials. For headless or automation environments, use a service principal:
az login --service-principal \
--username $APP_ID \
--password $CLIENT_SECRET \
--tenant $TENANT_IDAfter login, list your available subscriptions:
az account list --output tableSet the active subscription you want to work with:
az account set --subscription "My Production Subscription"Core Resource Management Commands
Every Azure resource lives inside a resource group. Understanding how to manage resource groups is critical for both the exam and day-to-day administration.
Working with Resource Groups
Create a resource group in the East US region:
az group create \
--name rg-cloudninjas-demo \
--location eastusList all resource groups in your subscription:
az group list --output tableShow details of a specific resource group:
az group show --name rg-cloudninjas-demoDelete a resource group and all its contents (use with caution):
az group delete --name rg-cloudninjas-demo --yes --no-waitCommon Pitfall: The --no-wait flag returns control to your terminal immediately but deletion continues in the background. Do not recreate resources in the same group until deletion completes. Verify with az group show — it will return an error once the group is gone.
Deploying and Managing Virtual Machines
Creating a VM is one of the most common AZ-104 tasks. Here is a complete working example:
az vm create \
--resource-group rg-cloudninjas-demo \
--name vm-webserver-01 \
--image Ubuntu2204 \
--size Standard_B2s \
--admin-username azureuser \
--generate-ssh-keys \
--location eastusThe --generate-ssh-keys flag automatically creates an SSH key pair in ~/.ssh/ if one does not already exist. The public key is installed on the VM.
Open port 80 for web traffic after the VM is created:
az vm open-port \
--resource-group rg-cloudninjas-demo \
--name vm-webserver-01 \
--port 80Get the public IP address of your VM:
az vm show \
--resource-group rg-cloudninjas-demo \
--name vm-webserver-01 \
--show-details \
--query publicIps \
--output tsvStop (deallocate) a VM to stop billing for compute:
az vm deallocate \
--resource-group rg-cloudninjas-demo \
--name vm-webserver-01Advanced CLI Techniques and Best Practices
Using JMESPath Queries
The --query flag uses JMESPath syntax to filter JSON output. This is extremely useful in scripts where you need to extract specific values.
List only the names and locations of all VMs in a resource group:
az vm list \
--resource-group rg-cloudninjas-demo \
--query "[].{Name:name, Location:location, Size:hardwareProfile.vmSize}" \
--output tableExtract the storage account key for use in a script:
STORAGE_KEY=$(az storage account keys list \
--resource-group rg-cloudninjas-demo \
--account-name mystorageaccount001 \
--query "[0].value" \
--output tsv)
echo "Key retrieved: ${STORAGE_KEY:0:10}..."Configuring CLI Defaults
Set default values so you do not have to repeat flags in every command:
az configure --defaults group=rg-cloudninjas-demo location=eastusNow commands like az vm list will automatically use the default resource group. Clear defaults when switching projects:
az configure --defaults group='' location=''Output Formats
The Azure CLI supports multiple output formats. Choose the right one for your use case:
- json — default, full detail, ideal for scripting
- table — human-readable columns, great for quick review
- tsv — tab-separated, ideal for variable assignment in bash
- yaml — readable structured output
- none — suppresses output, useful in CI/CD pipelines
# Human-readable table output
az account list --output table
# Assign a value to a variable using tsv
SUBSCRIPTION_ID=$(az account show --query id --output tsv)
echo $SUBSCRIPTION_IDWorking with Storage Accounts
Create a storage account with locally redundant storage:
az storage account create \
--name mystorageaccount001 \
--resource-group rg-cloudninjas-demo \
--location eastus \
--sku Standard_LRS \
--kind StorageV2Create a blob container inside the storage account:
az storage container create \
--name mycontainer \
--account-name mystorageaccount001 \
--auth-mode loginBest Practice: Always use --auth-mode login instead of passing account keys directly in commands. This leverages your Azure AD identity and avoids exposing secrets in shell history.
Exam Tips for Azure Administrator (AZ-104)
Key Concepts That Appear on the Exam
- The difference between az vm stop (OS shutdown, still billed) and az vm deallocate (compute released, not billed)
- Resource locks: use
az lock createto protect resources from accidental deletion - RBAC assignments are managed with
az role assignment create - The CLI uses Azure Resource Manager (ARM) under the hood — the same API as the portal
Common Exam Scenarios
- You need to prevent a resource group from being deleted — use a CanNotDelete lock
- You must assign a user the Contributor role on a specific resource group — use
az role assignment create - You need to resize a VM — use
az vm resizeafter deallocating
Quick Reference Commands
# Create a resource lock
az lock create \
--name DoNotDelete \
--resource-group rg-cloudninjas-demo \
--lock-type CanNotDelete
# Assign a role to a user
az role assignment create \
--assignee user@example.com \
--role Contributor \
--resource-group rg-cloudninjas-demo
# Resize a VM
az vm deallocate --resource-group rg-cloudninjas-demo --name vm-webserver-01
az vm resize \
--resource-group rg-cloudninjas-demo \
--name vm-webserver-01 \
--size Standard_B4ms
az vm start --resource-group rg-cloudninjas-demo --name vm-webserver-01
# List available VM sizes in a region
az vm list-sizes --location eastus --output tableSummary
You now have a solid foundation for using the Azure CLI in both real-world and exam scenarios. Here are the key takeaways:
- Install the CLI with a single command and authenticate using
az login - Every resource lives in a resource group — managing groups is fundamental
- Use
--querywith JMESPath to filter output and assign values to variables - Prefer
az vm deallocateoveraz vm stopto avoid unnecessary compute charges - Set CLI defaults with
az configureto speed up repeated workflows - Use
--auth-mode loginfor storage operations to avoid exposing keys
Next Steps: Practice creating and destroying resource groups, VMs, and storage accounts in a sandbox subscription. Combine CLI commands into bash scripts to automate multi-step deployments. When you are comfortable, explore Azure Bicep and ARM templates for infrastructure-as-code deployments.
Ready to go deeper? Check out the full AZ-104 Azure Administrator Certification Guide on CloudNinjas.ca for study plans, practice questions, and lab exercises.

