Updated February 28, 2023
Introduction to PowerShell Rename Folder
In PowerShell, renaming an item like a folder or a file name is achieved with the help of Rename-Item Cmdlet. It just changes the item and not the contents of the item. This is different from moving the item and can’t be used to move the item. The folder/item that is being renamed must be present else the cmdlet will be thrown an error. This can also be used to rename multiple items/folders simultaneously. The article will be covering in detail on the various parameters that are associated with the Rename-Item cmdlet, their use and how renaming works in PowerShell.
Syntax and Parameters
The below is the syntax of the Rename-Item Cmdlet:
NAME
Rename-Item
SYNTAX
Rename-Item [-Path] <string> [-NewName] <string> [-Force] [-PassThru] [-Credential <pscredential>] [-WhatIf] [-Confirm] [-UseTransaction] [<CommonParameters>]
Rename-Item [-NewName] <string> -LiteralPath<string> [-Force] [-PassThru] [-Credential <pscredential>] [-WhatIf] [-Confirm] [-UseTransaction] [<CommonParameters>]
ALIASES
rni
ren
Example:
Rename-Item -Path "c:\test\test1.txt" -NewName "test2.txt"
The above cmdlet will rename the file test1.txt to test2.txt without altering the content
Rename-Item -Path "c:\test\" -NewName "test2"
The above cmdlet will rename the folder test to test2
Parameters:
- Confirm: This is used to get the confirmation of the user before executing the cmdlet. It’s an optional parameter. Its default value is false, and its type is switch parameter. The alias for this parameter is cf. Default value is false. It doesn’t accept pipeline characters as input and wild card characters are also not accepted.
- Credential: This is for impersonation purpose or elevating the account on which the cmdlet is being run. To use this, invoke command must be used and it is not supported by any providers installed within PowerShell. Its type is PS Credential. Its default value is current user. It accepts pipeline input but doesn’t accept wildcard characters.
- Force: Hidden folders or items or read-only folders or read-only items can’t be renamed unless force parameters are explicitly mentioned in the rename-item cmdlet. Constant variables and aliases can’t also be renamed. The security restrictions are still maintained even though force parameter is used. Its type is switch parameter. Its default value is false.It doesn’t accept pipeline characters as input and wild card characters are also not accepted.
- Literal Path: This denotes or represents the item or items path to be renamed. The value whatever is typed as the literal value. None of the characters are interpreted as wild cards. In case if the path contains escape characters, it must be enclosed within single quotation to inform PowerShell not to interpret as escape sequence. This is a mandatory parameter.
- -NewName: This denotes the new name of the item to which the item must be renamed. Only a name must be entered and not a path. The cmdlet will understand whether a path or a file must be renamed. Wild cards are not accepted. Replace operator must be used when multiple items are to be renamed. Its type is string. Default value is none. It accepts pipeline input, but wild card characters are not accepted.
- PassThru: This doesn’t generate any input. It is used to return items to be the pipeline. It’s an optional parameter. Its type is switch parameter. Its default value is false.It doesn’t accept pipeline characters as input and wild card characters are also not accepted.
- Path: This is a mandatory parameter. It denotes the path of the item to be renamed. Its type is string. Its default value is none. Pipeline input is accepted whereas wild card characters are not considered.
- WhatIf: This doesn’t produce any output. It just shows what will happen if the cmdlet is run. Its type is switch parameter. Its alias is wi. Its default value is false.It doesn’t accept pipeline characters as input and wild card characters are also not accepted.
The rename-item cmdlet supports only with providers that are being exposed. To find the list of available providers in our session, run the below cmdlet.
Input:
Get-PsProvider
Output:
List of Providers in the current PowerShell Session
Name | Capabilities | Drives |
Registry | ShouldProcess, Transactions | {HKLM, HKCU} |
Alias | ShouldProcess | {Alias} |
Environment | ShouldProcess | {Env} |
FileSystem | Filter, ShouldProcess, Credentials | {C} |
Function | ShouldProcess | {Function} |
Variable | ShouldProcess | {Variable} |
Examples of PowerShell Rename Folder
Following are the example are given below:
Example #1
Input:
Write-Host "Welcome to the demo of renaming folder"
write-host "Renaming Test1 to test9 in the below example"
Write-Host "List of Folders before renaming"
Get-ChildItem -Path C:\Vignesh\Test
Rename-Item -Path C:\Vignesh\Test\Test1 -NewName Test9
Write-Host "Renaming completed successfully"
Write-Host "List of folders after renaming"
Get-ChildItem -Path C:\Vignesh\Test
Write-Host "Renaming a file"
Write-Host "File name before renaming"
Get-ChildItem -Path C:\Vignesh\Test\Actual1239
Write-Host "After Renaming"
Rename-Item -Path C:\Vignesh\Test\Test9\test.txt -NewNameactual.txt
Get-ChildItem -Path C:\Vignesh\Test\Actual1239
Output:
Example #2
Input:
write-host "Welcome to the demo of bulk renaming of folders and files"
Write-Host "Renaming multiple folders at once"
Write-Host "before renaming the folders names are as below"
Get-ChildItem -Path C:\Vignesh\Test
Get-ChildItem C:\Vignesh\Test -Recurse | Rename-Item -NewName{ $_.name -replace 'Actual', 'Actual123'}
Write-Host "After renaming the folders"
Write-Host "Now the folders name are as below"
Get-ChildItem -Path C:\Vignesh\Test
Write-Host "Renaming txt files to doc"
Write-Host "Before Renaming the files are as below"
Get-ChildItem C:\Vignesh\Test\Actual1239
Write-Host "Renaming all txt files to log"
Get-ChildItem C:\Vignesh\Test\Actual1239 | Rename-Item -NewName{ $_.Name -replace '.txt','.log' }
Write-Host "After renaming"
Get-ChildItem C:\Vignesh\Test\Actual1239
Output:
Example #3
Input:
Write-Host "Moving folders within a directory to another directory"
Write-Host "Before moving the list of files in the source directory is"
Get-ChildItem C:\Vignesh\Script
Write-Host "List of files in destination directory"
Get-ChildItem C:\Vignesh\Test
Write-Host "Moving the folders from script to test"
Get-ChildItem -Path C:\Vignesh\Script | Move-Item -Destination C:\Vignesh\Test
Write-Host "Folders moved successfully"
Write-Host "Folders now in source"
Get-ChildItem C:\Vignesh\Script
Write-Host "Folders now in Destination"
Get-ChildItem C:\Vignesh\Test
Write-Host "Renaming file name with todays date and time"
Write-Host "File names before renamin"
Get-ChildItem C:\Vignesh\Test\Actual1239
$curDateTime = Get-Date -Format yyyyMMdd-HHmmss
Get-ChildItemC:\Vignesh\Test\Actual1239 -Recurse |
Rename-Item -NewName {$_.Basename + '_' + $curDateTime + $_.Extension }
Write-Host "File names after renaming"
Get-ChildItem C:\Vignesh\Test\Actual1239
Output:
Conclusion
Thus, the article covered in detail about the Rename-Item cmdlet. It can be used to rename folders, files or any item. The article also demonstrated appropriate example scripts on how to rename a single file, single folder, multiple files, multiple folders and how a file name can be appended with today date. To learn more about this cmdlet in detail, it is advisable to write sample scripts and practice them.
Recommended Articles
This is a guide to PowerShell Rename Folder. Here we also discuss the Introduction and syntax and parameters along with different examples and its code implementation. You may also have a look at the following articles to learn more –