Skip to content

Get-ConsumptionUsage

Azure: Gets Azure consumption usage details

param(
	[string]$BillingPeriodName,
	[switch]$IncludeMeterDetails,
	[switch]$IncludeAdditionalProperties,
	[int]$MaxCount,
	[string]$Tag,
	[datetime]$StartDate,
	[datetime]$EndDate,
	[string]$InstanceName,
	[ValidateSet('*', 'UsageStart', 'UsageEnd', 'BillingPeriodName', 'InstanceName')]
	[string[]]$Properties = @('UsageStart', 'UsageEnd', 'BillingPeriodName', 'InstanceName')
)

try {
	Import-Module Az.Billing -ErrorAction Stop

	if ($Properties -contains '*') {
		$Properties = @('*')
	}

	[hashtable]$cmdArgs = @{
		'ErrorAction'                 = 'Stop'
		'IncludeMeterDetails'         = $IncludeMeterDetails
		'IncludeAdditionalProperties' = $IncludeAdditionalProperties
	}

	if ($MaxCount -gt 0) {
		$cmdArgs.Add('MaxCount', $MaxCount)
	}
	if (-not [string]::IsNullOrWhiteSpace($BillingPeriodName)) {
		$cmdArgs.Add('BillingPeriodName', $BillingPeriodName)
	}
	if (-not [string]::IsNullOrWhiteSpace($InstanceName)) {
		$cmdArgs.Add('InstanceName', $InstanceName)
	}
	if (-not [string]::IsNullOrWhiteSpace($Tag)) {
		$cmdArgs.Add('Tag', $Tag)
	}
	if ($null -ne $StartDate -and $StartDate.Year -gt 2000) {
		$cmdArgs.Add('StartDate', $StartDate.ToUniversalTime())
	}
	if ($null -ne $EndDate -and $EndDate.Year -gt 2000) {
		$cmdArgs.Add('EndDate', $EndDate.ToUniversalTime())
	}

	$ret = Get-AzConsumptionUsageDetail @cmdArgs | Sort-Object UsageStart -Descending | Select-Object $Properties
	Write-Output $ret
} catch {
	Write-Error $_
	exit 1
}

Name of a specific billing period.

Off

Include meter details in the usage records.

Off

Include additional properties in the usage records.

Maximum number of records to return.

The tag of the usages to filter.

The start date for filtering usage records.

The end date for filtering usage records.

The specific instance name to filter.

List of properties to include in the output. Use * for all properties.

An interactive directory of PowerShell scripts.