Introduction to PowerShell Rename Folder
Rename-Item [-Path*] <String> [-NewName*] <String> [-Confirm] [-Credential <PSCredential>] [-Force] [-PassThru] [-UseTransaction] [-WhatIf] [<CommonParameters>]
Quick Links
PowerShell makes renaming files from the command line super easy. You can use specific names, add increasing numbers, add prefixes and suffixes, and even append timestamps. We’ll show you how to do that all on your Windows 11 or Windows 10 computer.
Learn How to Find a File or Folder’s Full Path
To rename files, you’ll have to provide PowerShell with your file or parent folder’s full path. If you know how to do this, skip to the relevant section below. If you aren’t quite sure how to get a folder or file’s full path, we’ll show you.
Open a File Explorer window and find your file or folder. Press and hold down the Shift key on your keyboard, right-click your file or folder, and choose “Copy as Path.”
That action has copied your selected item’s full path to your clipboard. You can now paste this path wherever required in the commands below.

Renaming a folder or file is a routine task we often need to perform while working with files and folders. Manual folder renaming might seem like a quick task, but it can be tedious and time-consuming to manually rename folders and directories, especially if you have numerous folders or have to perform the task regularly. That’s why I turned to PowerShell!
PowerShell provides a simple and efficient way to rename folders. In this article, we will discuss different methods to rename a folder using PowerShell. We will cover how to rename a single folder, rename multiple folders using wildcards, and rename folders using a regular expression.
Using Substring Method to Rename Folders using PowerShell
Another way to rename folders is by using the Substring method. The Substring method returns a specified number of characters from a string. Let’s consider an example where we want to rename a folder with a name longer than a specified length. In this case, we can use the substring method to select the first n characters of the folder name. The command to rename the folder would be:
Get-ChildItem -Path "C:\Test" -Directory | Where-Object { $_.Name.Length -gt 10 } | Rename-Item -NewName { $_.Name.Substring(0, 10) }In this command, we are using the Get-ChildItem cmdlet to get all items in the directory specified by the path “C:\Test”. We then used the cmdlet to select only the folders with names longer than 10 characters. Finally, we use the Rename-Item cmdlet with pipeline input to rename the selected folders by selecting the first ten characters of the folder name.
Often you may want to use the Select-Object cmdlet in PowerShell to select a specific property of an object and then rename the property in the output.
This particular example selects the team and points properties from the $my_data object.
It then renames the points property to TotalPoints in the output.
Suppose we use the Import-Csv cmdlet to import this CSV file into PowerShell:

The $my_data object has three properties: team, points and assists.
Suppose that we would like to select the team and points columns and rename the points column to TotalPoints in the output.

Notice that this returns the team column along with the points column renamed as TotalPoints.

