How to Copy and Rename Files in PowerShell?

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" -Recurse

Rename 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 $newFileName

This 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:

Copy and Rename Files in PowerShell

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 $newFileName

Advanced 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 $newFileName

Incrementing 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 $newFileName

This 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: