How to Add Date to Filename Using PowerShell?

One of my team members was working on some files in PowerShell, and they had to add the current date to the file name. I suggested a few methods. In this tutorial, I will explain how to add the date to a filename using PowerShell with examples.

To add a date to a filename during file creation using PowerShell, you can use the Get-Date cmdlet to retrieve the current date and format it. Here’s a simple script: $Date = Get-Date -Format "yyyyMMdd"; $FilePath = "C:\MyFolder\file_$Date.txt"; New-Item -Path $FilePath -ItemType "File". This script formats the current date as yyyyMMdd, appends it to the filename, and creates a new file with the updated name.

Add Date to Filename Using PowerShell

PowerShell provides the Get-Date cmdlet, which retrieves the current date and time. You can format this date and append it to your filenames. Here’s a basic syntax:

$Date = Get-Date -Format "yyyyMMdd"
Rename-Item "C:\MyFolder\file.txt" "C:\MyFolder\file_$Date.txt"

In this example, Get-Date -Format "yyyyMMdd" formats the date as 20240921, which is then appended to the filename.

Let me show you a few other methods for adding current date to file names in PowerShell.

Method 1: Add Date to Filename During File Creation

If you are creating a new file and want to include the current date in its name, you can use the following PowerShell script:

$Date = Get-Date -Format "yyyyMMdd"
$FilePath = "C:\MyFolder\file_$Date.txt"
New-Item -Path $FilePath -ItemType "File"

This script creates a new file with the current date appended to its name. This method ensures that every new file you create will have the current date appended to its name.

I executed the above script, and you can see the output in the screenshot below:

Add Date to Filename Using PowerShell

Read Get the Last Friday of the Month Using PowerShell

Method 2: Rename Existing Files

To rename existing files and add the current date, you can use the Rename-Item cmdlet in PowerShell.

Here is an example and the complete PowerShell script.

$Date = Get-Date -Format "yyyyMMdd"
$OriginalFilePath = "C:\MyFolder\file.txt"
$NewFilePath = "C:\MyFolder\file_$Date.txt"
Rename-Item -Path $OriginalFilePath -NewName $NewFilePath

This script renames the existing file by appending the current date.

You can see the exact output in the screenshot below:

How to Add Date to Filename Using PowerShell

Method 3: Add Date and Time to Filename

Sometimes, you might need to add both date and time to the filename for more granularity. Here’s how you can do it:

$DateTime = Get-Date -Format "yyyyMMdd_HHmmss"
$OriginalFilePath = "C:\MyFolder\file.txt"
$NewFilePath = "C:\MyFolder\file_$DateTime.txt"
Rename-Item -Path $OriginalFilePath -NewName $NewFilePath

This script appends the current date and time, formatted as 20240921_153045, to the filename.

You can see the exact output in the screenshot below:

PowerShell Add Date to Filename

Read PowerShell: Get the Last Day of the Previous Month

Method 4: Add Date to Filename After a Specific Character

If your filenames follow a specific pattern and you want to insert the date after a particular character, you can use the following approach:

$Date = Get-Date -Format "yyyyMMdd"
$OriginalFilePath = "C:\MyFolder\file_name.txt"
$FileName = [System.IO.Path]::GetFileNameWithoutExtension($OriginalFilePath)
$Extension = [System.IO.Path]::GetExtension($OriginalFilePath)
$NewFileName = $FileName.Insert($FileName.IndexOf("_") + 1, $Date)
$NewFilePath = "C:\MyFolder\$NewFileName$Extension"
Rename-Item -Path $OriginalFilePath -NewName $NewFilePath

This script inserts the date immediately after the first underscore in the filename.

You can see the output in the screenshot below:

PowerShell Add Date to Filename examples

Conclusion

Using PowerShell, you can add a date to file names to manage and organize them. In this tutorial, I explained how to add the current date to file names while creating new files, renaming existing ones, or inserting dates at specific positions.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.