Notice that this returns the same results as the previous example.
PowerShell: How to Use Select-Object to Only Get Property Value
PowerShell: How to Use Group-Object with Multiple Properties
PowerShell: How to Use Sort-Object with Multiple Properties
I’m trying to rename all files in all subfolders to prefix it with the folder name
So that all files in all subdirectories under d:\ are renamed to be .
| Existing folder | Existing Filename | New Filename |
|---|---|---|
| d:\2023_09_28 Sydney | my doc.doc | d:\2023_09_28 Sydney\2023_09_28 Sydney my doc.doc |
| d:\2023_09_28 Sydney | my file.doc | d:\2023_09_28 Sydney\2023_09_28 Sydney my file.doc |
| d:\2023_09_29 Brisbane | my doc.doc | d:\2023_09_29 Brisbane\2023_09_29 Brisbane my doc.doc |
| d:\2023_09_29 Brisbane | my file.doc | d:\2023_09_29 Brisbane\2023_09_29 Brisbane my file.doc |
I can rename all files in a folder, but can’t work out how to do this for all subfolders.
This is what I am currently using to rename the files in the current folder:
But it is giving me errors and seems to be renaming the filename multiple times.
I would really appreciate it if someone could provide me with advise on how to do this. Thanks!
asked Sep 29, 2023 at 4:01
Your approach using PowerShell is close, but you need to modify it slightly to avoid renaming files multiple times.
$Path = "D:\"
# Get all files recursively
Get-ChildItem -Path $Path -File -Recurse | ForEach-Object { $newName = Join-Path $_.Directory.FullName ($_.Directory.Name + " " + $_.Name) Rename-Item -Path $_.FullName -NewName $newName
}$Path is set to the root directory, which is “D:” in your case. You can change this path to match your specific directory.
Get-ChildItem -Path $Path -File -Recurse retrieves all files recursively under the specified directory.
Inside the loop, we create a new name for the file by combining the directory’s name and the original file name using Join-Path.
Finally, we use Rename-Item to rename the file with the new name.
This script will do the job. 😀
answered Sep 29, 2023 at 8:38
You need to either capture the result of the Get-ChildItem cmdlet in a variable first and loop over that, or enclose the Get-ChildItem part in between brackets so it will gather the files to a finish before looping over the results.
Especially, since you do not use a -Filter here to find and rename only specific files, without capturing or bracketizing Get-ChildItem will happily enumerate newly renamed files again and again..
Try capturing first:
$allFiles = Get-ChildItem -Path $Path -File -Recurse
$allFiles | Rename-Item -NewName { '{0} {1}' -f $_.Directory.Name, $_.Name }Or surround the enumeration of files witch brackets
(Get-ChildItem -Path $Path -File -Recurse) | Rename-Item -NewName { '{0} {1}' -f $_.Directory.Name, $_.Name }answered Oct 1, 2023 at 14:05
8 gold badges26 silver badges43 bronze badges
How to Add Timestamps to All Files in a Folder
Get-ChildItem PATH -Recurse -Include "*.*" | ForEach-Object { Rename-Item -Path $_.FullName -NewName "$($_.DirectoryName)\$($_.BaseName)_$(Get-Date -F yyyy-MM-dd_HH-mm)$($_.Extension)"}
The above command uses your computer’s system time. If that time is incorrect, it’s possible to change the timezone on your Windows 11 and Windows 10 PC.
And that’s how you assign new names one file at a time or in bulk on your Windows system, without using File Explorer. It’s just one of the many ways that PowerShell aids with mundane Windows tasks.
Renaming folders if they already exist in PowerShell
Another common challenge when renaming folders with PowerShell is dealing with duplicates. If you try to rename a folder to an existing name, PowerShell will throw an error, “Rename-Item : Cannot create a file when that file already exists.”. This can be frustrating, especially if you’re renaming a large number of folders.
To avoid this issue, you can use the Test-Path cmdlet to check if a folder already exists before attempting to rename it. If the folder exists, you can add a suffix to the new name to avoid duplicates.
Here’s an example of how to rename folders if they exist in PowerShell:
#Parameters
$OldFolder = "C:\Projects\Platinum"
$NewFolder ="C:\Projects\Platinum71k"
$counter = 0
$FinalFolder= $NewFolder
#Check if the New Folder Already Exits
while (Test-Path $finalFolder) { $counter++ #Append ($Counter) to the Folder Name $FinalFolder = "$NewFolder ($counter)"
}
#Rename Folder
Rename-Item -Path $OldFolder -NewName (Split-Path -Path $FinalFolder -Leaf)In this example, we’re using the Test-Path cmdlet to check if a folder with the new name already exists. If it does, we’re appending a number to the new name to avoid duplicates. For example, if a folder named “Platinum71k” already exists, we’ll rename the new folder to “Platinum71k (1)”.
Renaming Multiple Folders using Wildcards
The Rename-Item cmdlet is great for renaming folders one at a time, but what if you have hundreds or thousands of folders to rename? In that case, you’ll need a more automated approach. One way to automate folder renaming is to use conditional statements and wildcards. Conditional statements allow you to specify conditions that must be met before the folder is renamed. The wildcard character * matches any character or string of characters. For example, you can use a conditional statement to rename all folders that contain a specific keyword in their name.
Here’s an example of how to use conditional statements to rename folders containing a specific keyword:
Get-ChildItem -Path "C:\Projects" -Directory |
ForEach-Object { if ($_.Name -match "keyword") { Rename-Item -Path $_.FullName -NewName ($_.Name -replace "keyword", "new_keyword") }
}In this example, we’re using the Get-ChildItem cmdlet to retrieve a list of all the directories in the “C:\Projects” folder. We’re then using the ForEach-Object cmdlet to iterate through each directory, and the conditional statement to check if the directory name contains the keyword “keyword”. If it does, we’re using the Rename-Item cmdlet to replace “keyword” with “new_keyword”.
Let’s rename each folder from a given folder by adding prefixes to them!
#Parameters
$FolderPath ="C:\Projects"
$Prefix ="Archive_"
#Rename each folder by adding a Prefix to its name
Get-ChildItem -Path $FolderPath -Directory | ForEach-Object { Rename-Item -Path $_.FullName -NewName ($Prefix + $_.Name)
}Now, let’s consider an example where we want to rename all folders in a directory that contain the word “Test”. The command to rename all such folders to “Test1” would be:
Get-ChildItem -Path "C:\Test\*" -Directory | Where-Object { $_.Name -like "*Test*" } | Rename-Item -NewName { $_.Name -replace "Test", "Test1" }In this command, we use the Get-ChildItem cmdlet to get all items in the directory specified by the path “C:\Test*”. We then use the Where-Object cmdlet to select only the folders that contain the word “Test”. Then, we use the replace operator to set the new name of the item. Finally, we use the Rename-Item cmdlet to rename the selected folders to “Test1”.
How to Add a Prefix or Suffix to a File Name
Get-ChildItem PATH | Rename-Item -NewName {"PREFIX" + $_.Name}Get-ChildItem PATH | Rename-Item -NewName {$_.BaseName + "SUFFIX" + $_.Extension}Renaming Folders using Regular Expressions
A regular expression is a pattern that describes a set of strings. You can use regular expressions to select and manipulate text in PowerShell. Let’s consider an example where we want to rename folders that have a specific pattern in their names, say: Folder 1, Folder 2, Folder 3, etc., and you wish to rename them to New Folder 1, New Folder 2, New Folder 3, etc. In this case, we can use a regular expression to select the folders we want to rename. The command to rename folders that match a regular expression would be:
$FolderPath = "C:\Test"
$Pattern = 'Folder (\d+)$'
$Replacement = 'New Folder $1'
Get-ChildItem -Path $FolderPath -Directory | Where-Object { $_.Name -match $Pattern } | ForEach-Object { $NewName = $_.Name -replace $Pattern, $Replacement Rename-Item -Path $_.FullName -NewName $NewName
}In this command, we use the Get-ChildItem cmdlet to get all items in the directory specified by the path “C:\Test”. We then use the Where-Object cmdlet to select only the folders that match the regular expression “Folder (\d+)$”. Finally, we use the Rename-Item cmdlet to rename the selected folders by replacing the matched regular expression with “New Folder $1”.
Renaming a Single Folder in PowerShell
Rename-Item -path "C:\Project 001" -NewName "Project One"
Rename-Item -Path "old_folder_name" -NewName "new_folder_name"
Replace “old_folder_name” with the current name of the folder, and “new_folder_name” with the new name you want to give the folder. Once done, Press enter, and the folder will be renamed. It’s that simple!

