Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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)

.

share|improve this question
1  
Is this a Windows domain network? –  Moab Apr 11 '11 at 20:17
    
@Moab: 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). –  Jonas Apr 11 '11 at 20:25
    
@ Jonas, Your choice of words makes it sound like more than one PC, you might edit your original post to include this new information. –  Moab Apr 11 '11 at 20:48
    
@Moab: I have tried to clarify the question. Is that better? –  Jonas Apr 12 '11 at 0:13
    
It is now, have a look. –  Moab Apr 12 '11 at 2:11
show 1 more comment

3 Answers

up vote 4 down vote accepted

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.

share|improve this answer
    
@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
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
1  
@Jonas I've had a chance to doublecheck the output of the query 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
show 11 more comments
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.

share|improve this answer
add comment

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

share|improve this answer
    
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
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.