Skip to main content

How to Get the Week Number of the Year in a Batch Script

A common requirement for organizational and reporting scripts is to know the current week number of the year. You might need this to create a weekly backup folder (e.g., Backup_2023-Week43), to generate a weekly report with a standardized name, or for any task that operates on a weekly schedule.

This is a task that is impossible to do reliably with pure batch script commands. Batch has no native date-aware functions to handle the complex rules of calendar weeks (like leap years and different start days). The only robust solution is to delegate this calculation to a more powerful scripting language that is built into Windows: PowerShell.

This guide will teach you the modern, standard, and highly recommended method for getting the week number by using a PowerShell one-liner from within your batch script.

The Core Challenge: Batch Has No Calendar Logic

You cannot calculate the week number with simple SET /A math. The rules are complex:

  • Which day does the week start on (Sunday or Monday)?
  • Does "Week 1" start on January 1st, or on the first Sunday/Monday of the year?
  • How are leap years handled?

A pure batch script to handle all this would be extraordinarily complex and error-prone. It is not a recommended approach.

PowerShell has a powerful Get-Date cmdlet that can format dates in almost any way imaginable, including different standards for week numbers. We can call this command from our batch script and capture its output.

Syntax: powershell -Command "Get-Date -UFormat %%V"

This is a single, self-contained command that will return the current week number.

The PowerShell Command Explained (-UFormat %%V)

  • powershell -Command "...": Executes the PowerShell command from cmd.exe.
  • Get-Date: The PowerShell cmdlet for getting the current date and time.
  • -UFormat: This switch tells Get-Date to format the output using Unix/strftime-compatible codes.
  • %%V: This is the most critical part. The %V code is the format specifier for the ISO 8601 week number (where the week starts on a Monday, and Week 1 is the first week with at least four days in the new year).

The Double Percent (%%): You must use a double percent sign (%%V) in your batch script. This is because a single % is used for batch variable expansion. The %% "escapes" the character, telling the batch parser to pass a single %V to PowerShell. If you use a single %V, the batch script will try to find a variable named V and your command will fail.

The Script: Capturing the Week Number

To use the week number in your script, you must wrap the PowerShell command in a FOR /F loop to capture its output into a variable.

@ECHO OFF
SET "WeekNumber="

ECHO --- Getting the current week number of the year ---
ECHO.

FOR /F %%W IN (
'powershell -NoProfile -ExecutionPolicy Bypass -Command "Get-Date -UFormat %%V"'
) DO (
SET "WeekNumber=%%W"
)

IF DEFINED WeekNumber (
ECHO The current week number is: %WeekNumber%
) ELSE (
ECHO Could not determine the week number.
)

Common Pitfalls and How to Solve Them

  • Forgetting the Double Percent (%%): This is the number one mistake. Using Get-Date -UFormat %V will fail. Solution: Always use %%V inside a batch script.

  • Different Week Numbering Standards: The ISO 8601 standard (%V) is the most common, but your region might use a different one.

    • %%V: ISO 8601 week number (Monday is the first day of the week).
    • %%U: Week number with Sunday as the first day of the week.
    • %%W: Week number with Monday as the first day of the week (slightly different rule from ISO).
    • Solution: Choose the format specifier that matches your requirement. For consistency, %V is recommended.
  • PowerShell Execution Policy: On a highly restricted system, the PowerShell command could be blocked. Solution: As shown in the script, using the -ExecutionPolicy Bypass switch is a robust way to ensure your command runs.

Practical Example: Creating a Weekly Backup Folder

This is a classic use case. The script creates a uniquely named folder for the current week's backups, like Backup_2023-Week43.

@ECHO OFF
SETLOCAL

ECHO --- Weekly Backup Folder Setup ---
ECHO.

REM --- Step 1: Get the current year ---
FOR /F "tokens=2 delims==" %%Y IN ('WMIC OS GET LocalDateTime /VALUE') DO SET "dt=%%Y"
SET "CurrentYear=%dt:~0,4%"

REM --- Step 2: Get the current week number ---
SET "WeekNumber="
FOR /F %%W IN ('powershell -Command "Get-Date -UFormat %%V"') DO SET "WeekNumber=%%W"

REM --- Step 3: Pad the week number with a leading zero if needed ---
IF %WeekNumber% LSS 10 SET "WeekNumber=0%WeekNumber%"

REM --- Step 4: Construct the folder name ---
SET "BackupFolder=E:\Backups\Backup_%CurrentYear%-Week%WeekNumber%"

ECHO The backup folder for this week will be:
ECHO "%BackupFolder%"
ECHO.

IF NOT EXIST "%BackupFolder%\" (
MKDIR "%BackupFolder%"
ECHO [SUCCESS] Folder created.
) ELSE (
ECHO [INFO] Folder already exists.
)

ENDLOCAL

Conclusion

Calculating the week number is a task that clearly shows the limitations of pure batch scripting and the power of leveraging modern, built-in tools.

  • A pure-batch solution is not recommended as it is unreliable and extremely complex.
  • The PowerShell Get-Date -UFormat method is the modern, robust, and highly recommended approach. It is fast, accurate, and handles all the complex calendar logic for you.
  • The most critical part of the syntax is using a double percent sign (%%V) to escape the format specifier within the batch script.