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 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. :)

share|improve this question
add comment

3 Answers 3

up vote 2 down vote accepted

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

share|improve this answer
    
And how do you suggest we use PsShutdown for that purpose? –  AndreyT Mar 26 '13 at 17:08
add comment

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)

share|improve this answer
    
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 Jan 24 '11 at 20:45
1  
Yes, 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 Jan 24 '11 at 22:58
    
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 Jan 25 '11 at 17:05
    
beautiful solution. i'll copy :) –  kokbira May 10 '11 at 20:15
    
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 May 10 '11 at 20:30
add comment

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!)

share|improve this answer
    
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 Mar 22 '10 at 4:32
    
The %neartime% I had already, the rest is a guess, I have never actually tried sleeping the PC from the system account. –  Charles Gargent Mar 22 '10 at 7:40
    
see also "at" command –  kokbira May 10 '11 at 20:14
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.