Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

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
share|improve this answer
Also refer below URL it might be… – Gaurav Singh Dec 9 '10 at 22:13
Also refer to limit the time :… – Gaurav Singh Dec 9 '10 at 22:15
You could also use tskill.exe instead of rwinsta – mfinni Dec 9 '10 at 22:19
we try to avoid linking to the "hyphen-site" over here, as their site goes against everything that we stand for. If you need to, please quote the content of the post you're linking to in your answer, as a lot of people can't or don't know how to, read the answers on that site. – Mark Henderson Dec 10 '10 at 1:48

Use the idle timeout and disconnect timeout in Terminal Services.

share|improve this answer

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?

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.