Back to Homepage
Sunday 9 February 2025
16

How to Copy Files in PowerShell

Introduction to Basic Commands

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.

Copying Multiple Files from a Folder

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.

Copying Files Recursively

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

Filtering Files by Extension

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"

Copying Multiple Specific Files

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.

Copying Files While Preserving Folder Structure

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.

Handling Conflicts

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:

Overwriting Files

To overwrite existing files without prompting, use the -Force parameter:

Copy-Item -Path "C:\Source\Path\*" -Destination "C:\Destination\Path" -Recurse -Force

Avoid Overwriting Files

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.

Conclusion

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.

Hashtags:
powershell
Share:
Created by:
Author photo

Jorge García

Fullstack developer