This script pulls monthly usage trends (record count and volume in bytes) for all Log Analytics workspaces in your Azure account. It uses the Azure CLI and supports:
- All subscriptions by default (or target one via
--subscription/-Subscription) - Region and resource group filtering
- Parallel processing with timeout protection
- CSV output with clean, deduplicated structure
- Live progress reporting
- Error handling and debug logging
Bash (Linux/macOS):
./query_az_logs.sh [OPTIONS]PowerShell (Windows/macOS/Linux):
./query_az_logs.ps1 [OPTIONS]| Bash | PowerShell | Description |
|---|---|---|
--subscription <ID> |
-Subscription <ID> |
Azure subscription ID (if omitted, queries all) |
--region <REGION> |
-Region <REGION> |
Filter by region (e.g. eastus, centralus) |
--resource-group <RG> |
-ResourceGroup <RG> |
Filter by resource group name |
--timeout <SECONDS> |
-Timeout <SECONDS> |
Per-workspace query timeout in seconds (default: 15) |
-h / --help |
-? / Get-Help |
Show usage instructions |
Download the script and make it executable:
chmod +x query_az_logs.shLog in to Azure:
az loginNo additional setup is required. You may need to allow script execution:
Set-ExecutionPolicy -Scope CurrentUser RemoteSignedLog in to Azure:
az loginQuery all subscriptions:
./query_az_logs.sh./query_az_logs.ps1Target a single subscription with filters:
./query_az_logs.sh \
--subscription 11111111-2222-3333-4444-555555555555 \
--region eastus \
--resource-group my-analytics-rg \
--timeout 20./query_az_logs.ps1 `
-Subscription 11111111-2222-3333-4444-555555555555 `
-Region eastus `
-ResourceGroup my-analytics-rg `
-Timeout 20You can run this query manually in Azure Monitor → Logs or the Log Analytics workspace query editor without using the scripts.
let startDate = startofmonth(datetime_add('month', -11, startofmonth(now())));
union *
| where TimeGenerated >= startDate
| extend Month = format_datetime(TimeGenerated, 'yyyy-MM')
| summarize
MonthlyRecords = count(),
MonthlyVolume_GB = round(sum(_BilledSize) / 1073741824, 3)
by Month
| sort by Month ascWhat it returns: One row per month for the last 12 months, with total record count and billed volume in GB across all tables in the workspace.
Retention period is a workspace-level property and is not accessible from within a KQL log query. The export scripts (
query_az_logs.sh/query_az_logs.ps1) capture retention viaaz monitor log-analytics workspace listand include it as theRetentionInDayscolumn in the CSV output. To check retention for a specific workspace manually: Log Analytics workspace → Usage and estimated costs → Data retention.
To run it:
- Open the Azure Portal
- Navigate to your Log Analytics workspace (search "Log Analytics workspaces")
- Select Logs from the left menu
- Paste the query and click Run
Note: This query must be run inside a Log Analytics workspace → Logs. It will not work in Azure Resource Graph Explorer (
portal.azure.com/#view/...resourceGraph), which uses a different schema and does not exposeTimeGeneratedor_BilledSize.
To adjust the lookback window, change
-11to the number of additional months to go back (e.g.-23for 24 months).
To break down volume by individual table (the Azure equivalent of a Splunk index), add Type to the grouping:
let startDate = startofmonth(datetime_add('month', -11, startofmonth(now())));
union *
| where TimeGenerated >= startDate
| extend Month = format_datetime(TimeGenerated, 'yyyy-MM')
| summarize
MonthlyRecords = count(),
MonthlyVolume_GB = round(sum(_BilledSize) / 1073741824, 3)
by Month, Table = Type
| sort by Month asc, MonthlyVolume_GB descWhat it returns: One row per table per month. Sorted by volume descending within each month so the noisiest tables surface first. Useful for identifying which data sources are driving ingestion costs.
In Excel, pivot on
Tableto compare volume trends across tables side by side.
| File | Description |
|---|---|
workspace_logs.csv |
Main output: one row per workspace per month |
errors.log |
Captures failed or timed-out queries |
debug.log |
Diagnostic info and status logs |
SubscriptionID,WorkspaceID,Name,ResourceGroup,Location,CreatedDate,RetentionInDays,DailyQuotaGb,Month,MonthlyRecords,MonthlyVolume
abc-sub-123,...,MyWorkspace,...,2023-01-01T12:00:00Z,30,5.0,2024-12,124234,203948573
abc-sub-123,...,MyWorkspace,...,2023-01-01T12:00:00Z,30,5.0,2025-01,122003,200330123- Azure CLI (
az) - Azure credentials with
ReaderorLog Analytics Readerrole
Bash only:
- Bash-compatible shell (Linux/macOS)
timeoutorgtimeout(macOS:brew install coreutils)
PowerShell only:
- PowerShell 7+ (
pwsh) recommended for cross-platform use - Windows PowerShell 5.1 is also supported
- Workspaces with no recent data won't return rows (this is not an error).
- Queries are billed per run (beware of large environments).
- Script does not yet support parallel throttling (all workspaces are queried concurrently).
- Visualize the CSV in Excel or Google Sheets to analyze trends.
- Use filters to scope large environments and reduce cost.
- Combine with cron (Bash) or Task Scheduler (PowerShell) to collect monthly snapshots for auditing.
- Consider enriching with
workspace.idortagsif needed.
MIT — free to use, fork, and modify.