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.
To use (expand) environment variables in the registry, the value must be of the type REG_EXPAND_SZ
.
Based on this question, it would be easiest to add that kind of value with the reg
command rather than a .reg
file..
reg add <KEY> /v <NAME> /t REG_EXPAND_SZ /d <DATA>
See reg add /?
for more information on this syntax.
You could also encode the data as hex. There's some examples of that here:
REG_EXPAND_SZ
data must be presented as what MS calls a binary data
type (subtype "2"), so the value must be formatted in a kind of
hexidecimal format, comma-delimited, two tokens per byte (padded with
zeros), with a terminating null byte of course (and further
explanation is obviously far beyond the scope of this article, and the
author won't be held responsible for anyone's misuse of the incomplete
information given thus far). So the line above will not work but
indicates the end result I wanted to achieve. As a real working .REG
-file entry the example above must be rendered as:
"SoMeThIng"="%WINDIR%\\system32"
becomes
"SoMeThIng"=hex(2):22,25,57,49,4e,44,49,52,25,5c,5c,73,79,73,74,65,6d,33,32,22,00
Best Answer
The
-h
switch is used to shut down the computer on Linux, not Windows. The correct command to shut down a Windows computer after 7 hours is:Windows will show a dialog box with a countdown until the time the computer will shut down.
But, you want to hibernate, not shutdown, and unfortunately, the
/h
and the/t
switch don't work together. As a workaround, you can use theat
command to scheduleshutdown /h
to run at a certain time. For example, it is 3:00pm in my time zone at present, so 10 hours later would be 1:00am. To schedule it to hibernate then, I would run:It uses 24-hour time notation, so if you wanted it to hibernate at 1:00pm, you'd run:
Please note, that while you don't need administrator permissions to run the
shutdown
command on default Windows installations, you do need them for theat
command.