Get usage data with the Azure CLI

This article explains how you get cost and usage data with the Azure CLI. If you want to get usage data using the Azure portal, see View and download your Azure usage and charges.

Set up the Azure CLI

Start by preparing your environment for the Azure CLI.

  • If you prefer to run CLI reference commands locally, install the Azure CLI. If you're running on Windows or macOS, consider running Azure CLI in a Docker container. For more information, see How to run the Azure CLI in a Docker container.

    • If you're using a local installation, sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For other sign-in options, see Sign in with the Azure CLI.

    • When you're prompted, install the Azure CLI extension on first use. For more information about extensions, see Use extensions with the Azure CLI.

    • Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.

Configure an export job to export cost data to Azure storage

After you sign in, use the export commands to export usage data to an Azure storage account. You can download the data from there.

  1. Create a resource group or use an existing resource group. To create a resource group, run the group create command:

    az group create --name TreyNetwork --location "China North"
    
  2. Create a storage account to receive the exports or use an existing storage account. To create an account, use the storage account create command:

    az storage account create --resource-group TreyNetwork --name cmdemo
    
  3. Run the export create command to create the export:

    az costmanagement export create --name DemoExport --type Usage \--scope "subscriptions/00000000-0000-0000-0000-000000000000" --storage-account-id cmdemo \--storage-container democontainer --timeframe MonthToDate --storage-directory demodirectory
    

Next steps