How to Rename Files with Date in PowerShell?

Recently, I got a requirement to rename a file by appending the current date in PowerShell. I tried different methods, and in this PowerShell tutorial, I will explain to you how to rename files with date in PowerShell using various methods.

To rename a file in PowerShell and append the current date, you can use the Rename-Item cmdlet in combination with Get-Date. Here’s a concise example:

Get-ChildItem "C:\MyFolder\file.txt" | Rename-Item -NewName {$_.BaseName + (Get-Date -Format "yyyyMMdd") + $_.Extension}

This command will change the file name to include the current date in the format yyyyMMdd, for instance, file20240214.txt for February 14, 2024.

Method 1: Using Rename-Item Cmdlet

The simplest way to rename a file with the current date in PowerShell is to use the Rename-Item cmdlet. Here’s a basic example:

Get-ChildItem "C:\MyFolder\file.txt" | Rename-Item -NewName {$_.BaseName + (Get-Date -Format "yyyyMMdd") + $_.Extension}

This script will rename file.txt to file20240507.txt (assuming today’s date is May 7, 2024). The Get-Date -Format cmdlet is used to generate the date string.

You can see the output in the screenshot below after I executed the script using VS code.

Rename Files with Date in PowerShell

Method 2: Using a Custom Function

You can also create a custom function that can be reused to rename any file with the current date in PowerShell.

Here is the complete script.

function Rename-FileWithDate {
    param (
        [string]$Path,
        [string]$DateFormat = "yyyyMMdd"
    )
    Get-ChildItem $Path | Rename-Item -NewName {$_.BaseName + (Get-Date -Format $DateFormat) + $_.Extension}
}

# Usage
Rename-FileWithDate -Path "C:\MyFolder\file.txt"

This function allows you to specify the file path and an optional date format.

Method 3: Append Date and Time

Sometimes, you might want to include the time as well as the date while renaming a file in PowerShell. Here’s how you can modify the script to append both date and time:

Get-ChildItem "C:\MyFolder\file.txt" | Rename-Item -NewName {$_.BaseName + (Get-Date -Format "yyyyMMdd-HHmmss") + $_.Extension}

This will rename your file to something like file20070514-153045.txt, including the time in a 24-hour format.

Method 4: Renaming Multiple Files in a Directory

If you need to rename all files in a directory, you can loop through each file using the Get-ChildItem PowerShell cmdlet and then rename it. Here is the complete PowerShell script.

Get-ChildItem "C:\MyFolder\*.txt" | ForEach-Object {
    $newName = $_.BaseName + (Get-Date -Format "yyyyMMdd") + $_.Extension
    Rename-Item $_.FullName -NewName $newName
}

This script will rename every .txt file in the specified directory, appending the current date to each file name.

Method 5: Preserving Original File Name Structure

To keep the original file name intact and simply append the date, you can adjust the script like this:

Get-ChildItem "C:\MyFolder\file.txt" | Rename-Item -NewName {$_ -replace $_.Name, ($_.BaseName + "_" + (Get-Date -Format "yyyyMMdd") + $_.Extension)}

In this example, the file file.txt would be renamed to file_20240214.txt.

Rename with Regular Expressions

For more complex renaming scenarios, you might need to use regular expressions. Here’s an example that renames files with a specific pattern:

Get-ChildItem "C:\MyFolder\*.txt" | ForEach-Object {
    $newName = $_.Name -replace 'pattern', (Get-Date -Format "yyyyMMdd")
    Rename-Item $_.FullName -NewName $newName
}

Replace ‘pattern’ with the actual pattern you’re trying to match in the file names.

Handling Errors and Exceptions

When renaming files, you may encounter errors due to permissions, file locks, or other issues. To handle these type of scenarios, you can add error handling to your script:

Get-ChildItem "C:\MyFolder\*.txt" | ForEach-Object {
    try {
        $newName = $_.BaseName + (Get-Date -Format "yyyyMMdd") + $_.Extension
        Rename-Item $_.FullName -NewName $newName -ErrorAction Stop
    } catch {
        Write-Error "Error renaming file $_.Name: $_"
    }
}

This script will stop on any error and output a message to help you troubleshoot.

Conclusion

In this PowerShell tutorial, I have explained in detail how to rename files with date in PowerShell using various methods. The simplest way you can do this is by using the Rename-Item PowerShell Cmdlet.

You may also like: