How to Log Off and Remove User Sessions Remotely with Power Shell

Many tech pros encounter and withstand repetitive tasks, handling things through the point-and-click approach. Automating things can be the ideal approach to deal with such scenarios. That is precisely why Azure Virtual Desktop (AVD) is quickly emerging as the preferred solution for organizations looking for a dependable, safe, and cost-efficient solution for remote working. Formerly known as Windows Virtual Desktop, AVD is a cloud-based virtualization service for remotely accessing Windows applications and data from various devices.

However, users must still use PowerShell modules to perform specific AVD tasks. This blog post explains how to log off and remove sessions from the AVD environment using PowerShell commands.

Note: It is important to understand that logging off and removing user sessions are two different things. When a user logs off from an AVD Session Host, the function disconnects the session, keeping the session active with the “disconnected” state on the session host.

Logging off using PowerShell

By following the steps outlined, you will be able to log off a user from Session Host. –

Please note that before executing the PowerShell commands explained in this blog post, you are required to install PowerShell modules. To install the PowerShell modules, execute the below command:

Install-Module -Name Az.DesktopVirtualization

Once the PowerShell modules are installed, connect to the AVD environment. That can be done via Azure username and password or using a Service Principal.

Logging Off Single user session

These sessions can be logged off with Disconnect-AzWVDUserSession or PowerShell cmdlet.

·       $CurrentResGroup = “SampleResourceGroup”

·       $CurrentPool = “SampleHostPoolName”

·       $SessHost = “SessionHostName”

·       $CurrentSessID = “14”

·       Disconnect-AzWvdUserSession -ResourceGroupName $CurrentResGroup -SampleHostPoolName $CurPool -SessionHostName $SessHost -Id $CurrentSessID

Remove User Session

Use below PowerShell commands to remove user session from the session host. Note that removing a user session will forcefully disconnect, and the user might lose unsaved data.

$CurrentResGroup = “SampleResourceGroup”$CurrentPool = “SampleHostPoolName”$SessHost = “SessionHostName”$CurrentSessID = “14”Remove-AzWvdUserSession -ResourceGroupName $CurrentResGroup -HostPoolName $CurrentPool -SessionHostName $SessHost -Id $CurrentSessID

AVD Sessions Manager

Wintellisys AVD Sessions Manager lets users manage sessions for all host pools. They can add multiple tenants in AVD Sessions Manager and oversee all sessions of the selected AVD Tenant. It also allows users to perform specific WVD management tasks such as disconnecting a single or bulk user, force logoff, and other management tasks.

Here is how users can automatically log off and remove AVD sessions using the AVD Sessions Manager

Log Off Session – Users can find the “Log Off Session” button in the AVD Sessions Manager Grid as shown below: 

When you click the “Force Log-Off” button, the process will show a dialogue box asking if you want to complete the action. If you say “Yes,” the process will forcefully log off the user from the AVD Sessions

Remove Session– Users can find the “Remove Session” button in the AVD Sessions Manager Grid as shown below:

Clicking on the “Remove Session” button will open a dialogue box asking if you want to take action. If you say “Yes,” the process will forcefully remove the user from the AVD Sessions.

About the Author Rajeev