Skip to main content
added an alternative script for the other portion of the question.
Source Link

In order to log out disconnected users while leaving the current user connected, copy the following script code into a .cmd file such as "LogOffUsers.cmd" and then run it as a service at midnight:

@echo off
for /f "tokens=1-7 delims=,: " %%a in ('query user ^| find /i "disc"') do logoff %%b

The script works by using the query command to find users who are disconnected by searching the phrase "disc", then logging them out.

If you wanted the script to instead run continuously as a service, logging out users when they had been disconnected/inactive for a certain period of time, you would instead use:

@echo off
:Top
for /f "tokens=1-7 delims=,: " %%a in ('query user ^| find /i "disc"') do if %%d GTR 32 (logoff %%b) else %%e GTR 32 (logoff %%b)
choice /T 120 /C 1 /D 1 /N
goto top

This script uses the same query command, but additionally checks the "IDLE TIME" portion of the results, logging the user off if idle time is greater than 32 ( "GTR 32" ). That phrase occurs twice because the "IDLE TIME" token can occur two slightly different positions. Then the line beginning with "choice" waits 2 minutes before performing the operation again by looping to the beginning. You can increase or decrease the "32" value according to your needs.

Found here.

In order to log out disconnected users while leaving the current user connected, copy the following script code into a .cmd file such as "LogOffUsers.cmd" and then run it as a service at midnight:

@echo off
for /f "tokens=1-7 delims=,: " %%a in ('query user ^| find /i "disc"') do logoff %%b

The script works by using the query command to find users who are disconnected by searching the phrase "disc", then logging them out.

Found here.

In order to log out disconnected users while leaving the current user connected, copy the following script code into a .cmd file such as "LogOffUsers.cmd" and then run it as a service at midnight:

@echo off
for /f "tokens=1-7 delims=,: " %%a in ('query user ^| find /i "disc"') do logoff %%b

The script works by using the query command to find users who are disconnected by searching the phrase "disc", then logging them out.

If you wanted the script to instead run continuously as a service, logging out users when they had been disconnected/inactive for a certain period of time, you would instead use:

@echo off
:Top
for /f "tokens=1-7 delims=,: " %%a in ('query user ^| find /i "disc"') do if %%d GTR 32 (logoff %%b) else %%e GTR 32 (logoff %%b)
choice /T 120 /C 1 /D 1 /N
goto top

This script uses the same query command, but additionally checks the "IDLE TIME" portion of the results, logging the user off if idle time is greater than 32 ( "GTR 32" ). That phrase occurs twice because the "IDLE TIME" token can occur two slightly different positions. Then the line beginning with "choice" waits 2 minutes before performing the operation again by looping to the beginning. You can increase or decrease the "32" value according to your needs.

Found here.

simplified script for specific scenario
Source Link

In order to log out disconnected users while leaving the current user connected, copy the following script code into a .cmd file such as "LogOffUsers.cmd" and then run it as a service at midnight:

@echo off
for /f "tokens=1-7 delims=,: " %%a in ('query user ^| find /i "disc"') do if %%d GTR 32 (logoff %%b) else %%e GTR 32 (logoff %%b)

The script works by using the query command to find users who are disconnected by searching the phrase "disc", then logging them out.

Found here.

In order to log out disconnected users while leaving the current user connected, copy the following script code into a .cmd file such as "LogOffUsers.cmd" and then run it as a service at midnight:

@echo off
for /f "tokens=1-7 delims=,: " %%a in ('query user ^| find /i "disc"') do if %%d GTR 32 (logoff %%b) else %%e GTR 32 (logoff %%b)

The script works by using the query command to find users who are disconnected by searching the phrase "disc", then logging them out.

Found here.

In order to log out disconnected users while leaving the current user connected, copy the following script code into a .cmd file such as "LogOffUsers.cmd" and then run it as a service at midnight:

@echo off
for /f "tokens=1-7 delims=,: " %%a in ('query user ^| find /i "disc"') do logoff %%b

The script works by using the query command to find users who are disconnected by searching the phrase "disc", then logging them out.

Found here.

Tailored the solution to user's specific request
Source Link

CopyIn order to log out disconnected users while leaving the current user connected, copy the following script code into a .cmd file such as "LogOffTimer"LogOffUsers.cmd" and then run it as a service at midnight:

@echo off
:Top
for /f "tokens=1-7 delims=,: " %%a in ('query user ^| find /i "disc"') do if %%d GTR 32 (logoff %%b) else %%e GTR 32 (logoff %%b)
choice /T 120 /C 1 /D 1 /N
goto top

The script works by using the query command to find users who are disconnected by searching the phrase "disc", then logging them out.

Found here.

Copy the following script code into a .cmd file such as "LogOffTimer.cmd" and then run it as a service:

@echo off
:Top
for /f "tokens=1-7 delims=,: " %%a in ('query user ^| find /i "disc"') do if %%d GTR 32 (logoff %%b) else %%e GTR 32 (logoff %%b)
choice /T 120 /C 1 /D 1 /N
goto top

Found here.

In order to log out disconnected users while leaving the current user connected, copy the following script code into a .cmd file such as "LogOffUsers.cmd" and then run it as a service at midnight:

@echo off
for /f "tokens=1-7 delims=,: " %%a in ('query user ^| find /i "disc"') do if %%d GTR 32 (logoff %%b) else %%e GTR 32 (logoff %%b)

The script works by using the query command to find users who are disconnected by searching the phrase "disc", then logging them out.

Found here.

Source Link
Loading