PowerShell has a built-in cmdlet Rename-Item that allows you to rename files, directories, or other items in a file system or registry.
You can use any of the following methods to rename files in the PowerShell.
Method 1: Rename files in PowerShell
Rename-Item -Path "C:\temp\log.txt", -NewName "log_daily.txt"
This example renames the file log.txt to log_daily.txt.
Method 2: Rename multiple files in PowerShell
Get-ChildItem -Path "C:\temp\log\*.txt" | Rename-Item -NewName {$_.Name -replace '.txt','.log'}
This example gets all the files in the log folder with the *.txt file extension and pipes to the Rename-Item. The Rename-Item cmdlet renames multiple files with the extension .txt to .log using the -NewName parameter.
Method 3: Rename hidden and read-only files in PowerShell
These methods can be used to rename files in PowerShell using the Rename-Item cmdlet.
The following examples show how to use each one of the methods.
Rename File in PowerShell Using Rename-Item
To rename a file in PowerShell, you can use the Rename-Item cmdlet. It changes the name of the specified file.
The following example shows how to do it with syntax.
# Rename file name Rename-Item -Path "C:\temp\log\log.txt" -NewName "log_daily.txt" # Retrieve the files in the given folder Get-ChildItem -Path C:\temp\log\
This PowerShell script renames the file name log.txt to log_daily.txt. The -Path parameter specifies the current file that you want to rename and the -NewName parameter specifies the new name you want to give to the file.
Note that, the Rename-Item cmdlet doesn’t affect the content of the item being renamed.
Rename Multiple Files in PowerShell Using Rename-Item
To rename multiple files in PowerShell with the given extension, use the Rename-Item cmdlet. The Get-ChildItem cmdlet can be used to retrieve the list of all files that you want to rename.
The following example shows how to do it with syntax.
Get-ChildItem -Path "C:\temp\log\*.txt" Get-ChildItem -Path "C:\temp\log\*.txt" | Rename-Item -NewName {$_.Name -replace '.txt','.log'} Get-ChildItem -Path "C:\temp\log\*.log"
The output of the above PowerShell script shows the multiple files in a folder having a .txt file extension renamed to a .log file extension.
The Get-ChildItem cmdlet gets all the files in the specified folder and pipes them to the Rename-Item command.
The Rename-Item command uses the -NewName parameter to rename multiple files with an extension to another extension using the -replace operator.
Rename Hidden and Read-Only Files in PowerShell Using Rename-Item
To rename hidden and read-only files in PowerShell, you can use the Rename-Item cmdlet with the -Force parameter. This parameter allows you to rename items forcefully.
The following example shows how to do it with syntax.
Rename-Item -Path "C:\temp\log\paywall.txt" -NewName paywall.log -Force
In this PowerShell script, the Rename-Item cmdlet renames the file specified at location C:\temp\log\paywall.txt to the new name paywall.log.
The Rename-Item cmdlet uses the -NewName parameter to specify the name for the file and the -Force parameter to force the cmdlet to rename the file, even if it’s hidden or read-only.
Conclusion
I hope the above article on how to rename files in PowerShell with the Rename-Item cmdlet is helpful to you.
The Rename-Item command renames the file name or its extension in a file system or registry. It doesn’t change the content of the file while renaming it.
You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.