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
![](https://mlumx7rcpldi.i.optimole.com/w:1024/h:283/q:mauto/f:best/ig:avif/https://styr-it.nl/wp-content/uploads/2022/06/Scrnshot1.png)
![](https://mlumx7rcpldi.i.optimole.com/w:661/h:24/q:eco/f:best/ig:avif/https://styr-it.nl/wp-content/uploads/2022/06/Scrnshot2.png)
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/
Let’s hope Microsoft will add something similair soon to overcome this issue.