Windows 7 Command Line Timed Log Off

4

0

This command immediately logs off the user:

shutdown /l /f

/l = This option will immediately log off the current user on the current machine. You can not use the /l option with the /m option to log off a remote computer. The /d, /t, and /c options are also not available with /l.

As I can't use /t /c switches, I can't time the log off.

Note: It should logoff even after closing the command.

Any alternate ways?

user243868

Posted 2013-08-13T07:19:50.920

Reputation: 51

timeout /T 10 && logoff doesn't work? – gregg – 2018-02-19T14:01:46.213

1you could write a batch file using ping 192.0.2.2 -n 1 -w 10000 > nul as a delay then shutdown /l /f or do you have restriction on they why it needs to be performed? – 50-3 – 2013-08-13T07:44:18.190

1On Windows 7, you could also use timeout 10, however I need this logoff to occur even after the batch/command finishes. – user243868 – 2013-08-13T07:46:33.120

Answers

2

You can create a scheduled task using schtasks:

schtasks /create /st 09:50 /sc once /tr logoff /tn LogOff

This would create a new scheduled task at 09:50, which is run once. It will execute logoff (logoff should do the same as shutdown /l /f) and the task will be named "LogOff".

Der Hochstapler

Posted 2013-08-13T07:19:50.920

Reputation: 77 228

Looks promising. How do I set it 10 seconds from now? – user243868 – 2013-08-13T07:54:07.170

@user243868: I'm not sure if schtasks directly supports that, it probably doesn't. As it looks, schtasks doesn't even accept time values with seconds in the ST parameter. – Der Hochstapler – 2013-08-13T08:00:54.093

@user243868: Does that work? Whenever I gave schtasks a time with seconds (as in 01:23:45), it would tell me the time is in the past. – Der Hochstapler – 2013-08-13T08:26:25.713

Had to change it a bit but I think I got it. Will submit the possible solution. – user243868 – 2013-08-13T08:44:31.390

Nope I can't submit my own solution. – user243868 – 2013-08-13T08:51:27.983

You can edit your question and add the solution. And accept @oliver's answer if it helped. – Rik – 2013-08-13T09:02:38.433

@user243868: You might have to wait a while to be able to do that, because you're a new user. – Der Hochstapler – 2013-08-13T11:55:39.677

Ok I added my solution to original post. – user243868 – 2013-08-14T04:34:14.957

@user243868: Don't add solutions to the question. Just post your own answer and mark it as the solution. – Der Hochstapler – 2013-08-14T06:54:25.820

Alright now I can add my solution, but I cant accept it as the answer?? Have to wait 22hours. How crazy. – user243868 – 2013-08-14T08:23:29.173

@user243868: There are waiting periods for new users to avoid that people increase their reputation by creating a bunch of accounts. – Der Hochstapler – 2013-08-14T08:26:54.703

1

Edit: Full Solution. However schtasks does not support scheduling in the next 10 seconds. The soonest next schedule is when its a new minute.

@echo on

for /F "tokens=1-3 delims=:." %%a in ("%time%") do (
set Hour=%%a
set Minute=%%b
set Seconds=%%c
)

set /A newTime=(Hour*3600) + (Minute*60) + (Seconds + 60)
set /A Hour=newTime/3600
set /A Minute=(newTime %% 3600) / 60
set /A Seconds=(newTime %% 3600) %% 60

if %Hour% gtr 23 (set Hour=0) ELSE (IF %Hour% lss 10 set Hour=0%Hour%)
IF %Minute% lss 10 set Minute=0%Minute%
IF %Seconds% lss 10 set Seconds=0%Seconds%

Set TaskTime=%Hour%:%Minute%:%Seconds%
Echo %Time%
Echo %TaskTime%

schtasks /delete /tn "LogOff" /f

schtasks /create /st %TaskTime% /sc once /tr logoff /tn "LogOff"

user243868

Posted 2013-08-13T07:19:50.920

Reputation: 51

0

You can make an AutoIt script to delay running the command:

; Initialize variables
$second = 1000

; Script options
Opt("TrayAutoPause", 0) ; 0 = no pause, 1 = pause
Opt("TrayIconHide", 1)  ; 0 = show, 1 = hide tray icon

; Optionally, sleep for a number of seconds before starting the process
If 0 < $CmdLine[0] Then
    Sleep( $CmdLine[1] * $second )
EndIf

Run("shutdown /l /f")

After you compile the script into a program, you can call it from a batch file:

@echo off
start "" DelayLogoff.exe 10
pause

Tip: You could just put this whole thing in a batch file, but using a hidden AutoIt script avoids the problems of the otherwise very nice Windows Timeout program.

JonathanDavidArndt

Posted 2013-08-13T07:19:50.920

Reputation: 890

0

Couldn't find anything on google searches so....

I did this with a Vista/Server 2008R2 GPO: Create and edit a GPO via GPMC on a 2008R2 or later DC. Goto Computer/Preferences/Control Panel/Scheduled Tasks. Schedule two tasks, one for 15 min. warning, one for logoff. For each assign the task to be run by Domain Users (Check groups to enable). Run only when user is logged in. Run with highest priority. Schedule the message task 15 min.s prior to the shutdown task. Respective Actions are Take action: Display a Message and Take Action: Run a Program: C:\windows\system32\logoff.exe.

Make sure the message is set to run first.

Regards.

Drew

Posted 2013-08-13T07:19:50.920

Reputation: 1

please fix the formatting of your answer .... – Pierre.Vriens – 2018-02-19T15:17:34.430