Checking Azure costs from the command line

Posted: (EET/GMT+2)

 

Cost control is important in all Azure environments, be they development, testing or production. If you run test environments or small production workloads in Azure, it is worth checking costs from the command line occasionally.

The Azure Portal is good for dashboards, but command-line queries are handy for scripts, monthly checks, and quick troubleshooting.

First, check the current subscription:

az account show --output table

If you have multiple subscriptions, list them:

az account list --output table

Then store the current subscription ID in a variable:

subscriptionId=$(az account show --query id -o tsv)

You can query Azure Cost Management using az rest.

az rest \
    --method post \
    --uri "https://management.azure.com/subscriptions/$subscriptionId/providers/Microsoft.CostManagement/query?api-version=2025-06-01" \
    --body '{
        "type": "Usage",
        "timeframe": "MonthToDate",
        "dataset": {
            "granularity": "None",
            "aggregation": {
                "totalCost": {
                    "name": "PreTaxCost",
                    "function": "Sum"
                }
            }
        }
    }'

This returns the current month-to-date cost data for the selected subscription.

Tip: the exact values and available fields can depend on the subscription type, billing account, and permissions.

For a more useful view, group the result by resource group:

az rest \
    --method post \
    --uri "https://management.azure.com/subscriptions/$subscriptionId/providers/Microsoft.CostManagement/query?api-version=2025-06-01" \
    --body '{
        "type": "Usage",
        "timeframe": "MonthToDate",
        "dataset": {
            "granularity": "None",
            "aggregation": {
                "totalCost": {
                    "name": "PreTaxCost",
                    "function": "Sum"
                }
            },
            "grouping": [
                {
                    "type": "Dimension",
                    "name": "ResourceGroup"
                }
            ]
        }
    }'

That is usually the first useful breakdown when checking development or test subscriptions. If you combine this with normal Azure CLI commands, you are ready to investigate that surprise cost. For example:

az group list --output table

az resource list \
    --resource-group my-resource-group \
    --output table

Common resources to check first:

  • virtual machines left running
  • large managed disks
  • public IP addresses
  • Log Analytics workspaces
  • container registries
  • databases scaled higher than expected.

For one-off checks, the Portal is often easier. For repeatable checks, scripts are the way to go.

If you run temporary Azure environments, consider adding a cleanup step to your scripts:

az group delete \
    --name my-test-rg \
    --yes

Do this only for resource groups that are meant to be disposable.

For small teams, a monthly command-line cost check can prevent a lot of surprises.

Happier hacking!