How to get list of Reservations in Azure via Powershell that include remaining cost or cancellation fee?

Chris Moceri 10 Reputation points
2024-06-03T20:22:18.02+00:00

I'm trying to use powershell or the Azure API to get a list of all of my reservations in USWest and USWest2 that are in a provisioned state of Succeeded (or not archived), along with their remaining cost like you can in the UI (see attached picture). Having to click through the UI for each reservation would take quite awhile.

Is there a way to do this using the Az powershell cmdlets or the API?

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,260 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,272 questions
{count} votes

1 answer

Sort by: Most helpful
  1. PRADEEPCHEEKATLA-MSFT 84,456 Reputation points Microsoft Employee
    2024-06-04T06:06:41.6666667+00:00

    @Chris Moceri - Thanks for the question and using MS Q&A paltform.

    Yes, you can use the Azure PowerShell module to get a list of all your reservations in USWest and USWest2 that are in a provisioned state of Succeeded (or not archived), along with their remaining cost or cancellation fee. Here's an example PowerShell script that you can use:

    # Connect to your Azure account
    Connect-AzAccount
    
    # Set the subscription context
    Set-AzContext -SubscriptionId <SubscriptionId>
    
    # Get all reservations in USWest and USWest2 that are in a provisioned state of Succeeded (or not archived)
    $reservations = Get-AzReservation `
        -Location "USWest","USWest2" `
        -State "Succeeded" `
        -IncludeExpired $false `
        -IncludeChildren $false `
        -ApiVersion "2018-01-31"
    
    # Loop through each reservation and get its remaining cost or cancellation fee
    foreach ($reservation in $reservations) {
        $reservationOrderId = $reservation.ReservationOrderId
        $reservationId = $reservation.Id
        $remainingCost = Get-AzReservationUsage `
            -ReservationOrderId $reservationOrderId `
            -ReservationId $reservationId `
            -ApiVersion "2018-01-31" `
            | Where-Object { $_.Name -eq "RemainingCost" } `
            | Select-Object -ExpandProperty Quantity
        $cancellationFee = Get-AzReservation `
            -ReservationOrderId $reservationOrderId `
            -ReservationId $reservationId `
            -ApiVersion "2018-01-31" `
            | Select-Object -ExpandProperty CancellationFee
        Write-Output "Reservation: $($reservation.Name), Remaining Cost: $($remainingCost), Cancellation Fee: $($cancellationFee)"
    }
    

    This script first connects to your Azure account and sets the subscription context. It then uses the Get-AzReservation cmdlet to get all reservations in USWest and USWest2 that are in a provisioned state of Succeeded (or not archived). Finally, it loops through each reservation and uses the Get-AzReservationUsage and Get-AzReservation cmdlets to get its remaining cost or cancellation fee.

    Note that you may need to modify the script to fit your specific needs, such as filtering by reservation type or scope. Also, make sure that you have the latest version of the Azure PowerShell module installed.

    For more details, refer to Manage Reservations for Azure resources.

    Hope this helps. Do let us know if you any further queries.


    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.

    1 person found this answer helpful.
    0 comments No comments