The Rename-Item can also be used to rename files and registry keys. Here is another post on renaming files in PowerShell:
Rename-Item -Path "C:\Logs\AppLog.txt" -NewName "Old-AppLog.txt"
More here: How to Rename Files using PowerShell?
Handling errors while renaming folders with PowerShell
One of the challenges of automating folder renaming with PowerShell is handling errors. If something goes wrong during the renaming process, you’ll want to be notified to take corrective action.
To handle errors in PowerShell, you can use the Try-Catch statement. The Try-Catch statement allows you to specify what should happen if a particular command or series of commands fails. You can use it to log errors, display error messages, or take other corrective actions.
Here’s an example of how to use the Try-Catch statement to handle errors while renaming folders:
$directories = Get-ChildItem -Path "C:\Projects" -Directory
foreach ($directory in $directories) { try { if ($directory.Name -match "keyword") { $newName = $directory.Name -replace "keyword", "new_keyword" Rename-Item -Path $directory.FullName -NewName $newName -ErrorAction Stop } } catch { Write-Host "Error renaming folder $($directory.FullName): $_" }
}How to Rename Files in a Folder With an Increasing Number
Get-ChildItem PATH -Recurse -Include "*.txt" | ForEach-Object -Begin { $Counter = 1 } -Process { Rename-Item $_.FullName -NewName ("{0}_{1}" -f $Counter, $_.Name) ; $Counter++ }
Best practices for renaming folders with PowerShell
Now that we’ve covered the basics of renaming folders with PowerShell, let’s look at some best practices to keep in mind:
- Always test your scripts on a small set of folders before running them on a large dataset.
- Use conditional statements to target specific folders or directories.
- Use Try-Catch statements to handle errors and log them for future reference. Make sure you handle common errors and avoid duplicates. For example, check if the source folder exists before renaming it. Otherwise, you’ll see an error message, “Rename-Item : Cannot rename because item at ‘AppLog’ does not exist.”.
- Always use descriptive and meaningful folder names.
- Avoid using special characters or reserved words in folder names.
- Back up your data before running any automated scripts.
How to Rename a Specific File
To give a new name to a specific file on your PC, use PowerShell’s Rename-Item cmdlet. This cmdlet takes your file’s full path, the new name you want to assign, and renames your file.
Rename-Item PATH -NewName MyName.ext
Make sure to use your file’s original extension when you rename the file. Using another extension can make your file unusable.
Rename-Item "C:\Users\Username\Desktop\Old-Test.txt" -NewName “New-Test.txt”
PowerShell won’t display a message confirming your file is renamed, but know that the job is done.
Move a Folder and rename it in PowerShell
Renaming a folder is different from moving a folder. To move and rename a folder, you need to use the Move-Item cmdlet. Here’s an example of how to rename a folder:
Move-Item -Path "C:\SourceFolder" -Destination "C:\RenamedFolder"
This command will rename the folder ‘SourceFolder’ located in C: to ‘RenamedFolder’.
Conclusion
Renaming folders and directories can be a tedious and time-consuming task, but with PowerShell, it doesn’t have to be. In this article, we discussed different methods to rename a folder using PowerShell. We covered how to rename a single folder, rename multiple folders using wildcards, and rename folders using regular expressions. We also explored how to use the Substring method to rename folders. PowerShell provides a powerful and flexible way to rename folders, making it a useful tool for managing files and folders on your system.
And remember, always test your scripts on a small dataset before running them on many folders. Happy renaming!
How do I rename a folder and file in PowerShell?
To rename a folder or file using PowerShell, you can use the Rename-Item cmdlet. Here’s an example of how to do it:Rename-Item -Path "C:\Old-File-or-Folder-Name" -NewName "New-FIle-or-Folder-Name"
Supply the path of the item to “path” and the new item name for “NewName” parameters.
How to rename a folder using the command line (CMD)?
In the Command Prompt (cmd.exe) on Windows, you can use the ren (or rename) command to rename a folder. Here’s an example:Ren "C:\OldFolderName" "NewFolderName"
How do I rename all files in a folder with a prefix?
How do you handle space in the path in PowerShell?
In PowerShell, if a path contains a space, you will need to enclose the path in double quotes. Here’s an example: Get-ChildItem "C:\Some Folder\Some File.txt"
How do I change the working directory in the PowerShell script?
How do I move a file to a new directory in PowerShell?
To move a file to a new directory in PowerShell, you can use the Move-Item cmdlet. Here’s an example of how to do it:Move-Item -Path "C:\Temp\AppLog.txt" -Destination "C:\Scripts\"
How can I recursively rename folders within a directory structure?
Can I rename a folder and move it to a different location simultaneously?
Yes, you can combine renaming and moving a folder in a single command using the Move-Item cmdlet. Here’s an example: Move-Item -Path "Path/to/OldFolderName" -Destination "Path/to/NewLocation/NewFolderName"
What happens if a folder with the new name already exists?
If a folder with the new name already exists in the same location, the Rename-Item cmdlet will throw an error: “Rename-Item : Cannot create a file when that file already exists”.
Can I rename multiple folders at once using PowerShell?


