Logging off and removing WVD user sessions in bulk

Like other Microsoft services, Windows Virtual Desktop ships with required PowerShell modules to interact with WVD resources. Microsoft Azure Portal provides a limited set of functionalities through which you can manage some aspects of the Windows Virtual Desktop environment, but you are still required to use PowerShell modules to perform certain tasks. For example, you cannot remove or disconnect user sessions in bulk using the Azure Portal. This is where this article comes handy. In this article, we explain the difference between logging off and remove actions and what PowerShell modules to use to log off and removing WVD user sessions for all or selected host pools. Please note this article explains modules used for Windows Virtual Desktop Spring Update. Before you start to use PowerShell commands or scripts explained in this article, please ensure you install WVD PowerShell modules.

Understanding the difference between logging off and removing user sessions

removing-WVD-user

Before you start to use the PowerShell commands and scripts explained in this article, let us understand the differences between logging off and removing user sessions. When you log off a user session from a WVD tenant, the process actually disconnects the session and keeps the session alive with the “disconnected” state on the session host. On the other hand, when you remove a user session, the process actually removes the session from the session host and also removes session entry from the WVD database.

Tip: The removal of the session always frees up system resources on the session host, whereas logging off a session keeps the session alive, which, in turn, uses the system resources. So, if any of the session hosts run out of system resources because a particular user is causing system resources to be utilized by the applications running by the user, you can use the removal method to remove the user session from the session host.

Installing Az.DesktopVirtualization PowerShell module

For WVD 2.0, you are required to use the Az.DesktopVirtualization PowerShell module. To install Az.DesktopVirtualization PowerShell modules, you need to execute this command:
Install-Module -Name Az.DesktopVirtualization
After executing the above command, you should have Az.DesktopVirtualization PowerShell modules installed on your computer. To confirm that you have Az.DesktopVirtualization PowerShell modules installed, open PowerShell ISE, and then check for Get-AzWVDUserSession or similar PowerShell cmdlets.

Connecting to Azure Windows Virtual Desktop 2.0 environment

Once you have installed the PowerShell modules, you will be required to use the Connect-AzConnect PowerShell module to connect to the WVD 2.0 environment. You can either connect using an Azure username and password or use a Service Principal.

Logging off or removing a single user session

To log off or remove a single user session from WVD, you can always use the Disconnect-AzWVDUserSession or Remove-AzWVDUserSession PowerShell cmdlet. Here are a few examples that explain how to log off or remove single user sessions using PowerShell cmdlets. Please make sure to provide inputs to variables as part of the commands before executing.

To log off a user session:

$CurResGroup = "ThisResourceGroup"
$CurPool = "HostPoolName"
$SessHost = "SessionHostName"
$CurSessID = "15"
Disconnect-AzWvdUserSession -ResourceGroupName $CurResGroup -HostPoolName $CurPool -SessionHostName $SessHost -Id $CurSessID

To remove a user session:

$CurResGroup = "ThisResourceGroup"
$CurPool = "HostPoolName"
$SessHost = "SessionHostName"
$CurSessID = "15"
Remove-AzWvdUserSession -ResourceGroupName $CurResGroup -HostPoolName $CurPool -SessionHostName $SessHost -Id $CurSessID

Logging off or removing user sessions in bulk

To start logging off or removing user sessions in bulk, you can use the PowerShell script below. Make sure to modify the scripts to provide inputs for the resource group and host pool. The PowerShell script below, which is collected from Wintellisys WVD Manager management packs, can be utilized only for a single host pool.

Note that the script can be used to perform one action at a time — either remove the user session or log off the user session. By default, it takes “Remove” action. To change the action, modify the “$LogOffOrRemove” variable.

$CurResGroup = "ThisResourceGroup"
$CurPool = "HostPoolName"
$LogOffOrRemove = "Remove"
$UserSessFile = "C:\Temp\AllUserSessions.CSV"
Remove-item$UserSessFile -ErrorActionSilentlyContinue
Get-AzWvdUserSession-ResourceGroupName$CurResGroup -HostPoolName $CurPool | Select-Object* | Export-CSV$UserSessFile
$CSV = Import-CSV$UserSessFile
ForEach ($Item in $CSV)
{
$SessHost = $Item.SessionHostName
$CurSessID = $Item.SessionID
$Error.Clear()
IF ($LogOffOrRemove -eq "Remove")
{
Remove-AzWvdUserSession-ResourceGroupName $CurResGroup -HostPoolName $CurPool -SessionHostName $SessHost -Id $CurSessID
}
IF ($LogOffOrRemove -eq "Logoff")
{
Disconnect-AzWvdUserSession-ResourceGroupName $CurResGroup -HostPoolName $CurPool -SessionHostName $SessHost -Id $CurSessID
}
IF ($Error.Count -eq 0)
{
Write-Host"UserWasRemoved..."
}
else
{
Write-Host"ErrorRemovingUser:$Error"
}
}

The PowerShell script connects to a host pool specified in the “$CurPool” variable and then collects all user sessions from that host pool. The user sessions are stored in a CSV file named C:\Temp\AllUserSessions.CSV for processing by the next PowerShell commands. The ForEach loop retrieves required information from the CSV file and then takes an action: remove or log off.

One host pool only

The script can only be targeted to one host pool and you are required to provide the resource group name of the host pool. If you have hundreds of host pools, maintaining PowerShell scripts for all host pools becomes a little challenging, and also you need to make sure you are executing the logoff or remove commands for the correct host pool to avoid any disruption in your business services. It is worth mentioning that there are applications and tools available that you could utilize without causing any downtime to WVD services. Wintellisys WVD Manager Version 2.1 provides user sessions manager component that maintains connections for all host pools in your production environment and helps you log off or remove WVD user sessions for selected host pools as shown in the screenshot below:

removing WVD user

The application also maintains logs for actions performed using the Wintellisys WVD Manager. For example, if a user performs log off action and another user performs a remove action, you can easily find who performed what by looking at the operational logs in WVD Manager.

Featured image: Pixabay

About The Author

2 thoughts on “Logging off and removing WVD user sessions in bulk”

  1. Thank you Nirmal for the article. This is really helpful.
    We have deployed WVD in our environment with fslogix file share roaming profile. But we have observed even after logoff from session host local_user exist in the session host which to be removed by policy.
    Can you please help on this.

  2. Hi Kalyan,
    Sorry it took me longer than expected to get back to you.
    You can actually deploy FSLogix Registry Entry to remove local_user folders when users log off. However, please note it only works if users log off successfully.

    Thanks,
    Nirmal

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top