I need some script to be able to kill terminal sessions in our server.

Basically, some users are connecting through an application via Terminal Services. When they close the application, it usually triggers the disconnection and closure of the Terminal Services.

On some occasions, the users instead of closing the application, closes the terminal service. This leaves the terminal session hanging since the application is still running.

Is there a script available which would be able to detect those hanging terminal services, including the application which is running inside it?

Appreciate any help on this. Thanks.


Please try the below mentioned Script:

REM @echo off
REM This batch file will read a list of computers in 'computers.txt', query (qwinsta)
REM the sessions that are currenlty disconnected, then pass them to
REM reset them using (rwinsta).  It might not be as graceful as a logoff 
REM but it works.

REM Mike Stone
REM 06/19/2008

For /F %%A in (computers.txt) do (
    for /f "tokens=2" %%i in ('qwinsta /SERVER:%%A ^| find /i "disc"') do echo %%i | rwinsta %%i /SERVER:%%A /V

Use the idle timeout and disconnect timeout in Terminal Services.


Assuming that you're running Terminal Services in a domain (and even if you're not), there are a number of GPO settings (or local settings) to control RDP sessions. Seems like using those settings is preferred over reinventing the wheel. Why write and run a script when you can configure Group Policy to do the work for you?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy