Problem solve Get help with specific problems with your technologies, process and projects.

Use a PowerShell script to force logoff an RDP session

When end users remain logged in to an RDP session, it consumes valuable resources. Use this PowerShell script to force users to log off.

It's not a new problem: An end user logs in to a Windows server using Remote Desktop Protocol and then forgets...

to log off. When a session remains open, it continues to consume resources on the server unnecessarily. There's a PowerShell script you can use to force end users to log off and free up those resources.

To begin a force logoff of a user's Remote Desktop Protocol (RDP) session, an admin must first query all the Remote Desktop Services' (RDS) server sessions on the machine and check their status. After detecting all disconnected services, the next step is the force logoff.

Download the free PowerShell module called PSTerminalServices and ensure it's available in your PowerShell. All the installation instructions are on the PSTerminalServices site.

The first step I'll take with this module is to see if I can get all of the active sessions on my lab server -- HYPERV (Figure 1).

Get-TSSession -ComputerName HYPERV
Get-TSSession -ComputerName HYPERV

Currently, I only have a couple sessions in a disconnected state. So, I'm seeing all of the sessions, but I only want to see those that are disconnected. To do that, I'll add the State parameter (Figure 2).

Get-TSSession -ComputerName HYPERV -State Disconnected
Get-TSSession -ComputerName HYPERV -State Disconnected

This is helpful, but there's still a problem. Session 0 is not an RDP session, and there’s no way to remove that from the result with Get-TSSession. I'll use Where-Object to remove that session as well (Figure 3).

Get-TSSession -ComputerName HYPERV -State Disconnected | where { $_.SessionID -ne 0 }
Get-TSSession -ComputerName HYPERV -State Disconnected | where { $_.SessionID -ne 0 }

Now, I can view all of the sessions I want to stop. Next, I just need to kill these sessions. To do this, the PSTerminalServices module has a Stop-TSSession cmdlet. This cmdlet does exactly what you think it does -- it kills the session (Figure 4).

Get-TSSession -ComputerName HYPERV -State Disconnected | where {$_.SessionID -ne 0} | Stop-TSSession
Get-TSSession -ComputerName HYPERV -State Disconnected | where {$_.SessionID -ne 0} | Stop-TSSession

The Stop-TSSession cmdlet forcefully logs off a session, which might cause end users to lose their work, so it prompts the admin. I could just hit "A" here and move on, but sometimes admins don't want to be prompted. If you plan to include this in a larger script, a prompt will break the script. The best bet is to remove that confirmation.

The Stop-TSSession cmdlet has a common PowerShell parameter, called –Force, which allows admins to perform the action without any confirmation.

Get-TSSession -ComputerName HYPERV -State Disconnected | where {$_.SessionID -ne 0} | Stop-TSSession -Force

If you receive no output, the session was logged off successfully.

Next Steps

Move files with a PowerShell FTP script

Manage RDS with PowerShell

How much do you know about RDP?

A breakdown of Remote Desktop Services roles

This was last published in September 2015

Dig Deeper on Windows PowerShell Scripting

PRO+

Content

Find more PRO+ content and other member only offers, here.

Join the conversation

3 comments

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

What do you do when an end user doesn't log off from an RDP session?
Cancel
You should be able to set the timeout on the server - both to lock the screen and kick the user off due to inactivity. The problem is you want to balance "went for coffee and is coming back, do not boot off prematurely" with security. The powershell recommend seems like a reasonable compromise. :-)
Cancel
Very useful for terminating sessions at a given moment. You can also enforce management of this with timeout settings via GPO - https://technet.microsoft.com/en-gb/library/cc754272.aspx
Cancel

-ADS BY GOOGLE

SearchServerVirtualization

SearchCloudComputing

SearchExchange

SearchSQLServer

SearchEnterpriseDesktop

SearchVirtualDesktop

Close