Learn how to use PowerShell Rename-Item to rename individual files and perform bulk rename operations.
Learn how to rename files using PowerShell. This guide covers the basics and a few advanced techniques, including syntax, common examples, the Registry, and even using a PowerToy.
The only prerequisites are PowerShell 5.1 or PowerShell 7 or later, and administrator permissions with your logged-in account.
The Rename-Item cmdlet is the most common cmdlet used with this function in the command-line world. It renames an item in a PowerShell provider namespace – this can include a filesystem, Registry key, or something similar. The cmdlet changes the name of a specified item, but does not affect the content of the item.
This is the basic syntax and common parameters of Rename-Item.
Rename-Item
[-Path] <String>
[-NewName] <String>
[-Force]
[-PassThru]
[-Credential <PSCredential>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Let’s start by going through some common examples of using Rename-Item to rename files and then some.
Quick Note! You can’t use Rename-Item to move an item, using a new path and filename. For that purpose, you could use Move-Item.
Here is a simple example of how to rename a file using PowerShell Rename-Item:
Rename-Item -Path "c:\users\mreinders\Downloads\Secure_File_Copy.ps1" -NewName "Secure_File_Copy_01.ps1"
Rename-Item is another example of a cmdlet that will give no output if things go well. You can enable more verbose switches, but we’re good for now. As a side note – if you do NOT give an absolute path in the -Path section, PowerShell will assume you mean in the current directory.
Let’s use the -PassThru parameter to allow us to pipe the results to another cmdlet like -Get-Item so we can get the full details of said file.
Rename-Item -Path "c:\users\mreinders\downloads\Secure_File_Copy_01.ps1" -NewName "Secure_File_Copy_02.ps1" -PassThru | Get-Item
Here we can see the details of the newly renamed file, now ending with an ‘_02’ sequence.
We can validate what will happen without actually doing anything with the ‘-WhatIf’ parameter. Let me show you.
Rename-Item -Path "c:\users\mreinders\downloads\Secure_File_Copy_02.ps1" -NewName "Secure_File_Copy_03.ps1" -WhatIf
This will show us in full detail what the command would do if you didn’t use the ‘-WhatIf’ parameter. It shows the original file and its location, and the new file name in the explicit destination location. Pretty helpful.
You can also use the ‘-Confirm’ parameter to ask PowerShell to prompt you before making the proposed change.
And another quick note on moving files. Here is a simple example of moving our beloved .PS1 file to a different directory with Move-Item.
Move-Item -Path "c:\users\mreinders\downloads\Secure_File_Copy_02.ps1" -Destination "c:\temp\Secure_File_Copy_03.ps1"
We moved and renamed the file in one fell swoop. Piece of cake.
Let me give you a quick example of renaming a Registry key on my VM. This is a good backup procedure you can use before you modify any keys or folders in the Registry.
Rename-Item -Path "HKLM:\Software\Mozilla" -NewName "Mozilla.old"
This is rather simple, but, as always, be careful in the Registry!
Let’s get a little deeper into the weeds and work on renaming multiple files with Rename-Item. We can first use Get-ChildItem to gather filtered files in a directory, then ask Rename-Item to change the extension to something else.
Get-ChildItem -Path "c:\Backup_Files\Attachments\*.pdf" | Rename-Item -NewName { $_.Name -replace ".pdf",".pdfold" }
As we can see, all the PDF files were renamed to PDFOLD. Let’s not dwell on why you would do this, but I’m sure you catch my drift with what you can do.
There are a variety of use cases to add tiemstamps to append to the beginning of filenames, or at the end. We can take this same directory of files as above, and add the creation timestamp at the end of each filename.
Get-ChildItem -Path "c:\Backup_Files\Attachments\" | ForEach-Object {
$timestamp = Get-Date -Format "ddMMyyyy_HHmmss"
$newname = $_.BaseName + "_" + $timestamp + $_.Extension
Rename-Item -Path $_.FullName -NewName $newname
}
So, in this example, it gets the point across. It’s not the prettiest way to maintain the files, but, moving forward you can now manage these files in a new way – being able to to decipher when the files were create just by their filenames.
If you’re interacting with directories and/or file names with special characters, you must use the -LiteralPath parameter like this.
Rename-Item -LiteralPath "c:\Scripts\old{script01}.txt" -NewName "Script01.txt"
I would like to include a few more advanced tools and ideas and some common troubleshooting you’ll likely run into when using these cmdlets. Let’s start with a very cool toy from PowerToys.
You have probably heard of PowerToys, open-source tools that Microsoft has been developing for many years, starting in earlier versions of Windows. The current release for Windows 11 is getting oh so close to the “1.0” final release. One of the very helpful toys for this post is called PowerToys PowerRename.
Here, I have a folder of icons ending in PNG. I want to bulk rename them all so the files end in JPG. I can use PowerRename like so.
I just typed in what to find that is unique in all the files, and then what I want to replace the same string with. You can see a nice preview before you click Apply. This has very powerful potential, especially if you want to rename thousands of log files, for example, in a folder a certain way. Very slick indeed.
You can also use a wildcard character, a specific file extension, a prefix, and even include recurse directories.
Most likely, when you come across any type of Access Denied error messages, you have insufficient permission to make the proposed change to files or folders in your specified location. One very quick tip is to run PowerShell in an Administrator session. That will solve 80+% of your permission issues when using PowerShell.
If you get a ‘File In Use’ error message, you need to make sure whatever application may still have that file open, is closed. You may need to engage the Task Manager to forcefully close any app associated with the file(s) in use.
As a side note, there is another PowerToy that could help you at least identify the app or process holding your file ransom. Look up ‘File Locksmith’ to learn more.
If you run into an error that closely resembles ‘The specified path is too long…’, you have run into a limitation in Windows. The default maximum path length limit is generally around 260 characters or so. You’ll need to shorten the path by either moving the files to a directory higher up on your volume/drive, or rename some of the parent folders above your location. But use caution as this has the potential to break some applications, etc.
Thank you for reading my latest post on renaming files with PowerShell. Please leave a comment or question below if you’d like.