Skip to content
Home » Blog » Azure Virtual Desktop script to logoff all disconnected sessions

Azure Virtual Desktop script to logoff all disconnected sessions

During the administration of AVD I found it annoying that I wasn’t able to just quickly logoff all disconnected users for a specific hostpool. To overcome this issue I created a fairly simple script which also asks you which Hostpool to check and logoff all disconnected users. It assumes you are in the correct subscription and logged in.

$ListOfHp = Get-AzWvdHostPool | Select Name,ID| Out-GridView -Title "Select Hostpool to logoff all Disconnected sessions" -OutputMode Single

if(!$ListOfHp){Write-Host "No Hostpool selected, exiting script...";start-sleep -seconds 10; Exit} 

$HostpoolName = $ListOfHp.Name
$ResourceGroupName = $ListOfHp.Id.Split("/")[4] 

$AllSessions = Get-AzWvdUserSession -HostPoolName $HostpoolName -ResourceGroupName $ResourceGroupName

ForEach ($Item in $AllSessions){if($item.SessionState -eq "Disconnected"){    
Write-Host "Removing user's:" $Item.ActiveDirectoryUserName ", session from Sessionhost:" $item.Name.Split("/")[1]    
Remove-AzWvdUserSession -HostPoolName $HostpoolName -ResourceGroupName $ResourceGroupName -Id $item.Name.Split("/")[2] -SessionHostName $item.Name.Split("/")[1]    
}}

pause

See below screenshots what will happen if you run the script

The end result will be that disconnected sessions will be removed (logged out from the VM). You should not use the Disconnect command, see the following blog: https://techgenix.com/logging-off-and-removing-wvd-user-sessions/

Leave a Reply

Your email address will not be published.