Batch file to shutdown system one day a week, hibernate it other days


  1. Posts : 6
    Windows 10 Pro
       #1

    Batch file to shutdown system one day a week, hibernate it other days


    Version 22H2.

    I need to properly code a batch file to shutdown my system one day a week (I've chosen Wednesdays), and hibernate it other days.

    I am aware of "shutdown /h" and "shutdown /s /hybrid" but want to know how to create the variable for the day of the week (numeric or string), and format the "if" command to run when the variable matches.
      My Computer


  2. Posts : 755
    Windows 7
       #2

    Code:
    @echo off
    
    for /f "tokens=1" %%i IN ('date /t') do set TODAY=%%i
    if %TODAY% == Wed (
       echo Wednesday
    ) else (
       echo Not Wednesday
    )
      My Computer


  3. Posts : 31,568
    10 Home x64 (22H2) (10 Pro on 2nd pc)
       #3

    latimer said:
    Version 22H2.

    I need to properly code a batch file to shutdown my system one day a week (I've chosen Wednesdays), and hibernate it other days.

    I am aware of "shutdown /h" and "shutdown /s /hybrid" but want to know how to create the variable for the day of the week (numeric or string), and format the "if" command to run when the variable matches.
    The problem with using the DATE command is that what you get can vary depending on the regional date format your system uses. Instead of using DATE try this:
    Here's the correct answer for a BAT script on a modern system:

    @echo off
    for /f %%i in ('powershell ^(get-date^).DayOfWeek') do set dow=%%i
    echo %dow%

    Outputs:

    Saturday
    Finding day of week in batch file? (Windows Server 2008) - Server Fault
      My Computers


  4. Posts : 16,892
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #4

    I suggest you just use Task scheduler to run the required commands on the required days.

    Denis
      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 18:35.
Find Us




Windows 10 Forums