Any command to sleep PC after x seconds?

Page 1 of 2 12 LastLast

  1. Posts : 163
    Windows 10
       #1

    Any command to sleep PC after x seconds?


    Any command to sleep PC after x seconds?

    So that, for example, I'm running some task that prevents the PC from going to sleep and then I go to bed, so I want my PC to sleep after 30 minutes.

    I found this one, but this is for shutting down after x seconds, not for sleeping:
    Shutdown -s -t x
      My Computer


  2. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #2

    Hello @Frank15,

    Frank15 said:
    Any command to sleep PC after x seconds?

    This is what I wrote and use . . .

    Code:
    
    @echo off
    :: #############################################
    :: # Title     : Put the Computer to Sleep     #
    :: # Created By: Paul Black                    #
    :: # Created On: 26-Dec-2021                   #
    :: # Filename  : Put_The_Computer_To_Sleep.bat #
    :: #############################################
    set "Title=Put the Computer to Sleep" & set "Author=Paul Black"
    title %Title% - Written by %Author%. & mode con: cols=80 lines=34
    
    setlocal EnableDelayedExpansion
    set "Params=%*"
    cd /d "%~dp0" && ( if exist "%Temp%\getadmin.vbs" del "%Temp%\getadmin.vbs") && fsutil dirty query %SystemDrive% 1>nul 2>nul || ( echo set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %Params%", "", "runas", 1 >> "%Temp%\getadmin.vbs" && "%Temp%\getadmin.vbs" && Exit /b)
    %WinDir%\system32\reg.exe query "HKU\S-1-5-19" 1>nul 2>nul || ( echo. & echo  ERROR: This Batch file MUST be run in an ELEVATED cmd prompt [ Administrator ] & echo. & echo         Right-click the Batch file and click ^<Run as Administrator^>. & echo. & echo ^>Press ANY key to EXIT . . . & pause >nul & Exit )
    
    set var_1=900
    set var_2=1800
    set var_3=2700
    set var_4=3600
    set var_5=4500
    set var_6=5400
    set var_7=6300
    set var_8=7200
    
    :Options
    
    echo. & echo  ==============================================================================
            echo  %Title%:-
            echo  ==============================================================================
    echo. & echo  Options:
    echo. & echo  [0] EXIT this Program.
    echo. & echo  [1]  15 minutes.
            echo  [2]  30 minutes.
            echo  [3]  45 minutes.
            echo  [4]  60 minutes.
    echo. & echo  [5]  75 minutes - 1 hour  15 minutes.
            echo  [6]  90 minutes - 1 hour  30 minutes.
            echo  [7] 105 minutes - 1 hour  45 minutes.
            echo  [8] 120 minutes - 2 hours -- minutes.
    echo. & echo  [9] User Defined.
    echo. & echo  ==============================================================================& echo.
    
    CHOICE /N /C 0123456789 /M ">Enter an Option number: "
    
    if %ErrorLevel%==10                  echo. & goto :User_Defined
    if %ErrorLevel%==9 set var=%var_8% & echo. & goto :Go_2_Sleep
    if %ErrorLevel%==8 set var=%var_7% & echo. & goto :Go_2_Sleep
    if %ErrorLevel%==7 set var=%var_6% & echo. & goto :Go_2_Sleep
    if %ErrorLevel%==6 set var=%var_5% & echo. & goto :Go_2_Sleep
    if %ErrorLevel%==5 set var=%var_4% & echo. & goto :Go_2_Sleep
    if %ErrorLevel%==4 set var=%var_3% & echo. & goto :Go_2_Sleep
    if %ErrorLevel%==3 set var=%var_2% & echo. & goto :Go_2_Sleep
    if %ErrorLevel%==2 set var=%var_1% & echo. & goto :Go_2_Sleep
    if %ErrorLevel%==1 Exit
    
    :Go_2_Sleep
    
    echo  The Computer is getting ready to go to Sleep . . . & echo.
          timeout %var% /nobreak >nul
          %SystemRoot%\System32\rundll32.exe powrprof.dll, SetSuspendState 0,1,0
    echo  The Computer has just Woken up. & echo.
    echo ^>Press ANY key to EXIT . . . & pause >nul & Exit
    
    :User_Defined
    
    set "time="
    set /p "time=>Enter the Time in Minutes and press <Enter>: " & echo.
    set /a  time=%time% * 60
    
    if %time% GEQ 1 (
      echo  The computer is getting ready to go to Sleep . . .
            timeout %time% /nobreak >nul
            %SystemRoot%\System32\rundll32.exe powrprof.dll, SetSuspendState 0,1,0
      echo  The Computer has just Woken up. & echo.
      echo ^>Press ANY key to EXIT . . . & pause >nul & Exit
    ) else (
      echo  ------------------------------------------------------------------------------
      echo  ERROR: You MUST enter the number of MINUTES.
      echo  ------------------------------------------------------------------------------& echo.
      echo ^>Press ANY key to return to the Options . . . & pause >nul & cls & goto :Options
    )
    
    

    Adjust the var's [ Seconds ] and Options: text as necessary.

    I hope this helps.
    Last edited by Paul Black; 17 Feb 2022 at 11:44.
      My Computer


  3. Posts : 372
    Windows 10 Home: 21H1 (OS Build 19043.1586)
       #3

    Paul Black said:
    Hello @Frank15,




    This is what I wrote and use . . .

    Code:
    
    @echo off
    :: #############################################
    :: # Title     : Put the Computer to Sleep     #
    :: # Created By: Paul Black                    #
    :: # Created On: 26-Dec-2021                   #
    :: # Filename  : Put_The_Computer_To_Sleep.bat #
    :: #############################################
    set "Title=Put the Computer to Sleep" & set "Author=Paul Black"
    title %Title% - Written by %Author%. & mode con: cols=80 lines=34
    
    setlocal EnableDelayedExpansion
    set "Params=%*"
    cd /d "%~dp0" && ( if exist "%Temp%\getadmin.vbs" del "%Temp%\getadmin.vbs") && fsutil dirty query %SystemDrive% 1>nul 2>nul || ( echo set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %Params%", "", "runas", 1 >> "%Temp%\getadmin.vbs" && "%Temp%\getadmin.vbs" && Exit /b)
    %WinDir%\system32\reg.exe query "HKU\S-1-5-19" 1>nul 2>nul || ( echo. & echo  ERROR: This Batch file MUST be run in an ELEVATED cmd prompt [ Administrator ] & echo. & echo         Right-click the Batch file and click ^<Run as Administrator^>. & echo. & echo ^>Press ANY key to EXIT . . . & pause >nul & Exit )
    
    set var_1=900
    set var_2=1800
    set var_3=2700
    set var_4=3600
    set var_5=4500
    set var_6=5400
    set var_7=6300
    set var_8=7200
    
    :Options
    
    echo. & echo  ==============================================================================
            echo  %Title%:-
            echo  ==============================================================================
    echo. & echo  Options:
    echo. & echo  [0] EXIT this Program.
    echo. & echo  [1]  15 minutes.
            echo  [2]  30 minutes.
            echo  [3]  45 minutes.
            echo  [4]  60 minutes.
    echo. & echo  [5]  75 minutes - 1 hour  15 minutes.
            echo  [6]  90 minutes - 1 hour  30 minutes.
            echo  [7] 105 minutes - 1 hour  45 minutes.
            echo  [8] 120 minutes - 2 hours -- minutes.
    echo. & echo  [9] User Defined.
    echo. & echo  ==============================================================================& echo.
    
    CHOICE /N /C 0123456789 /M ">Enter an Option number: "
    
    if %ErrorLevel%==10                  echo. & goto :User_Defined
    if %ErrorLevel%==9 set var=%var_8% & echo. & goto :Go_2_Sleep
    if %ErrorLevel%==8 set var=%var_7% & echo. & goto :Go_2_Sleep
    if %ErrorLevel%==7 set var=%var_6% & echo. & goto :Go_2_Sleep
    if %ErrorLevel%==6 set var=%var_5% & echo. & goto :Go_2_Sleep
    if %ErrorLevel%==5 set var=%var_4% & echo. & goto :Go_2_Sleep
    if %ErrorLevel%==4 set var=%var_3% & echo. & goto :Go_2_Sleep
    if %ErrorLevel%==3 set var=%var_2% & echo. & goto :Go_2_Sleep
    if %ErrorLevel%==2 set var=%var_1% & echo. & goto :Go_2_Sleep
    if %ErrorLevel%==1 Exit
    
    :Go_2_Sleep
    
    echo  The Computer is getting ready to go to Sleep . . . & echo.
          timeout %var% /nobreak >nul
          %SystemRoot%\System32\rundll32.exe powrprof.dll, SetSuspendState 0,1,0
    echo  The Computer has just Woken up. & echo.
    echo ^>Press ANY key to EXIT . . . & pause >nul & Exit
    
    :User_Defined
    
    set "time="
    set /p "time=>Enter the Time in Minutes and press <Enter>: " & echo.
    set /a  time=%time% * 60
    
    if %time% GEQ 1 (
      echo  The computer is getting ready to go to Sleep . . .
            timeout %time% /nobreak >nul
            %SystemRoot%\System32\rundll32.exe powrprof.dll, SetSuspendState 0,1,0
      echo  The Computer has just Woken up. & echo.
      echo ^>Press ANY key to EXIT . . . & pause >nul & Exit
    ) else (
      echo  ------------------------------------------------------------------------------
      echo  ERROR: You MUST enter the number of MINUTES.
      echo  ------------------------------------------------------------------------------& echo.
      echo ^>Press ANY key to return to the Options . . . & pause >nul & cls & goto :Options
    
    

    Adjust the var's [ Seconds ] and Options: text as necessary.

    I hope this helps.
    That's a tidy script Paul. Great job.

    Christophe
      My Computers


  4. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #4

    Hello @CMArbres,

    CMArbres said:
    That's a tidy script Paul. Great job.
    Thank you, it is appreciated.
      My Computer


  5. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #5

    Hello @CMArbres,

    CMArbres said:
    That's a tidy script Paul. Great job.
    Thank you, it is appreciated.
      My Computer


  6. Posts : 16,949
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #6

    Frank15 said:
    Any command to sleep PC after x seconds?
    So that, for example, I'm running some task that prevents the PC from going to sleep and then I go to bed, so I want my PC to sleep after 30 minutes.
    Frank,

    That's what the setting in Control panel, Power options is for.
    Also available at Settings, System, Power & sleep.

    Denis
      My Computer


  7. Posts : 163
    Windows 10
    Thread Starter
       #7

    Paul Black said:
    Hello @Frank15,




    This is what I wrote and use . . .

    Code:
    
    @echo off
    :: #############################################
    :: # Title     : Put the Computer to Sleep     #
    :: # Created By: Paul Black                    #
    :: # Created On: 26-Dec-2021                   #
    :: # Filename  : Put_The_Computer_To_Sleep.bat #
    :: #############################################
    set "Title=Put the Computer to Sleep" & set "Author=Paul Black"
    title %Title% - Written by %Author%. & mode con: cols=80 lines=34
    
    setlocal EnableDelayedExpansion
    set "Params=%*"
    cd /d "%~dp0" && ( if exist "%Temp%\getadmin.vbs" del "%Temp%\getadmin.vbs") && fsutil dirty query %SystemDrive% 1>nul 2>nul || ( echo set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %Params%", "", "runas", 1 >> "%Temp%\getadmin.vbs" && "%Temp%\getadmin.vbs" && Exit /b)
    %WinDir%\system32\reg.exe query "HKU\S-1-5-19" 1>nul 2>nul || ( echo. & echo  ERROR: This Batch file MUST be run in an ELEVATED cmd prompt [ Administrator ] & echo. & echo         Right-click the Batch file and click ^<Run as Administrator^>. & echo. & echo ^>Press ANY key to EXIT . . . & pause >nul & Exit )
    
    set var_1=900
    set var_2=1800
    set var_3=2700
    set var_4=3600
    set var_5=4500
    set var_6=5400
    set var_7=6300
    set var_8=7200
    
    :Options
    
    echo. & echo  ==============================================================================
            echo  %Title%:-
            echo  ==============================================================================
    echo. & echo  Options:
    echo. & echo  [0] EXIT this Program.
    echo. & echo  [1]  15 minutes.
            echo  [2]  30 minutes.
            echo  [3]  45 minutes.
            echo  [4]  60 minutes.
    echo. & echo  [5]  75 minutes - 1 hour  15 minutes.
            echo  [6]  90 minutes - 1 hour  30 minutes.
            echo  [7] 105 minutes - 1 hour  45 minutes.
            echo  [8] 120 minutes - 2 hours -- minutes.
    echo. & echo  [9] User Defined.
    echo. & echo  ==============================================================================& echo.
    
    CHOICE /N /C 0123456789 /M ">Enter an Option number: "
    
    if %ErrorLevel%==10                  echo. & goto :User_Defined
    if %ErrorLevel%==9 set var=%var_8% & echo. & goto :Go_2_Sleep
    if %ErrorLevel%==8 set var=%var_7% & echo. & goto :Go_2_Sleep
    if %ErrorLevel%==7 set var=%var_6% & echo. & goto :Go_2_Sleep
    if %ErrorLevel%==6 set var=%var_5% & echo. & goto :Go_2_Sleep
    if %ErrorLevel%==5 set var=%var_4% & echo. & goto :Go_2_Sleep
    if %ErrorLevel%==4 set var=%var_3% & echo. & goto :Go_2_Sleep
    if %ErrorLevel%==3 set var=%var_2% & echo. & goto :Go_2_Sleep
    if %ErrorLevel%==2 set var=%var_1% & echo. & goto :Go_2_Sleep
    if %ErrorLevel%==1 Exit
    
    :Go_2_Sleep
    
    echo  The Computer is getting ready to go to Sleep . . . & echo.
          timeout %var% /nobreak >nul
          %SystemRoot%\System32\rundll32.exe powrprof.dll, SetSuspendState 0,1,0
    echo  The Computer has just Woken up. & echo.
    echo ^>Press ANY key to EXIT . . . & pause >nul & Exit
    
    :User_Defined
    
    set "time="
    set /p "time=>Enter the Time in Minutes and press <Enter>: " & echo.
    set /a  time=%time% * 60
    
    if %time% GEQ 1 (
      echo  The computer is getting ready to go to Sleep . . .
            timeout %time% /nobreak >nul
            %SystemRoot%\System32\rundll32.exe powrprof.dll, SetSuspendState 0,1,0
      echo  The Computer has just Woken up. & echo.
      echo ^>Press ANY key to EXIT . . . & pause >nul & Exit
    ) else (
      echo  ------------------------------------------------------------------------------
      echo  ERROR: You MUST enter the number of MINUTES.
      echo  ------------------------------------------------------------------------------& echo.
      echo ^>Press ANY key to return to the Options . . . & pause >nul & cls & goto :Options
    
    

    Adjust the var's [ Seconds ] and Options: text as necessary.

    I hope this helps.
    Super thanks. This is hibernating instead of sleeping though, but that's what great anyway.
      My Computer


  8. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #8

    Hello @Frank15,

    Frank15 said:
    Super thanks.
    You are VERY welcome.
      My Computer


  9. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #9

    Hello @Frank15,

    Frank15 said:
    This is hibernating instead of sleeping though, but that's what great anyway.

    The command rundll32.exe powrprof.dll, SetSuspendState 0,1,0 for Sleep is correct. However, it will Hibernate instead of Sleep if you have NOT turned Hibernate Off.

    I hope this helps.
      My Computer


  10. Posts : 163
    Windows 10
    Thread Starter
       #10

    Paul Black said:
    Hello @Frank15,




    The command rundll32.exe powrprof.dll,SetSuspendState 0,1,0 for Seep is correct. However, it will Hibernate instead of Sleep if you do NOT turn the Hibernate Off.

    I hope this helps.
    Thank you, good to know. Hibernating is fine for me now anyway.
    However, when I choose option [9] User defined and enter 1 (meaning 1 minute) it doesn't work, should I use a different notation?

    This is what I get:
    >Enter an Option number: 9

    >Enter the Time in Minutes and press <Enter>: 1

    C:\Windows\System32>
    ..then it does nothing after 1 minute...
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 07:07.
Find Us




Windows 10 Forums