How to Automate User Session Management in AVD
    • Dark
      Light
    • Download PDF

    How to Automate User Session Management in AVD

    • Dark
      Light
    • Download PDF

    Article summary

    Quick Answer:  To automate user session management in Azure Virtual Desktop: (1) Enable AVD Scaling Plans (native, free) to automatically power off session hosts outside business hours based on session count and schedule — this is the highest-impact automation and reduces compute costs by 35–65%. (2) Configure RDP idle session timeout policies via Group Policy or Intune to disconnect inactive sessions after 15–30 minutes, freeing host capacity. (3) Use Azure Automation runbooks (PowerShell) for advanced scenarios like cost-triggered host deallocation, session draining before maintenance, and cross-host-pool load balancing. (4) Use Logic Apps for event-driven session management — for example, scaling up hosts when a budget alert fires or when session queue depth exceeds a threshold.

    Why Session Management Automation Is Critical for AVD Cost Control

    Session host VMs are the largest cost component in any AVD deployment, typically representing 45–60% of total monthly spend. Unlike web application VMs where autoscaling is well-understood, AVD session hosts require nuanced automation: they must be ready when users need them (cold-start penalty is 3–7 minutes for session host boot), but deallocated when unused. Without automation, most organizations default to leaving hosts running 24/7 — paying full compute cost even at 2am on a Sunday.

    A 10-node Standard_D8s_v5 host pool running 24/7 costs approximately $2,800/month. With proper Scaling Plan automation reducing runtime to 12 hours/weekday + 0 hours/weekend, that same host pool costs closer to $820/month — a 70% reduction with no impact on user experience.

    Method 01 Low Complexity

    AVD Scaling Plans

    AVD Scaling Plans are the native, zero-code solution for automating session host scaling. Introduced by Microsoft in 2022 and now generally available, Scaling Plans let you define schedules (peak/off-peak/ramp-up/ramp-down) and session thresholds that govern how many hosts are running at any time. They are free of charge beyond the underlying VM compute costs.

    How Scaling Plans Work

    A Scaling Plan divides the day into four phases, each with configurable host behavior:

    Phase

    Typical Time

    Behavior

    Host Action

    Ramp-Up

    7:00 AM – 9:00 AM

    Pre-warm hosts before peak starts. New hosts start when session load per host exceeds threshold.

    Start hosts proactively

    Peak Hours

    9:00 AM – 5:00 PM

    Maintain sufficient hosts for all users. Load balancing distributes sessions across running hosts.

    Maintain minimum host count

    Ramp-Down

    5:00 PM – 7:00 PM

    Stop new sessions on excess hosts. Existing sessions continue until user logs off or is disconnected.

    Drain and deallocate empty hosts

    Off-Peak

    7:00 PM – 7:00 AM

    Minimum hosts running. Force-disconnect idle sessions after timeout. Deallocate hosts to zero if no sessions.

    Deallocate all non-session hosts

    Creating a Scaling Plan via Azure Portal

    • Navigate to Azure Virtual Desktop → Scaling Plans → + Create

    • Set the time zone to match your users' geography (critical for accurate schedule execution)

    • Add a schedule: configure the four phases with your business hours

    • Set the peak load balancing algorithm to Depth-First (fills hosts sequentially — more cost-efficient than Breadth-First which spreads sessions)

    • Set minimum hosts during off-peak to 0 for maximum savings — or 1 if you need one host always warm for early users

    • Under Host Pools, assign the Scaling Plan to your pooled host pools (not personal host pools — those use different logic)

    Method 2 — Idle Session Disconnect via Group Policy

    Method 02 Low Complexity

    RDP Idle Session Policies

    Idle sessions — users who walked away from their desktop without logging off — consume an active session slot on a host, preventing Scaling Plans from deallocating it. RDP session timeout Group Policy settings disconnect idle sessions automatically, freeing host capacity without user impact (the session is preserved — users reconnect to the same desktop state).

    Recommended RDP Session Timeout Settings

    GPO Setting

    Recommended Value

    Effect

    Set time limit for active but idle RDS sessions

    15 minutes

    Disconnects sessions with no keyboard/mouse input

    Set time limit for disconnected sessions

    2 hours

    Logs off disconnected sessions, freeing the slot fully

    Set time limit for active RDS sessions

    8 hours

    Forces re-authentication after a full workday

    End session when time limits are reached

    Enabled (for idle only)

    Fully terminates idle sessions vs. just disconnecting

    Apply these policies via Intune (Settings Catalog → Administrative Templates → Windows Components → Remote Desktop Services → Remote Desktop Session Host → Session Time Limits) or via traditional Group Policy on your AVD session hosts.

    Method 3 — Azure Automation Runbooks for Advanced Scenarios

    Method 03 Medium Complexity

    Azure Automation Runbooks (PowerShell)

    Azure Automation runbooks handle scenarios that Scaling Plans can't — personal host pool automation, cost-triggered deallocation, cross-host-pool load rebalancing, and pre-maintenance session draining. Runbooks are PowerShell or Python scripts that run on a schedule or are triggered by events (such as budget alerts or Logic App workflows).

    Runbook: Drain and Deallocate Idle Personal Host VMs

    # Runbook: Deallocate personal host VMs with no active sessions

    # Schedule: Run every 2 hours

    # Scope: Personal host pools

    param(

    [string]$ResourceGroupName = "rg-avd-production",

    [string]$HostPoolName = "hp-personal",

    [int]$IdleThresholdMinutes = 120

    )

    Connect-AzAccount -Identity # Uses Automation Account managed identity

    $sessionHosts = Get-AzWvdSessionHost `

    -ResourceGroupName $ResourceGroupName `

    -HostPoolName $HostPoolName

    foreach ($sh in $sessionHosts) {

    $hostName = $sh.Name.Split('/')[1]

    $vmName = $hostName.Split('.')[0]

    # Check active session count

    $sessions = Get-AzWvdUserSession `

    -ResourceGroupName $ResourceGroupName `

    -HostPoolName $HostPoolName `

    -SessionHostName $hostName

    if ($sessions.Count -eq 0) {

    # No sessions — check VM uptime before deallocating

    $vm = Get-AzVM -ResourceGroupName $ResourceGroupName `

    -Name $vmName -Status

    $powerState = $vm.Statuses | Where-Object {$_.Code -like "PowerState/*"}

    if ($powerState.Code -eq "PowerState/running") {

    Write-Output "Deallocating idle host: $vmName"

    Stop-AzVM -ResourceGroupName $ResourceGroupName `

    -Name $vmName -Force -NoWait

    }

    }

    }

    Write-Output "Idle host deallocation complete."

    Scheduling the Runbook

    • In the Azure Automation Account →Schedules → Add a schedule

    • Set recurrence to every 2 hours (or every 30 minutes for high-frequency environments)

    • Link the schedule to the runbook under Runbooks → [runbook name] → Link to schedule

    • Assign the Automation Account's managed identity the Desktop Virtualization Virtual Machine Contributor and Virtual Machine Contributor roles on the AVD resource group

    Method 4 — Logic Apps for Event-Driven Session Management

    Method 04 Medium Complexity

    Azure Logic Apps (Event-Driven)

    Logic Apps extend session management beyond scheduled automation into event-driven responses. Examples: automatically scale up session hosts when a new AVD user is provisioned via HR system webhook; drain hosts and send user notifications when a budget alert fires; start additional hosts when session queue depth exceeds 5 users waiting for a host.

    Logic App: Auto-Scale Hosts When Session Queue Exceeds Threshold

    // Logic App trigger: Azure Monitor Alert (session queue depth > 5)

    // Action 1: Get available session host count via AVD API

    // Action 2: If available hosts < 2, start a stopped host

    // Action 3: Send Teams notification with scale-up confirmation

    {

    "triggers": {

    "When_an_Azure_Monitor_alert_fires": {

    "type": "ApiConnectionWebhook",

    "inputs": {

    "host": { "connection": { "name": "azuremonitor" }},

    "path": "/subscriptions/{sub-id}/alerts"

    }

    }

    },

    "actions": {

    "Start_Session_Host_VM": {

    "type": "Http",

    "inputs": {

    "method": "POST",

    "uri": "https://management.azure.com/subscriptions/{sub-id}/resourceGroups/rg-avd-prod/providers/Microsoft.Compute/virtualMachines/{vm-name}/start?api-version=2023-03-01",

    "authentication": { "type": "ManagedServiceIdentity" }

    }

    }

    }

    }

    Method 5 — Nerdio Auto-Scale (Third-Party, Turnkey)

    Method 05 Low Complexity

    Nerdio Manager Auto-Scale

    Nerdio Manager's Auto-Scale is the most feature-complete session management automation available for AVD, with no scripting required. It extends beyond Scaling Plans with: cost-based scaling decisions (scale hosts down when AVD cost exceeds hourly budget), GPU host warm-up scheduling, user-specific host affinity for persistent session scenarios, and integration with FSLogix profile pre-mounting to reduce session startup time.

    Nerdio Auto-Scale is particularly valuable for personal host pool automation — an area where Microsoft's native Scaling Plans have no coverage.

    Automation Decision Matrix

    Scenario

    Best Method

    Effort

    Cost Impact

    Pooled hosts — business hours only

    AVD Scaling Plans

    Low

    35–65% compute savings

    Personal hosts — idle deallocation

    Azure Automation Runbook

    Medium

    20–40% savings on personal pools

    Idle session disconnect

    Group Policy / Intune

    Low

    Frees session slots, enables Scaling Plan deallocation

    Queue-based scale-out

    Logic App + Monitor Alert

    Medium

    Prevents user experience degradation during scale events

    Cost-triggered scale-down

    Budget Alert → Automation Runbook

    Medium-High

    Emergency cost cap mechanism

    GPU host pre-warming

    Nerdio Manager

    Low (managed)

    Reduces GPU idle time by 40%+


    Was this article helpful?

    ESC

    Eddy AI, facilitating knowledge discovery through conversational intelligence