Unassign Users from Personal Azure Virtual Desktop

Open PowerShell and modify the SubscriptionID, ResourceGroupName, HostPoolName and SessionHostName. This script will remove the currently assigned user.

Connect-AZAccount
function GetAuthToken($resource) {
    $context = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile.DefaultContext
    $Token = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account, $context.Environment, $context.Tenant.Id.ToString(), $null, [Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never, $null, $resource).AccessToken
    $authHeader = @{
        'Content-Type' = 'application/json'
        Authorization  = 'Bearer ' + $Token
    }
    return $authHeader
}
 
$token = GetAuthToken -resource https://management.azure.com
 
$subscriptionId = "subscriptionid"
$ResourceGroupName = 'rgname'
$hostpoolname = "hpname"
$SessionHostName = "AVD"
$baseUrl = https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.DesktopVirtualization/hostpools/{2}/sessionHosts/{3}?api-version=2022-02-10-preview&force=true -f $subscriptionId, $ResourceGroupName, $hostpoolname, $SessionHostName
$Body = @{
  properties = @{
      allowNewSession = "true"
        assignedUser = ""
  }
}
$parameters = @{
    URI     = $baseUrl
    Method  = "PATCH"
    Headers = $token
    Body = $Body | ConvertTo-Json
}
$hostpoolToken = Invoke-RestMethod @parameters
Share or Save this:
Share