Get Azure cost for subscription including marketplace expenses with PowerShell

This will get last month consumption for a subscription including the money spend on marketplace.

$subscriptioname = "CHANGETHIS"
$roundDecimals = 2
$lastMonthBilling = (Get-Date).AddMonths(-1).ToString('yyyyMM')

# Set context to subscription
Set-azcontext -SubscriptionName $subscriptioname

# Get billing period
$BillingPeriod = Get-AzBillingPeriod -Name $lastMonthBilling
$startDate = $BillingPeriod.BillingPeriodStartDate.ToString("dd-MM-yyyy")
$endDate = $BillingPeriod.BillingPeriodEndDate.ToString("dd-MM-yyyy")

# Collect cost data
$currentCost = Get-AzConsumptionUsageDetail -StartDate $startDate -EndDate $endDate 
$currentCost += Get-AzConsumptionMarketplace -StartDate $startDate -EndDate $endDate

# Write output to screen
Write-Host "Current Cost of Subscription" (Get-AzContext).Subscription.Name ":" ([math]::Round(($currentCost | Measure-Object -Property PretaxCost -Sum).sum,$roundDecimals))

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.