Sign out remote desktop

This post was edited/updated on the 2nd of February, 2021. Published first on September 7th, 2014.

Based on users feedback, I decided to write a nice tool using SAPIEN PowerShell Studio 2015 which is a great support when you are creating advanced scripts.

This tool will support the IT help desk to manage remote desktop user sessions based on Remote Desktop Services 2012 / R2.

Please refer to the end of this article to download a copy of this tool.

Okay, this is not actually a Server virtualization related blog post but still in the virtualization space of course, Session virtualization in Remote Desktop Services [RDS] formerly known as Terminal Services [TS] and purely about PowerShell. None the less it is something that I use quite often when scripting RDS so I thought I would post it here.

As you know, the new BYOD [Bring Your Own Device] era is expanding number of devices every day, Operating Systems, and applications as well as the constant expectation that we all should be able to access vital information from anywhere anytime. Users can bring in whatever device they wish into work or work at home on their own personal device by using an RDP Client on that device and securely connecting with it.

You can get the latest Microsoft Remote Desktop Client App free for each platform here:

  • Windows
  • Android
  • Apple
  • Windows Phone Preview Current Release 8.1.4

The simple fact that the desktop and applications we are providing to our users are now running on servers under our direct control, and when they are working on site or remotely, their virtual desktop/session is still in the data center. RDS in all its forms is then an ideal way of allowing a [BYOD] policy.

Now Microsoft® RDS includes two techniques for providing virtual desktops, Session virtualization and VDI based on a collection of Windows 8 or 8.1 virtual desktops. While Session virtualization uses far less hardware resources, it is based on a server OS, which can be less experience for our users and limit the applications we can offer using this technique. In the opposite side, VDI consumes more resources, but offers our users a first-class experience. VDI is also different from RemoteApp, which lets you deliver individual applications that run remotely on the server to users own local desktops. Where they can run side by side with local applications. Theres no right answer here which option you need to choose, its about what is right for the department or business unit that will use VDI.

More information about Remote Desktop Services can be found here.

If you used to work with RDS aka [TS] in previous Windows Server releases, you will notice a tremendous improvement in Windows Server 2012/R2 that makes the deployment of VDI faster and easier, by providing a new unified central experience. RDS previously required multiple administrative tools, but with Server 2012/R2, most of them were combined into a single management console thats built into the new Server Manager that was introduced in Windows Server 2012 as showing in the following figure:

The new Server Manager central experience for Remote Desktop Services deployment. [Image: Charbel Nemnom]

Long story short, I am using Remote Desktop Services since Windows Server 2003/R2, which is end of support just a few months away, in between make sure you started planning the upgrade of your existing infrastructure to Windows Server 2012 R2.

Now back in Windows Server 2003/R2 and 2008/R2, If you need to control/shadow or Log Off a remote user session, we used to do the following as shown in below figures:

The Terminal Services experience Windows Server 2003/R2. [Image: Charbel Nemnom]

The Remote Desktop Services experience Windows Server 2008/R2. [Image: Charbel Nemnom]

In Windows Server 2012, Microsoft removed the Remote Control/Shadowing feature and restrict the Log Off feature in the UI by single user at a time , in other words, you cannot select multiple users and Log them off at the same time as we used to do in Windows Server 2008/R2 and 2003/R2.

But in Windows Server 2012 R2, Microsoft brings back the feature called Session Shadowing, with which youre able to monitor or take control of users active sessions. This was not available in Windows Server 2012, but Microsoft responded to input from customers who missed the feature , however the Log Off feature still by single user at a time.

You can shadow a remote user session in Windows Server 2012 R2 in one of two ways:

  • You can use the Server Manager if you prefer a graphical interface, OR
  • You can use the command line if you prefer a text-based interface

In Server Manager, you can browse for the session collection in which the user whose session you want to control is active or if you know which collection it is, you can access it directly from the Collections section. You can select whether you want to control the session or just view it and also whether or not the user will receive a prompt.

At the command line on a computer running Remote Desktop Client version 8.1 or above, type the following command:

C:\>mstsc /v: /shadow:

In case youre wondering how youre supposed to know the session ID? you can find it out by running the following PowerShell cmdlet [you must first import the Remote Desktop Module]:

PS C:\>Import-Module RemoteDesktop
PS C:\>Get-RDUserSession

Now what about to Log Off more than one user at a time? we still missing this feature.

The answer is

With PowerShell, of course:

Write-Host "==================================================" Write-Host "" Write-Host " PLEASE SELECT YOUR CHOICE " Write-Host "" Write-Host "==================================================" Write-Host "" Write-Host " A. End All Disconnected Remote User Sessions" Write-Host " B. End All Active Remote User Sessions" Write-Host " C. End All Idle Remote User Sessions" Write-Host " D. End All Remote User Sessions" Write-Host " X. Cancel and quit" $choice = Read-Host "`nEnter your Selection" Switch [$choice] { "A" {$RDSessions = Get-RDUserSession | Where-Object -Filter {$_.SessionState -eq 'STATE_DISCONNECTED'} } "B" {$RDSessions = Get-RDUserSession | Where-Object -Filter {$_.SessionState -eq 'STATE_ACTIVE'} } "C" {$RDSessions = Get-RDUserSession | Where-Object -Filter {$_.SessionState -eq 'STATE_IDLE'} } "D" {$RDSessions = Get-RDUserSession} "X" {Exit} } If [!$RDSessions] { Write-Output "No Remote User Sessions found with Choice:" $choice } Else { # Start Loop Foreach [$RDSession in $RDSessions] { Invoke-RDUserLogoff -UnifiedSessionID $RDSession.SessionId -HostServer $RDSession.HostServer -Force Write-Output "The user" $RDSession.UserName "is logged off from" $RDSession.HostServer "server" } # End Loop } # End If Read-Host "`nPress Enter to Exit

Chủ Đề