Programming

A PowerShell Script to Add Dates to Filenames

Sometimes the easiest way to organize a folder is to put the date right in the filename.

August 15, 2026

Sometimes the easiest way to organize a folder is to put the date right in the filename.

I like dates in filenames.

More specifically, I like dates formatted as eight digits:

YYYYMMDD

August 15, 2026 becomes:

20260815

There’s no ambiguity about whether the month or day comes first, it doesn’t waste characters on punctuation, and—most importantly—it sorts properly.

So if I have a folder full of files and want them organized chronologically, I can turn this:

Invoice.pdf
Meeting Notes.txt
Report.xlsx

into this:

20260412_Invoice.pdf
20260703_Meeting Notes.txt
20260815_Report.xlsx

Sort the folder alphabetically and you’ve also sorted it chronologically.

Sometimes, though, I still want the filename to control the sort order. In that case I want the date at the end:

Invoice_20260412.pdf
Meeting Notes_20260703.txt
Report_20260815.xlsx

That’s a perfect little job for PowerShell.

The Script

The options are deliberately kept at the top. Set $DatePosition to either "prepend" or "append" and choose whether you want the file’s last-modified date or creation date.

# -----------------------------------------
# Add dates to filenames
# GeekAtoZ.com
# -----------------------------------------

# OPTIONS

# Choose "prepend" or "append"
$DatePosition = "prepend"

# Choose "LastWriteTime" or "CreationTime"
$DateSource = "LastWriteTime"

# YYYYMMDD = 20260815
$DateFormat = "yyyyMMdd"


# -----------------------------------------
# PROCESS FILES
# -----------------------------------------

Get-ChildItem -File | ForEach-Object {

    $File = $_

    # Get the selected date from the file
    $FileDate = $File.$DateSource.ToString($DateFormat)

    # Separate filename from extension
    $BaseName = $File.BaseName
    $Extension = $File.Extension

    if ($DatePosition -eq "prepend") {

        # Don't add another date if one is already at the beginning
        if ($BaseName -notmatch '^\d{8}_') {
            $NewName = "${FileDate}_${BaseName}${Extension}"
            Rename-Item -LiteralPath $File.FullName -NewName $NewName
        }

    }
    elseif ($DatePosition -eq "append") {

        # Don't add another date if one is already at the end
        if ($BaseName -notmatch '_\d{8}$') {
            $NewName = "${BaseName}_${FileDate}${Extension}"
            Rename-Item -LiteralPath $File.FullName -NewName $NewName
        }

    }
    else {
        Write-Host 'DatePosition must be "prepend" or "append".'
        break
    }
}

Prepend the Date

At the top of the script, use:

$DatePosition = "prepend"

A file named:

Vacation Photo.jpg

with a date of August 15, 2026 becomes:

20260815_Vacation Photo.jpg

This is the option I like when the date itself is important to how I want the folder organized.

Windows doesn’t need to know anything about the dates. It’s simply sorting the filenames alphabetically, and because the date is written from largest unit to smallest—year, month, day—the files naturally fall into chronological order.

Append the Date

Change one line:

$DatePosition = "append"

Now:

Vacation Photo.jpg

becomes:

Vacation Photo_20260815.jpg

This is useful when I still want files grouped alphabetically by their regular names, but I also want the date permanently visible in the filename.

Notice that the date goes before the extension, not after it.

So you get:

Vacation Photo_20260815.jpg

rather than:

Vacation Photo.jpg_20260815

The file remains a perfectly normal .jpg file.

Modified Date or Creation Date?

The script defaults to:

$DateSource = "LastWriteTime"

That’s the file’s last-modified date.

If creation date makes more sense for what you’re organizing, change it to:

$DateSource = "CreationTime"

Nothing else in the script needs to change.

Why YYYYMMDD?

You could format the date as 08-15-2026, 8_15_26, or almost anything else.

I prefer:

20260815

It’s eight characters. No spaces. No punctuation. No confusion between American and international date formats.

And it sorts correctly.

January 9, 2025:

20250109

December 31, 2025:

20251231

January 1, 2026:

20260101

Put those filenames in alphabetical order and they’re automatically in chronological order too.

A Small Script That Does One Useful Thing

There’s nothing particularly sophisticated about this script.

That’s why I like it.

If I’ve got 200 files in a folder, I don’t want to manually rename 200 files. I also don’t need an application, a database, a cloud service, or an elaborate file-management system.

I need PowerShell to look at each file, read a date, and rename it.

Sometimes that’s all programming needs to be:

Take an annoying repetitive task and make the computer do it instead.

PowerShellWindowsFile ManagementAutomation