Skip to main content

How to Get the Day of the Week in Batch Script

In scripting and automation, you often need to perform tasks that depend on the day of the week. You might want to run a full backup only on a Friday, perform a cleanup task only on weekends, or simply include the day's name in a log file for better readability. While Windows Batch has a simple trick for this, it is famously unreliable across different computers.

This guide will teach you the classic "pure-batch" method using the %DATE% variable and explain its critical flaws. More importantly, it will demonstrate the vastly superior and modern approach using a PowerShell one-liner, which is the only recommended method for creating reliable and portable scripts.

The Challenge: The %DATE% Variable is Unreliable

The core problem is that the format of the %DATE% environment variable is not fixed. It changes based on the user's regional and language settings in Windows.

  • US English: Fri 10/27/2023 (The day is at the beginning)
  • UK English: 27/10/2023 (The day is not present at all)
  • Other formats: 2023-10-27 Fri (The day is at the end)

A script that assumes the day is always the first three characters (%DATE:~0,3%) will work on one machine and fail completely on another.

The Classic Method (Fragile): Slicing the %DATE% Variable

This method should only be used for quick, personal scripts where you are certain of the date format. It uses substring extraction to "slice" the first three characters from the %DATE% variable.

The Syntax (for US English format only)

SET "DayOfWeek=%DATE:~0,3%"

The correct and professional way to handle dates is to call PowerShell. Its Get-Date cmdlet returns a rich date-time object, from which we can reliably get the day of the week in any format we want, regardless of the system's regional settings.

Syntax: powershell -Command "(Get-Date).DayOfWeek"

This command will always return the full English name of the day (e.g., Friday). It is consistent, reliable, and portable.

Basic Example: Getting the Day's Name

Let's see both methods in action.

Method 1: Pure Batch Script (Unreliable)

@ECHO OFF
REM This only works on a system with a US English date format.
SET "DayName=%DATE:~0,3%"
ECHO Batch Method: Today is %DayName%.

Method 2: PowerShell Script (Reliable)

@ECHO OFF
FOR /F %%D IN ('powershell -Command "(Get-Date).DayOfWeek"') DO (
SET "DayName=%%D"
)
ECHO PowerShell Method: Today is %DayName%.

Getting the Day as a Number (Sunday=0, Monday=1, etc.)

For scripting logic, such as in a SWITCH/CASE structure, it's often easier to work with a number. PowerShell can provide this directly.

@ECHO OFF
FOR /F %%N IN ('powershell -Command "(Get-Date).DayOfWeek.value__"') DO (
SET "DayNumber=%%N"
)

ECHO The day of the week as a number is: %DayNumber%
ECHO (Sunday=0, Monday=1, Tuesday=2, ...)

This is extremely useful for conditional logic.

Common Pitfalls and How to Solve Them

The single biggest pitfall is relying on the %DATE% variable. A script written on a US machine will fail when run on a European machine. There is no simple batch workaround for this.

Solution: Do not use the %DATE% substring method in any script that needs to be portable or reliable. The PowerShell method is the only robust solution, as it is not affected by regional settings and always returns predictable output.

Practical Example: A "Friday Backup" Script

This is a classic use case. The script performs a quick, incremental backup every day, but on Fridays, it performs a full backup. This logic requires a reliable way to know what day it is.

@ECHO OFF
SETLOCAL
ECHO --- Daily Backup Script ---

REM --- Use the reliable PowerShell method to get the day of the week ---
SET "Today="
FOR /F %%D IN ('powershell -NoProfile -Command "(Get-Date).DayOfWeek"') DO (
SET "Today=%%D"
)

ECHO.
ECHO Today is %Today%.

IF /I "%Today%"=="Friday" (
ECHO It's Friday! Running the FULL backup...
REM (Your Robocopy command for a full backup would go here)
REM robocopy "C:\Source" "E:\Backups\Full" /MIR
) ELSE (
ECHO Running the INCREMENTAL backup...
REM (Your Robocopy command for an incremental backup would go here)
REM robocopy "C:\Source" "E:\Backups\Incremental" /XO
)

ECHO.
ECHO --- Backup complete ---
ENDLOCAL

Conclusion

While batch scripting offers a simple-looking trick to get the day of the week, it is a trap that can make your scripts fail unexpectedly on different systems.

  • The %DATE:~0,3% substring method is fragile and should only be used for personal scripts on a machine where you control the date format.
  • The PowerShell (Get-Date).DayOfWeek method is the overwhelmingly recommended best practice. It is reliable, portable, and immune to changes in the system's regional settings.

For any script that relies on knowing the day of the week, the PowerShell one-liner is the professional and correct choice.