Activity Log Audit
How to query the Azure Activity Log via CLI to see what changed on a resource, when, and by whom.
Prerequisites
- Azure CLI authenticated (
az login) - Appropriate Reader (or higher) role on the target subscription
Get the Resource ID
Activity log queries require the full resource ID. Get it for any VM:
az vm show \ --name <vm-name> \ --resource-group <resource-group> \ --query id -o tsvSave it for reuse:
VMID=$(az vm show --name vm-ssrs16-prod --resource-group rg-eshars-prod --query id -o tsv)Query Activity Log
Last 7 days
az monitor activity-log list \ --resource-id "$VMID" \ --offset 7d \ -o tableLast 14 days
az monitor activity-log list \ --resource-id "$VMID" \ --offset 14d \ -o tableCustom date range
az monitor activity-log list \ --resource-id "$VMID" \ --start-time 2026-02-05T00:00:00Z \ --end-time 2026-02-19T00:00:00Z \ -o tableFormatting: Key Columns Only
Show operation, status, timestamp, and who made the change:
az monitor activity-log list \ --resource-id "$VMID" \ --offset 14d \ --query "[].{Operation:operationName.localizedValue, Status:status.localizedValue, Time:eventTimestamp, Caller:caller, Category:category.localizedValue}" \ -o tableFiltering Out Noise
Exclude Microsoft.Advisor recommendations
The portal shows Advisor recommendations mixed in with real changes. Filter them out:
az monitor activity-log list \ --resource-id "$VMID" \ --offset 14d \ --query "[?caller!='Microsoft.Advisor'].{Operation:operationName.localizedValue, Status:status.localizedValue, Time:eventTimestamp, Caller:caller}" \ -o tableOnly actual changes (writes and actions)
az monitor activity-log list \ --resource-id "$VMID" \ --offset 14d \ --query "[?contains(operationName.value, '/write') || contains(operationName.value, '/action')].{Operation:operationName.localizedValue, Status:status.localizedValue, Time:eventTimestamp, Caller:caller}" \ -o tableOther Resource Types
The same pattern works for any Azure resource — not just VMs. Replace the az vm show with the appropriate command:
# App Serviceaz webapp show --name <app-name> --resource-group <rg> --query id -o tsv
# SQL Serveraz sql server show --name <server-name> --resource-group <rg> --query id -o tsv
# SQL Databaseaz sql db show --name <db-name> --server <server-name> --resource-group <rg> --query id -o tsv
# Key Vaultaz keyvault show --name <vault-name> --query id -o tsv
# By resource group (all resources in the group)az monitor activity-log list --resource-group <rg> --offset 14d -o tableNotes
- Activity Log retains events for 90 days (
--offsetmax is90d) - For longer retention, route logs to a Log Analytics workspace via diagnostic settings
- The CLI returns empty results (no error) if the resource ID is invalid — double-check with
echo "$VMID"