Home » How to Rename File with Date in PowerShell

How to Rename File with Date in PowerShell

PowerShell built-in cmdlet Rename-Item in combination with the Get-Date cmdlet to rename a file with a date.

The following methods can be utilized rename a file with the date in PowerShell.

Method 1: Rename a file with the current date/time in PowerShell

$newFileName = "paywall_" + (Get-Date -Format "yyyy-MM-dd") + ".txt"
Rename-Item -Path $original_file -NewName $newFileName

This example adds the date to filename $original_file by using the Rename-Item cmdlet.

The following example shows how to use this method.

Rename a File with a Date in PowerShell

To rename a file with the current date/time stamp in PowerShell, you can use the Rename-Item cmdlet in combination with the Get-Date cmdlet.

The following example shows how to use it with syntax.

# original file name that you want to rename
$original_file = "C:\temp\log\paywall.txt"

# add the current date timestamp to the original file name
$newFileName = $original_file + (Get-Date -Format "yyyy-MM-dd_HH-mm-ss") + ".txt"

# rename the original file name with new file name
Rename-Item -Path $original_file -NewName $newFileName

The output of the above PowerShell script adds the current date timestamp to filename $original_file.

PowerShell rename file with date
PowerShell rename file with current date timestamp

In this PowerShell script, the $original_file variable stores the file name. Using the Get-Date cmdlet, we append the current date time to the $original_file name and store it in $newFileName.

Finally, the Rename-Item cmdlet renames a file with the date.

Conclusion

I hope the above article on how to rename a file with a date in PowerShell using the Rename-Item cmdlet is helpful to you.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.