How can I logoff and go to stand-by via Win XP script?

4

3

I am looking for a way to write a script (of any sort) that would both log me off my local Windows account and also immediatedly put the computer to sleep / stand-by. I've found solutions that can do one or the other but not logoff and then sleep all in one script. The SHUTDOWN command doesn't offer this and I can't find any other options either.

This is for Win XP, SP3 right now but someday soon I'll want a Windows 7 solution as well.

Thanks for any ideas or an explanation as to why this isn't possible. :)

Peter

Posted 2010-03-21T18:25:42.440

Reputation: 290

Answers

2

Take a look at PsShutdown. Should do exactly what you want.

KutscheraIT

Posted 2010-03-21T18:25:42.440

Reputation: 940

And how do you suggest we use PsShutdown for that purpose? – AnT – 2013-03-26T17:08:19.847

5

This batch file is GREAT! I have added a few tweaks to make it work with Windows XP and also minimized the amount of time to wait for the Standby scheduled task. Scheduled tasks only run every minute, so I set the maximum wait time to 1 min, 15 sec. Now I can Logout and Standby with a simple click of a batch file icon!

Here is how I set it up, step by step:

  1. Create a directory "C:\MyProg"

  2. Create and save a text file "C:\MyProg\Standby.bat"

     rem * Standby *  
     rundll32.exe powrprof.dll,SetSuspendState
    
  3. Create and save a text file "C:\MyProg\Logoff-Standby.bat"

     rem * Logoff & Standby *  
     rem  
     rem from http://superuser.com/questions/122432/how-can-i-logoff-and-go-to-stand-by-via-win-xp-script
    
     rem  
     rem * Find time to schedule the standby task *  
     rem  
     rem Get current time hours, minutes, and seconds  
     set curtime=%time%  
     set curhh=%curtime:~0,2%  
     set curhh=%curhh: =%  
     set curmm=%curtime:~3,2%  
     set curmm=%curmm: =%  
     set curss=%curtime:~6,2%  
     set curss=%curss: =%  
     rem Set values for near seconds, minutes, and hours  
     rem Scheduled tasks can only run at a new minute, so bump the time up by 1 min, 15 sec and then round down to zero seconds
     set /a nearss=%curss% + 15  
     set /a nearmm=%curmm% + 1  
     set /a nearhh=%curhh%  
     if %nearss% gtr 59 (  
         set /a nearss=%nearss% - 60  
         set /a nearmm=%nearmm% + 1  
     )  
     if %nearmm% gtr 59 (  
         set /a nearmm=%nearmm% - 60  
         set /a nearhh=%nearhh% + 1  
     )  
     if %nearhh% gtr 23 set /a nearhh=%nearhh% - 24  
     rem Add leading zero to minutes and hours  
     if %nearmm% lss 10 set nearmm=0%nearmm%  
     if %nearhh% lss 10 set nearhh=0%nearhh%  
     rem Set neartime to result rounded down to zero seconds  
     set neartime=%nearhh%:%nearmm%:00  
     rem  
     rem * Schedule task to standby *  
     rem  
     schtasks /create /tn "standby" /tr C:\MyProg\standby.bat /sc once /st %neartime% /ru System  
     rem  
     rem * Logout *  
     rem  
     shutdown -l -f
    
  4. Right click on C:\MyProg\Logoff-Standby.bat > Send To > Desktop (create shortcut)

Fred

Posted 2010-03-21T18:25:42.440

Reputation: 51

it's often better to simply comment on someone else's answer rather than posting one of your own with some adjustments. But since you don't have enough rep to comment yet, you're forgiven for now ;-) – Ivo Flipse – 2011-01-24T20:45:58.653

1Yes, you are right; I do not have a comment button. I wanted to be sure that anyone who searched could find the same answer I did with a few fixes. The main fix is the use of the "shutdown -l" command rather than "rundll32 user32.dll,ExitWindowsEx". I think the original solution is ingenious, however. I would have never figured that out myself, and I could not find any other solutions to this that were even close. – Fred – 2011-01-24T22:58:37.133

One more fix is needed. When scheduling a task, the hours must be 2 digits with a leading zero if it is earlier than 10:00a. I had to add this line: if %nearhh% lss 10 set nearhh=0%nearhh% I added this to my answer. – Fred – 2011-01-25T17:05:10.977

beautiful solution. i'll copy :) – kokbira – 2011-05-10T20:15:05.670

i always used a for to extract hours, minutes and seconds from %time%. but where in bat you get current time? you forgot %time%? – kokbira – 2011-05-10T20:30:21.330

3

Create a batch file,

schtasks /create /tn "sleep" /tr c:\scripts\sleep.bat /sc once /st %neartime% /ru System
rundll32 user32.dll,ExitWindowsEx

the first line creates a scheduled task that runs sleep.bat as system user 1 minute from now. The second line logs you of.

In the sleep.bat file you need to add the following line which will send the PC to sleep.

rundll32.exe powrprof.dll,SetSuspendState

In the first batch file you need to work out what time it will be 1 minute from now (%neartime%)

set curtime=%time%
set curhh=%curtime:~0,2%
set curhh=%curhh: =%
set curmm=%curtime:~3,2%
set curmm=%curmm: =%
if "%curmm:~0,1%" == "0" set curmm=%curmm:~1,1%
set /a nearmm=%curmm% + 2
set /a nearhh=%curhh%
if %nearmm% gtr 59 (
    set /a nearmm=nearmm% - 60
    SET /A nearhh=%nearhh% + 1
)
if %nearmm% gtr 59 if %nearhh% gtr 23 set /a nearhh=%nearhh% - 24
if %nearmm% lss 10 set nearmm=0%nearmm%
set neartime=%nearhh%:%nearmm%

This wont be instant but it will be automated.

Alternatively you could set you pc to lock when resuming from sleep, and then the sleep command on its own will lock the station (although this doesnt log you off, its close!)

Charles Gargent

Posted 2010-03-21T18:25:42.440

Reputation: 703

Wow, this looks really excellent. I will try this out ASAP. Did you already have this worked out or did you just figure it out for me? :) – Peter – 2010-03-22T04:32:07.337

The %neartime% I had already, the rest is a guess, I have never actually tried sleeping the PC from the system account. – Charles Gargent – 2010-03-22T07:40:53.140

see also "at" command – kokbira – 2011-05-10T20:14:15.513