Azure Command Line Tips and Tricks – Part 1

,
4–6 minutes

Originally from

Found, then improved from

Found, then improved

Repost

the wolf at a slate-blue metal workbench (no wood, no brown) lifting one amber wrench from an open grey toolbox, a blue cloud-shaped lantern hanging overhead, the raven peering in with a feather

Just to make life a little easier, here are some useful tips and tricks via the command line (az or powershell) for working with Azure

These are the commands I reach for when I am about to build virtual machines from the command line: which VM sizes I can actually use in a region, which Marketplace images exist, and how to accept an image’s terms so a script or Terraform run doesn’t stop halfway. There are two flavours, the Azure CLI (az) and Azure PowerShell. Use whichever your scripts are already written in. You need to be signed in first (az login or Connect-AzAccount).

az command line:

Which VM sizes can I use in this region?

Before you pick a size in a template, check that your subscription can actually deploy it in the region you want. A size that isn’t available fails the deployment with a SkuNotAvailable error. This lists the VM sizes in one region, with the reason code of the first restriction on each, and leaves out anything marked NotAvailableForSubscription.

PowerShell
# Get a list of all virtual machine SKUs that are currently available within a subscription
$location='eastus2'; az vm list-skus --location $location  --resource-type virtualMachines --query "[].{Name:name, Location:locationInfo[0].location, Status:restrictions[0].reasonCode} | [? Status != 'NotAvailableForSubscription']" --output table

Tip: change eastus2 to your region. The $location='eastus2'; part is PowerShell syntax, so run it as written in PowerShell; in Bash, use location=eastus2 instead. The output is a table with Name, Location and Status columns, and Status is empty for sizes with no restriction. az vm list-skus already hides sizes your subscription can’t use unless you add --all, so the filter is a second check.

Which Marketplace images are there, and accepting their terms

To create a VM from the command line you need the image’s URN: publisher:offer:sku:version. The first three lines list images from the live Marketplace, all of Canonical’s, then one Ubuntu SKU and the Windows Server 2022 Datacenter images in East US 2. The last line accepts an image’s Marketplace terms. Some images have a purchase plan with extra terms, and you have to accept those once per subscription before a script or Terraform can deploy them; the portal does it for you when you click through.

PowerShell
# Get a list of all available virtual machine products in the market place
az vm image list --all --output table --publisher Canonical
az vm image list --location eastus2 --publisher Canonical --offer ubuntu-26_04-lts --sku server --all --output table
az vm image list --location eastus2 --publisher MicrosoftWindowsServer --offer WindowsServer --sku 2022-datacenter --all --output table

# Get and accept Marketplace terms and conditions (needed to deploy a vm via command line or terraform)
# Only images with a purchase plan need this; Canonical's free Ubuntu LTS images have none.
az vm image terms accept --urn MicrosoftWindowsServer:WindowsServer:2022-datacenter-smalldisk-g2:latest

Tip: --all asks the live service instead of the CLI’s short cached list, so the first line can take several minutes. The --offer and --sku filters accept partial names. The output is a table with Architecture, Offer, Publisher, Sku, Urn and Version columns; copy the Urn into az vm create --image, or put latest in place of the version. The Ubuntu line now lists 26.04 LTS (offer ubuntu-26_04-lts, SKU server). Canonical names each LTS release’s offer ubuntu-<version>-lts, so 24.04 is ubuntu-24_04-lts, and these free Ubuntu images have no terms to accept. To check whether an image has terms at all, run az vm image show with its URN: if plan is null there is nothing to accept.

PowerShell command line

Accepting Marketplace terms in PowerShell

The same terms step with Azure PowerShell: Get-AzMarketplaceTerms fetches the terms for a publisher, offer (-Product) and plan (-Name), and piping them into Set-AzMarketplaceTerms -Accept accepts them for the current subscription. Both cmdlets are in the Az.MarketplaceOrdering module.

PowerShell
# Get and accept Marketplace terms and conditions (needed to deploy a vm via command line or terraform)
Get-AzMarketplaceTerms -Publisher "MicrosoftWindowsServer" -Product "WindowsServer" -Name "2022-datacenter-smalldisk-g2" -OfferType 'virtualmachine' | Set-AzMarketplaceTerms -Accept

Tip: change the publisher, product and name to the plan you want. The output is one row per plan with Name, Product, Publisher, Accepted and the link to the privacy policy, and Accepted reads True once it has worked. The Get-AzMarketplaceTerms call includes -OfferType 'virtualmachine', as Microsoft’s own examples do.

That’s the toolbox for now. If you only remember one of these, make it the first: checking a size before you deploy saves a failed run. Microsoft’s guides for the CLI and for PowerShell cover finding images in more depth.