Before diving into copying multiple files, it is important to understand the basic PowerShell commands related to file management.
Copy-Item Command
The main command for copying files in PowerShell is Copy-Item. Its basic syntax is as follows:
Copy-Item -Path "Path\To\File" -Destination "Path\To\Destination"
This command copies a file from the specified path to the destination. You can use wildcards (*) to copy multiple files matching a pattern.
If you want to copy all files from one folder to another, you can use the following command:
Copy-Item -Path "C:\Source\Path\*" -Destination "C:\Destination\Path"
This command copies all files (but not folders) from C:\Source\Path to C:\Destination\Path.
To copy files and folders recursively (including subfolders), you need to add the -Recurse parameter:
Copy-Item -Path "C:\Source\Path\*" -Destination "C:\Destination\Path" -Recurse
You can filter files by their extension using wildcards. For example, to copy only .txt files:
Copy-Item -Path "C:\Source\Path\*.txt" -Destination "C:\Destination\Path"
If you need to copy specific files and do not want to use wildcards, you can do so with a file list:
$files = @("C:\Source\Path\file1.txt", "C:\Source\Path\file2.docx", "C:\Source\Path\file3.pdf")
foreach ($file in $files) {
Copy-Item -Path $file -Destination "C:\Destination\Path"
}
This script iterates through each file in the $files list and copies it to the destination folder.
To maintain the folder structure when copying, especially useful in backup or migration scenarios, you can use xcopy or robocopy, but here is how to do it with PowerShell:
Get-ChildItem -Path "C:\Source\Path" -Recurse | ForEach-Object {
$destination = $_.FullName.Replace("C:\Source\Path", "C:\Destination\Path")
if ($_.PSIsContainer) {
New-Item -ItemType Directory -Path $destination -Force
} else {
Copy-Item -Path $_.FullName -Destination $destination -Force
}
}
This script recursively iterates through all items in C:\Source\Path. If the item is a folder, it creates the same folder in the destination. If it is a file, it copies it to the corresponding destination location, preserving the original structure.
When copying files, conflicts may arise if a file with the same name already exists in the destination. Here are some ways to handle these conflicts:
To overwrite existing files without prompting, use the -Force parameter:
Copy-Item -Path "C:\Source\Path\*" -Destination "C:\Destination\Path" -Recurse -Force
To avoid overwriting existing files, you can use conditional logic:
Get-ChildItem -Path "C:\Source\Path" -Recurse | ForEach-Object {
$destination = $_.FullName.Replace("C:\Source\Path", "C:\Destination\Path")
if (-not (Test-Path -Path $destination)) {
Copy-Item -Path $_.FullName -Destination $destination -Recurse
}
}
This script copies only the files that do not already exist in the destination.
Copying multiple files in PowerShell is a task that can vary in complexity depending on your specific needs. From copying all files in a folder to preserving directory structures or avoiding file overwrites, PowerShell offers great flexibility. With the commands and scripts presented in this article, you should be able to handle most file-copying tasks efficiently and effectively.
Jorge García
Fullstack developer