Recently, I got a requirement to rename numerous report files in a project. PowerShell script is the best option for it. In this tutorial, I will explain how to rename multiple files using PowerShell.
Rename Multiple Files Using PowerShell
Recently, I was working on a project for a client in New York City. They had a folder containing thousands of files named using an old naming convention. The files followed the format “NYC_Report_YYYYMMDD.txt”. My task was to update the naming convention to include a prefix indicating the department and change the file extension from “.txt” to “.log”.
For example, I needed to rename files like this:
- “NYC_Report_20220101.txt” to “Finance_NYC_Report_20220101.log”
- “NYC_Report_20220102.txt” to “Finance_NYC_Report_20220102.log” And so on…
Manually renaming each file would have taken hours, if not days. That’s when I thought of using a PowerShell script for it.
Check out How to Check if a File Exists and Rename it Using PowerShell?
Rename Multiple Files using Rename-Item Cmdlet
PowerShell provides the Rename-Item
cmdlet, which allows you to rename files and directories. With a few lines of code, you can rename multiple files based on specific patterns or criteria.
First, open a PowerShell window and navigate to the directory containing the files you want to rename. Use the Set-Location
cmdlet followed by the directory path. For example:
Set-Location -Path "C:\NYCReports"
To rename the files, use the Rename-Item
cmdlet along with the -NewName
parameter. You can specify the new name using a script block {}
that allows you to dynamically generate the new file name based on the original name.
Here’s the PowerShell command I used to rename the files:
Get-ChildItem -Filter "NYC_Report_*.txt" | Rename-Item -NewName { "Finance_" + $_.Name -replace '\.txt$', '.log' }
Let me explain to you how the command works:
Get-ChildItem -Filter "NYC_Report_*.txt"
: This retrieves all files in the current directory that match the pattern “NYC_Report_*.txt”. The asterisk*
acts as a wildcard, matching any characters.|
: The pipeline operator passes the retrieved files to the next cmdlet.Rename-Item -NewName { ... }
: This renames each file based on the script block inside{}
."Finance_" + $_.Name
: This prepends the prefix “Finance_” to the original file name.$_
represents the current file object in the pipeline.-replace '\.txt$', '.log'
: This replaces the file extension “.txt” with “.log”. The$
symbol ensures that only the extension at the end of the file name is replaced.
After running this command, all the matching files in the directory will be renamed according to the specified pattern.
I executed the above PowerShell cmdlet and you can check the exact output in the screenshot below:
To verify that the files were renamed successfully, you can use the Get-ChildItem
cmdlet again:
Get-ChildItem -Filter "Finance_NYC_Report_*.log"
This command will display all the files that match the new naming pattern “Finance_NYC_Report_*.log”.
Let me show you another example.
In this example, I will show you how to rename all .txt
files in a directory to have a .log
extension using PowerShell.
Check out How to Download a File from URL Using PowerShell?
PowerShell Rename Multiple Files Add Prefix
Let me show you now how to rename multiple files and add prefix to it using PowerShell. In PowerShell it is easy to do, let me show you how.
For example, let’s say I have a folder with files named “Report_20240101.txt”, “Report_20240102.txt”, etc., and I want to add the prefix “Sales_” to each file name.
Here’s the PowerShell command to accomplish this:
Get-ChildItem -Path "C:/MyFolder" -Filter "Report_*.txt" | ForEach-Object {
try {
$newName = "Sales_" + $_.Name
Rename-Item -Path $_.FullName -NewName $newName
} catch {
Write-Host "Error renaming file: $($_.FullName) - $($_.Exception.Message)"
}
}
This command retrieves all files matching the pattern “Report_*.txt” and renames them by prepending the prefix “Sales_” to the original file name.
After running this command, the files will be renamed to “Sales_Report_20240101.txt”, “Sales_Report_20240102.txt”, and so on.
Here is the output you can see in the screenshot below:
Let me show you another example of renaming multiple files using PowerShell.
Let’s say you want to add the prefix USA_
to all files in the C:\Reports
directory.
Here is the PowerShell script.
Get-ChildItem -Path "C:\Reports" | ForEach-Object {
$newName = "USA_" + $_.Name
Rename-Item -Path $_.FullName -NewName $newName
}
Here:
- Get-ChildItem: Lists all files in
C:\Reports
. - ForEach-Object: Iterates through each file.
- “USA_” + $_.Name: Adds the prefix
USA_
to the file name. - Rename-Item: Renames the file with the new name.
Read How To Check If File Modified In Last 24 Hours Using PowerShell?
Rename Multiple Files Based on Date in PowerShell
Sometimes, you might want to rename files based on their creation date. This is particularly useful for organizing files chronologically.
Let me show you an example.
Let’s rename files in C:\Reports
to include their creation date in the format YYYYMMDD
.
Get-ChildItem -Path "C:\Reports" | ForEach-Object {
$date = $_.CreationTime.ToString("yyyyMMdd")
$newName = "$date$($_.Extension)"
Rename-Item -Path $_.FullName -NewName $newName
}
Here is how the script works.
- Get-ChildItem: Lists all files in
C:\Reports
. - ForEach-Object: Iterates through each file.
- $_ .CreationTime.ToString(“yyyyMMdd”): Extracts the creation date in
YYYYMMDD
format. - “$date$($_.Extension)”: Combines the date with the file extension.
- Rename-Item: Renames the file with the new name.
Read How to Create a File in PowerShell if it Doesn’t Exist?
Rename Multiple Files Sequentially using PowerShell
I will explain here how to rename multiple files sequentially using PowerShell in a specific order.
Let’s rename all files in C:\Reports
to Report001
, Report002
, etc.
Here is the complete PowerShell script.
$counter = 1
Get-ChildItem -Path "C:\Reports" | Sort-Object LastWriteTime | ForEach-Object {
$newName = "Report" + $counter.ToString("000") + $_.Extension
Rename-Item -Path $_.FullName -NewName $newName
$counter++
}
Here is how the script works:
- $counter = 1: Initializes a counter.
- Get-ChildItem: Lists all files in
C:\Reports
. - Sort-Object LastWriteTime: Sorts files by their last write time.
- ForEach-Object: Iterates through each file.
- “Report” + $counter.ToString(“000”) + $_.Extension: Creates a new name with a sequential number.
- Rename-Item: Renames the file with the new name.
- $counter++: Increments the counter.
Combine Prefix and Date in File Names
You can combine different elements, such as adding both a prefix and a date to file names. Let’s add the prefix USA_
and the creation date to files in C:\Reports
.
Here is an example of combine prefix and date using PowerShell.
Get-ChildItem -Path "C:\Reports" | ForEach-Object {
$date = $_.CreationTime.ToString("yyyyMMdd")
$newName = "USA_" + $date + $_.Extension
Rename-Item -Path $_.FullName -NewName $newName
}
Here is how the PowerShell script works.
- Get-ChildItem: Lists all files in
C:\Reports
. - ForEach-Object: Iterates through each file.
- $_.CreationTime.ToString(“yyyyMMdd”): Extracts the creation date in
YYYYMMDD
format. - “USA_” + $date + $_.Extension: Combines the prefix and date with the file extension.
- Rename-Item: Renames the file with the new name.
Read How to Create a Log File using PowerShell?
Handle Errors and Exceptions While Renaming Multiple Files
When working with file renaming, it’s essential to handle potential errors gracefully. For instance, if a file with the new name already exists, PowerShell will throw an error.
Here is the complete PowerShell script.
Get-ChildItem -Path "C:\Reports" | ForEach-Object {
try {
$newName = "USA_" + $_.Name
Rename-Item -Path $_.FullName -NewName $newName -ErrorAction Stop
} catch {
Write-Host "Failed to rename $($_.Name): $($_.Exception.Message)"
}
}
Here is how the PowerShell script works.
- try: Attempts to rename the file.
- catch: Catches any errors that occur.
- Write-Host: Outputs a message if renaming fails.
Conclusion
I hope this tutorial has helped you understand how to rename multiple files using PowerShell. I explained how to change file extensions, add prefixes, rename files based on dates, or rename files sequentially using PowerShell.
You may also like:
- PowerShell Copy-Item with Folder Structure
- PowerShell Copy-Item Cmdlet to Copy Files and Folders
- Get-ChildItem Sort By Date in PowerShell
- How to Rename Folders to Uppercase or Lowercase Using PowerShell?
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com