Batch file to disconnect users on Terminal Services

@echo off
rem Force all remote desktop sessions off
query session >session.txt
for /f “skip=2 tokens=3,” %%i in (session.txt) DO logoff %%i
del session.txt


@echo off
echo Force disconnected remote desktop sessions off
query session >session.txt
rem “skip two lines
rem  read 3 items
rem (default) delimited by space

rem the second token is ‘disconnected’ then logoff the session.
for /f “skip=2 tokens=2,3,4” %%i in (session.txt) DO if ‘%%j’ == ‘Disc’ logoff %%i
del session.txt

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s