In this article, I will explain various methods to copy and rename files using PowerShell, providing examples and complete scripts to help you understand better.
To copy and rename a file in PowerShell, you can use a combination of the Copy-Item and Rename-Item cmdlets. First, use Copy-Item to copy the file to the desired location, and then use Rename-Item to rename the newly copied file. For instance:
Copy-Item -Path "C:\Source\example.txt" -Destination "C:\Destination"
Rename-Item -Path "C:\Destination\example.txt" -NewName "newexample.txt"This script will copy example.txt from the Source folder to the Destination folder and then rename it to newexample.txt.
Copy Files with PowerShell
Before we get into renaming, let’s start with the basics of copying files. The primary cmdlet used for copying files in PowerShell is Copy-Item. This cmdlet allows you to copy files and directories to a new location.
Basic Copy Command
Here’s a simple example of copying a single file from one location to another:
Copy-Item -Path "C:\Source\file.txt" -Destination "C:\Destination\file.txt"Copy Multiple Files
To copy multiple files in PowerShell, you can use wildcards:
Copy-Item -Path "C:\Source\*.txt" -Destination "C:\Destination"This command will copy all .txt files from the Source directory to the Destination directory in PowerShell.
Preserving Folder Structure
If you want to copy an entire directory and preserve the folder structure, you can use the PowerShell -Recurse parameter:
Copy-Item -Path "C:\SourceFolder" -Destination "C:\DestinationFolder" -RecurseRename Files with PowerShell
To rename a file in PowerShell, you use the Rename-Item cmdlet. This cmdlet changes the name of a single file or folder.
Basic Rename Command
Here’s an example of renaming a file:
Rename-Item -Path "C:\Source\oldname.txt" -NewName "newname.txt"Copy and Rename Files in One Step
While there isn’t a single cmdlet that copies and renames a file in PowerShell simultaneously, you can combine Copy-Item with Rename-Item in a script to perform both actions together.
Using a Script Block
Here’s an example script that copies and renames a file in PowerShell:
$fileToCopy = "C:\Source\file.txt"
$destinationFolder = "C:\Destination"
$newFileName = "newfile.txt"
Copy-Item -Path $fileToCopy -Destination $destinationFolder
Rename-Item -Path (Join-Path $destinationFolder (Split-Path $fileToCopy -Leaf)) -NewName $newFileNameThis PowerShell script takes a source file, copies it to the destination, then renames the copied file to the new name you specify.
I executed the code using VS code, and you can see the output in the screenshot below:

Handling File Extensions
If you need to maintain the file extension and only change the base name, you can use the BaseName and Extension properties of a FileInfo object:
$fileToCopy = "C:\Source\file.txt"
$destinationFolder = "C:\Destination"
$newBaseName = "newfile"
$fileInfo = Get-Item $fileToCopy
$newFileName = $newBaseName + $fileInfo.Extension
Copy-Item -Path $fileToCopy -Destination $destinationFolder
Rename-Item -Path (Join-Path $destinationFolder $fileInfo.Name) -NewName $newFileNameAdvanced Copy and Rename Techniques in PowerShell
Sometimes, you might need to copy and rename files in a more complex way, such as adding timestamps or incrementing numbers to filenames.
Adding Timestamps to Filenames
To add a timestamp to a copied file’s name, you can use the Get-Date cmdlet:
$fileToCopy = "C:\Source\file.txt"
$destinationFolder = "C:\Destination"
$timestamp = Get-Date -Format "yyyyMMddHHmmss"
$newFileName = "file_$timestamp.txt"
Copy-Item -Path $fileToCopy -Destination $destinationFolder
Rename-Item -Path (Join-Path $destinationFolder (Split-Path $fileToCopy -Leaf)) -NewName $newFileNameIncrementing File Names
If you’re copying a file to a directory where files with similar names exist, you might want to increment the file name:
$fileToCopy = "C:\Source\file.txt"
$destinationFolder = "C:\Destination"
$baseName = "file"
$extension = ".txt"
$index = 1
do {
$newFileName = "$($baseName)_$($index)$($extension)"
$index++
} while (Test-Path (Join-Path $destinationFolder $newFileName))
Copy-Item -Path $fileToCopy -Destination $destinationFolder
Rename-Item -Path (Join-Path $destinationFolder (Split-Path $fileToCopy -Leaf)) -NewName $newFileNameThis script will keep incrementing the index until it finds a filename that doesn’t exist in the destination folder.
Conclusion
In this PowerShell tutorial, I have explained how to copy and rename files in PowerShell using Copy-Item and Rename-Item cmdlets.
You may also like:
- PowerShell Foreach File in Folder
- Count Lines in a File in PowerShell
- How to Get File Name from Path in PowerShell?
- How to Check if a File Contains a String in PowerShell?
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.