How to Rename a File using PowerShell?

Requirement: Rename a file using PowerShell.

PowerShell to Rename a File

Renaming files can be a time-consuming process, especially when you have to alter many files at once. Thankfully, PowerShell allows you to rename multiple files quickly. In this article, we will show you how to rename a file using PowerShell.

To rename a file using PowerShell, you can use the Rename-Item cmdlet, you need to specify the path to the file and the new name you want to give it. For example, to rename a file called oldname.txt to newname.txt, you can use the following command:

Rename-Item -Path C:\Example\oldname.txt -NewName C:\Example\newname.txt

This will rename the file from oldname.txt to newname.txt in the C:\Example directory.

PowerShell to rename file – Overwrite existing

The Rename-Item cmdlet cannot overwrite existing files (However, You can test if the target file exists and then delete it!). Use the Move-Item cmdlet instead to rename a file with the “Force” switch. This cmdlet allows you to specify both the source path of the file and the destination path, which can include a new file name. Here’s an example of how you can use the Move-Item cmdlet to move and rename a file:

Move-Item -Path C:\Temp\Oldname.txt -Destination C:\Temp\newname.txt -Force

Note that you will need to have the appropriate permissions to rename a file in the specified location. It has a -Force parameter that can be used to override any errors that might occur during the move operation, such as overwriting any existing files.

PowerShell to Rename multiple files

To rename multiple files in PowerShell, you can use the Get-ChildItem cmdlet to get a list of the files you want to rename, and then pipe the list to the Rename-Item cmdlet. You can use wildcards to specify the files you want to rename, and you can use the -NewName parameter of the Rename-Item cmdlet to specify the new names for the files.

Let’s recursively replace the “Space” character with “-” in all files from a Folder and its sub-folders.

Get-ChildItem -Path "C:\Temp" -Recurse -Include "*.txt" | Rename-Item -NewName { $_.Name -replace " ","-" }

Rename all files in a Folder with an increasing number

Get-ChildItem -Path "C:\Temp" -Recurse -Include "*.txt" | ForEach-Object -Begin { $Counter = 1 } -Process { Rename-Item $_ -NewName "Log_$Counter.log" ; $Counter++ }

Rename all files by adding the time stamp in file names:

#Get the Timestamp
$TimeStamp = Get-Date -F yyyy-MM-dd_HH-mm

#Get all text files from a Folder and rename them by appending Timestamp
Get-ChildItem -Path "C:\Temp" -Recurse -Include "*.txt" | ForEach-Object { 
    Rename-Item -Path $_.FullName -NewName "$($_.DirectoryName)\$($_.BaseName)_$TimeStamp$($_.Extension)"
}

Rename all the files in the current directory, prefixing each with “AppLog – “:

#Get all text files from a Folder and rename them by appending Timestamp
Get-ChildItem -Path "C:\Temp" -Recurse -Include "*.txt" | ForEach-Object {
    Rename-Item $_.FullName -NewName "$($_.DirectoryName)\AppLog - $($_.BaseName)$($_.Extension)"
}

Bulk Rename All Files in a Directory

Here is an example of how to rename a series of files named “abc.txt”, “def.txt”, “xyz.txt”, etc. to “file1.csv”, “file2.csv”, “file3.csv”, etc., you can use the following script:

$i = 1
Get-ChildItem C:\Temp*.txt | ForEach-Object {
   Rename-Item $_ -NewName "File$i.csv"
   $i++
}

This will rename the files in a sequential manner, incrementing the number at the end of the filename for each file.

PowerShell script to rename file extensions

For example, suppose you have a directory with .txt files and wants to rename all the file’s extension .log:

#Get All Txt Files from C:\Logs Folder
$Files = Get-ChildItem C:\Logs\*.txt

#Change File extention from .txt to .log
ForEach ($File in $Files) {
    Rename-Item -Path $File.FullName -NewName ($File.Name -replace ".txt", ".log")
    Write-host "Renamed File:"$File.FullName
}

This will rename all of the .txt files in the C:\Logs directory to have a .txt extension. It uses the -replace operator to replace the .txt extension with .log while keeping the rest of the filenames the same.

PowerShell to rename if File exists

To rename a file in PowerShell if it exists, you can use the Test-Path cmdlet to check if the file exists, and then use the Rename-Item cmdlet to rename the file. Here’s an example of how you can do this:

#Parameter
$OldFile = "C:\Logs\OldFile.txt"
$NewFile = "C:\Logs\NewFile.log"

#PowerShell to rename file if exists
If (Test-Path $OldFile) {
    Rename-Item -Path $OldFile -NewName $NewFile
    Write-host "'$OldFile' has been renamed to '$NewFile'" -f Green
}
Else{
    Write-host "'$OldFile' does not exists!" -f Yellow
}

This will check if oldFile.txt exists in the specified path. If it does, it will be renamed to newFile.log. If the file doesn’t exist, the Rename-Item cmdlet will not be executed, and the script will continue running. How about checking if the target file exists and deleting it, before renaming the old file?

#Parameter
$OldFile = "C:\Logs\OldFile.txt"
$NewFile = "C:\Logs\NewFile.log"

#PowerShell to rename file if exists
If (Test-Path $OldFile) {
    #Check if the Target File Exists
    If (Test-Path $NewFile) {
        #Delete the target file
        Remove-Item $NewFile
    }
    Rename-Item -Path $OldFile -NewName $NewFile
    Write-host "'$OldFile' has been renamed to '$NewFile'" -f Green
}
Else{
    Write-host "'$OldFile' does not exists!" -f Yellow
}

Conclusion

In conclusion, time-consuming tasks like renaming your large batch of files can be made much simpler and faster with PowerShell. It allows you to use wildcards, and other advanced features to rename multiple files at once. It also allows you to preview the changes before committing them using “-WhatIf” switch, which can be helpful in avoiding mistakes. Overall, PowerShell makes it easy to rename files quickly and efficiently, making it a valuable tool for anyone working with large numbers of files.

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

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