A Microsoft offering that enables tracking of cloud usage and expenditures for Azure and other cloud providers.
Yep - Azure Cost Management provides a REST API for retrieving resource-level cost data. Try the following PowerShell example, which queries virtual machine costs for a resource group over the previous 30 days and displays the results as PowerShell objects.
Connect-AzAccount
$subscription = (Get-AzContext).Subscription.Id
$group = "Production-East"
$begin = (Get-Date).ToUniversalTime().AddDays(-30).ToString("yyyy-MM-dd")
$finish = (Get-Date).ToUniversalTime().ToString("yyyy-MM-dd")
$scope = "/subscriptions/$subscription/resourceGroups/$group"
$uri = "https://management.azure.com$scope/providers/Microsoft.CostManagement/query?api-version=2025-03-01"
$query = @{
type = "ActualCost"
timeframe = "Custom"
timePeriod = @{
from = "${begin}T00:00:00Z"
to = "${finish}T00:00:00Z"
}
dataset = @{
granularity = "None"
aggregation = @{
total = @{
name = "PreTaxCost"
function = "Sum"
}
}
filter = @{
dimensions = @{
name = "ResourceType"
operator = "In"
values = @("Microsoft.Compute/virtualMachines")
}
}
grouping = @(
@{
type = "Dimension"
name = "ResourceId"
}
)
}
} | ConvertTo-Json -Depth 10
$response = Invoke-AzRestMethod -Method POST -Uri $uri -Payload $query
$report = $response.Content | ConvertFrom-Json
$columns = $report.properties.columns.name
foreach ($values in $report.properties.rows) {
$entry = [ordered]@{}
for ($i = 0; $i -lt $columns.Count; $i++) {
$entry[$columns[$i]] = $values[$i]
}
[pscustomobject]$entry
}
Replace Production-East with the resource group's actual name. The output contains aggregated cost data grouped by resource ID. Set granularity to Daily if you need daily cost records instead of a total for the period. Separately billed resources, such as managed disks and public IP addresses, require queries for their respective resource types. For production reporting, handle pagination if the response includes a nextLink and verify that the selected cost metric matches your billing requirements.
If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.
hth
Marcin