9

I would like to make sure that people are logged out once they're done working on a single, specific multi-user machine. Currently, the next user will just "switch user" and keep working, but having many users logged on at the same time with programs open seems to use up resources.

Is it possible to automatically log out users from a machine if they haven't been active for 24 hours?


Alternatively, is it possible to log out everyone but the current user at midnight?

The computer is on a Windows domain network - though I only want the auto-logout to work for the single machine (and I'm not the network admin)

.

slhck
  • 223,558
  • 70
  • 607
  • 592
Jonas
  • 717
  • 4
  • 12
  • 20

3 Answers3

6

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.

eMansipater
  • 346
  • 1
  • 7
  • @eMansipater: Would you mind explaining what this does, and where I can set the 24 hours? – Jonas Apr 11 '11 at 20:32
  • 1
    The best solution is to disable fast user switching, this will force a log off before the new user can log into their account. – Moab Apr 11 '11 at 20:52
  • 1
    @Moab disabling fast user switching means a non-admin cannot log in if the previous user has locked the screen. @Jonas A wise question-- the bit in parentheses is using the query command from support.microsoft.com/kb/186592 to find users who are disconnected by searching the phrase "disc", then logging them out. The choice command is waiting to only perform the operation every 2 minutes. There is no need for the 24 hour option since this script should only log out a user if another user has already logged in. – eMansipater Apr 11 '11 at 21:12
  • Thanks eMansipater, the ultimate solution is user training with consequences, the easier you make it for users the worse they become. – Moab Apr 11 '11 at 22:29
  • @eMansipater: Thank you very much for your explanation. If I understand correctly, old users would be logged out as soon as another user is logged in for at most two minutes with this script. This is almost equivalent to disabling fast user switching. Is it possible, instead of waiting 120s, to have the script only run at, say, midnight, where it would log out everyone but the currently active user (other than start the service at midnight and wait 24h)? – Jonas Apr 12 '11 at 01:25
  • @Moab: There are a few good uses for fast user switching: an admin can log on without the user needing to log off, or if one user goes for lunch, another can take over for an hour till the first user comes back and continues. I completely agree that training would be the best solution, but that's unfortunately not very feasible in my situation. Also, being a user on that machine myself at times, I like if things are easy for me :) – Jonas Apr 12 '11 at 01:29
  • @Jonas You could simply take the loop out of the script, then schedule it to be run every night at midnight. The key difference between the script as is and disabling fast user switching is that a user will still be able to log in if someone else has locked a computer. Would you like me to modify the code I supplied? – eMansipater Apr 12 '11 at 02:37
  • @eMansipater: Ah, yes, switching with someone else logged in is indeed useful. Removing the loop means that I only keep the first and the third line, right? – Jonas Apr 12 '11 at 21:40
  • @Jonas exactly. – eMansipater Apr 13 '11 at 10:07
  • @eMansipater: If I right-click on the .cmd file and run as admin, nothing happens. If I run the `for...`-command from the command line, I get a `%%a was unexpected at this time`. Am I doing something wrong? – Jonas Apr 13 '11 at 13:46
  • 1
    @Jonas a batch file uses "%%a", but to do the same thing at command line you type "%a" instead--that should help with debugging. I don't have a Windows machine handy to try the script on, so there might be something subtle wrong with it. You could try changing the "GTR 32 (logoff %%b)" commands to "echo logoff %%b" without the quotes, or "echo logoff %b" without the quotes if typing it in. That way it will print out the users it is trying to log off. If it prints garbage or nothing it will show the script is malformed. I'm afraid I don't know "query" output format by heart to check it. – eMansipater Apr 13 '11 at 17:21
  • @eMansipater: `for /f "tokens=1-7 delims=,: " %a in ('query user ^| find /i "disc"') do logoff %b` works as advertised when run from the command line as admin. Just out of curiosity: What do the if-clause and the GTR 32 mean that I left out? Were these important? – Jonas Apr 14 '11 at 17:57
  • @eMansipater: also, it works as a .cmd file with %%. – Jonas Apr 14 '11 at 18:05
  • 1
    @Jonas I've had a chance to doublecheck the output of the [query](http://support.microsoft.com/kb/186592) command and those parts of the script were so that the logout would only occur if the user had been idle for a set period of time. 'if %%d GTR 32' means to run the logoff command if the token %%d is greater than 32. Depending on use of remote desktop, etc. the "idle" token can appear in two places, which is why the script checks for both scenarios. Your modified version simply logs out _any_ idle user no matter how long it's been. If I can get to a windows machine I can verify syntax. – eMansipater Apr 14 '11 at 23:48
  • @eMansipater: Oh, that's why the script didn't work with GTR when I tested! I logged in as user, switched to admin, and was, of course, not idle for long enough :). Thank you very much for your help! – Jonas Apr 15 '11 at 00:05
  • @Jonas No problem! I will edit the answer to clarify both possible solutions for future reference, as soon as I can get to a Windows machine and verify the syntax for the script. – eMansipater Apr 15 '11 at 01:07
  • @eMansipater query command link not available anymore – Guntis Dec 10 '18 at 17:53
  • This solution is not workable for me as the workstation indicates the user is "Active" under state even though the workstation is locked. Is there another solution or way to force the user into disconnected state when the workstation is locked? – duct_tape_coder Jun 25 '21 at 21:16
1
for /f "tokens=1-7 delims=,: " %%a in ('query user ^| find /i "disc"') do if %%d GTR 32 (logoff %%b) else (if %%e GTR 35 (logoff %%b))

Note that the above will only work for idle minutes, you need to make a slight amendment to it if you want to use hours of idle time before logging out the disconnected session.

for /f "tokens=1-8 delims=,:/ " %%a in ('query user ^| find /i "disc"') do if %%d GTR 23 (if %%h GTR 2012 (logoff %%b))

Alter the 23 to adjust the hours, the above will work on 24hours or more of idle time. The %%h 2012 ensures the %%d value is an hour and not a minute value.

slhck
  • 223,558
  • 70
  • 607
  • 592
MOB
  • 11
  • 1
0

I would look for a way to disable the "Switch User" feature. This might help.

Wizard Prang
  • 329
  • 1
  • 6
  • I actually like the "switch user" feature: this way, somebody who runs a long calculation doesn't block other users from using the computer as well. – Jonas Mar 20 '13 at 16:58