Home » How To » How to Rename Files with PowerShell

How to Rename Files with PowerShell

Renaming files is a simple task on Windows. All it takes is a couple of clicks and you are done. However, if you are looking for a more flexible approach, you can use the built-in PowerShell command line tool to rename files however you want. For example, you can rename a single file or multiple files, add time stamps, add custom prefixes or suffixes, etc. Let’s get started.

Note: The steps below are tested to work on Windows 11, 10, and 8.

How to Open PowerShell on Windows

Open the Start menu, search for Windows PowerShell, and click on the top result to open PowerShell.

Note: If the files require admin privileges to rename, select the Run as Administrator option in the Start menu to open PowerShell with admin rights.

open PowerShell as admin

Rename a File Using the PowerShell Command

To rename a file using PowerShell, we can use the Rename-Item cmdlet with -Path and -NewName flags. Here’s how.

In the PowerShell window, run the following command while replacing the example path with the actual path of the file.

Rename-Item -Path "C:\example\oldfilename.txt" -NewName "newfilename.txt"

PowerShell command to rename a specific file

As soon as you run the command, PowerShell renames the file to the new name.

PowerShell Command to Rename Multiple Files at Once

We can use the same Rename-Item cmdlet to rename multiple files. There are two main variations for renaming multiple files. The first command lets you replace a specific text within the file name, which is useful for files sharing a common naming scheme. The second command lets you completely rename all the files in a directory to any name you want with a sequential number added at the end.

Replacing Specific Text in File Names

When you need to rename a bunch of files that all have a similar name style, there might be a need to replace or update a specific part of the name. In that case, you can run the following command.

In the command, replace C:\example with your folder’s path, *.txt with the file extension of your files, oldtext with the text you want to change, and newtext with the new text.

Get-ChildItem -Path "C:\example" -Filter "*.txt" | Rename-Item -NewName {$_.Name -replace 'oldtext','newtext'}

PowerShell command to replace a specific text in files

As soon as you run the command, PowerShell updates the specific text in all the matching files in the given directory. If a file doesn’t have the specified text, PowerShell will not rename it.

renamed a specific text in the file names

Sequential Renaming Multiple Files

If you simply want to rename all the files in a directory with increasing sequential numbering, run the following command. The result will be the same as when you do it via File Explorer.

Before running the command, replace C:\example with your folder’s path, *.txt with the file extension of your files, and newname with the new name you want to assign.

$script:index = 0; Get-ChildItem -Path "C:\example" -Filter "*.txt" | Rename-Item -NewName { $script:index++; "newname" + " " + $script:index + $_.Extension }

PowerShell command to add sequential numbers at the end of file names

As soon as you execute the command, PowerShell will rename all the files in the directory to the new name and add sequential numbering starting from 1 at the end of the file name.

file renamed with sequential numbers at the end

PowerShell Command to Add Timestamps to File Names

You can also add timestamps at the end of the filename while renaming with PowerShell. This command is especially useful when you want to organize files by date and time.

To add a timestamp at the end of the file names, run the following command. As before, replace C:\example with your folder path and *.txt with the file extension.

Get-ChildItem -Path "C:\example" -Filter "*.txt" | Rename-Item -NewName {$_.BaseName + "_" + (Get-Date -Format "yyyy-MM-dd") + $_.Extension}

PowerShell command to add timestamp to file names
file renamed with timestamp at the end of file names

Note: If you want to add hours, minutes, and seconds to the timestamp, modify the command like this.

Get-ChildItem -Path "C:\example" -Filter "*.txt" | Rename-Item -NewName {$_.BaseName + "_" + (Get-Date -Format "yyyy-MM-dd_HH-mm-ss") + $_.Extension}

file renamed with timestamp with hours minutes and seconds at the end

As soon as you run the command, PowerShell will add the timestamp to all the files in the directory.

Adding Prefixes or Suffixes to File Names Using PowerShell

You can also add custom prefixes (at the start of the file name) and suffixes (at the end of the file name) while renaming. This method is particularly useful for things like organizational clarity, version control, tagging, and more.

To add a prefix, run the following command. Replace the C:\example path with the folder path, and newprefix with the custom prefix you want to use.

Get-ChildItem -Path "C:\example" -Filter "*" | Rename-Item -NewName {"newprefix_" + $_.Name}

To add a suffix, run the following command. Replace the C:\example path with the folder path and newsuffix with the custom suffix you want to use.

Get-ChildItem -Path "C:\example" -Filter "*" | Rename-Item -NewName { $_.BaseName + "_suffix" + $_.Extension }

And there you have it! It’s that easy to rename files using PowerShell. Whether it’s renaming a single file, handling multiple files at once, adding custom prefixes/suffixes, or embedding timestamps, I’ve shown the commands you can run. Use them as you need.

Related tutorials:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top