PowerShell Copy-Item Cmdlet to Copy Files and Folders

The PowerShell Copy-Item is a useful cmdlet that allows you to copy files and folders. In this tutorial, I will explain how to use the PowerShell Copy-Item cmdlet to copy files and folders efficiently with some examples.

PowerShell Copy-Item

PowerShell Copy-Item is used to copy files and directories from one location to another. Whether you are migrating data, backing up files, or simply organizing your directories, Copy-Item can save you a significant amount of time and effort. It can handle files, directories, and even registry keys.

Copy-Item provides a wide range of options and parameters that allow you to customize your copying tasks according to your specific needs. You can filter files based on attributes, use wildcards, and even copy files recursively.

Basic Syntax of Copy-Item

The basic syntax of the Copy-Item cmdlet is as follows:

Copy-Item -Path <source> -Destination <destination>
  • -Path: Specifies the path to the file or folder you want to copy. You can provide a single path or multiple paths separated by commas.
  • -Destination: Specifies the path where you want to copy the file or folder. If the destination folder doesn’t exist, Copy-Item will create it automatically.

Here’s a simple example that demonstrates how to copy a file using Copy-Item:

Copy-Item -Path "C:\Reports\SalesReport.xlsx" -Destination "C:\ReportsBackup"

In this example, the “SalesReport.xlsx” file located in the “C:\Reports” folder will be copied to the “D:\Backup” folder.

Check out How to Get File Size Using PowerShell?

How to Use Copy-Item Cmdlet in PowerShell

Now, let me show you some practical examples of using the Copy-Item cmdlet in PowerShell, which I used while working as a PowerShell administrator.

Example 1: Copy a Single File

Let’s start with a simple example. Suppose you want to copy a file named Report2024.pdf from the C:\Projects directory to the C:\Archives directory.

Copy-Item -Path "C:\Reports\Reports2024.pdf" -Destination "C:\Archives"

This command will copy the Report2024.pdf file to the C:\Archives directory.

I executed the above script using VS code, and you can see the exact output in the screenshot below:

powershell copy-item

Example 2: Copy Multiple Files

If you need to copy multiple files, you can use wildcard characters. For instance, to copy all PDF files from C:\Projects to C:\Archives:

Copy-Item -Path "C:\Projects\*.pdf" -Destination "C:\Archives"

This command will copy all files with a .pdf extension from the source to the destination directory.

Check out Get the Last Modified Date of a File in PowerShell

Example 3: Copy Directories Recursively

To copy an entire directory and its contents, you need to use the -Recurse parameter in the Copy-Item cmdlet. For example, to copy the C:\Projects directory and all its subdirectories to D:\Backup\Projects:

Copy-Item -Path "C:\Projects" -Destination "D:\Backup\Projects" -Recurse

This command will ensure that all files and subdirectories under C:\Projects are copied to D:\Backup\Projects.

Example 4: Copy Files with a Progress Bar

When copying large files or directories, it can be helpful to see the progress. PowerShell supports this with the Write-Progress cmdlet. Here is an example of how to copy files with a progress bar:

$source = "C:\Projects"
$destination = "D:\Backup\Projects"
$files = Get-ChildItem -Path $source -Recurse
$total = $files.Count
$count = 0

foreach ($file in $files) {
    $count++
    Write-Progress -Activity "Copying files" -Status "$count out of $total" -PercentComplete (($count / $total) * 100)
    Copy-Item -Path $file.FullName -Destination $destination -Recurse
}

This script will display a progress bar indicating the percentage of files copied.

Read PowerShell unblock-file

Example 5: Handle File Conflicts

When copying files, you might encounter conflicts if files with the same name already exist in the destination directory. By default, Copy-Item will overwrite existing files without prompting. To avoid this, you can use the -Force parameter to overwrite files or handle conflicts manually.

To prompt the user before overwriting files, you can use a custom script:

$source = "C:\Projects\Report2024.pdf"
$destination = "C:\Archives\Report2024.pdf"

if (Test-Path $destination) {
    $response = Read-Host "File exists. Overwrite? (Y/N)"
    if ($response -eq "Y") {
        Copy-Item -Path $source -Destination $destination -Force
    }
} else {
    Copy-Item -Path $source -Destination $destination
}

This script checks if the file exists in the destination and prompts the user for confirmation before overwriting.

Here is the exact output you can see in the screenshot below:

powershell copy-item overwrite

Example 6: Copying Files with Specific Attributes

Sometimes, you may need to copy files based on their attributes, such as creation date or size. You can use the Get-ChildItem cmdlet to filter files and then use Copy-Item to copy them. For example, to copy all files created in the last 30 days:

$source = "C:\Projects"
$destination = "D:\Backup\RecentProjects"
$cutoffDate = (Get-Date).AddDays(-30)

Get-ChildItem -Path $source -Recurse | Where-Object { $_.CreationTime -gt $cutoffDate } | ForEach-Object {
    Copy-Item -Path $_.FullName -Destination $destination -Recurse
}

This script filters files based on their creation date and copies only the recent ones.

Read PowerShell get-childitem sort by date

Example 7: Copy Files Over a Network

Copying files over a network can be slightly different due to network paths and permissions. For example, to copy a file from a local directory to a network share:

Copy-Item -Path "C:\Projects\Report2024.pdf" -Destination "\\ServerName\SharedFolder\Report2024.pdf"

Ensure you have the necessary permissions to access the network share.

Example 8: Log Copy Operations

For auditing purposes, you might want to log the details of copy operations. You can achieve this by redirecting output to a log file:

$source = "C:\Projects"
$destination = "D:\Backup\Projects"
$logFile = "C:\Logs\CopyLog.txt"

Get-ChildItem -Path $source -Recurse | ForEach-Object {
    Copy-Item -Path $_.FullName -Destination $destination -Recurse
    Add-Content -Path $logFile -Value "Copied $($_.FullName) to $destination at $(Get-Date)"
}

This script logs each file copied along with the timestamp.

PowerShell Copy-Item Overwrite

In PowerShell, the Copy-Item cmdlet is used to copy files and directories from one location to another. When copying items, you may encounter situations where the destination already contains a file or directory with the same name. In such cases, PowerShell provides options to control the overwrite behavior.

By default, when using Copy-Item, PowerShell will prompt you for confirmation before overwriting an existing file or directory at the destination.

However, there may be scenarios where you want to automatically overwrite existing files or directories without being prompted for confirmation. You can use the -Force parameter with the Copy-Item cmdlet to achieve this.

Here’s an example that demonstrates how to use the -Force parameter to overwrite existing files:

Copy-Item -Path "C:\Source\file.txt" -Destination "C:\Destination\file.txt" -Force

In this example, if a file named “file.txt” already exists in the “C:\Destination” directory, the -Force parameter will instruct PowerShell to overwrite it without prompting for confirmation.

Similarly, when copying directories, you can use the -Force parameter to overwrite existing directories and their contents:

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

In this case, the -Recurse parameter is used to copy the entire directory and its subdirectories, while the -Force parameter ensures that any existing directories at the destination are overwritten without confirmation.

If you want to overwrite files selectively based on certain conditions, you can combine the -Force parameter with other parameters like -Filter, -Include, or -Exclude. For example:

Copy-Item -Path "C:\Source\*.txt" -Destination "C:\Destination" -Force -Include "report*"

This command will copy only the text files from the “C:\Source” directory that match the pattern “report*” and overwrite any existing files with the same names in the “C:\Destination” directory.

Read PowerShell If Date Is Older Than 30 Days

PowerShell Copy-Item Exclude Folder

In PowerShell, the Copy-Item cmdlet allows you to copy files and directories from one location to another. When copying items, you may want to exclude specific folders from being copied. PowerShell provides a few ways to achieve this using the -Exclude parameter or by combining other cmdlets with Copy-Item.

  • Using the -Exclude Parameter: The -Exclude parameter allows you to specify one or more folders to exclude from the copy operation. Here’s an example:
Copy-Item -Path "C:\Source\*" -Destination "C:\Destination" -Exclude "Temp", "Backup"

In this example, all files and directories from the “C:\Source” directory will be copied to the “C:\Destination” directory, except for the “Temp” and “Backup” folders.

You can also use wildcards with the -Exclude parameter to exclude folders based on patterns. For example:

Copy-Item -Path "C:\Source\*" -Destination "C:\Destination" -Exclude "Temp*", "*Backup"

This command will exclude folders that start with “Temp” or end with “Backup” from being copied.

  • Using the -Filter Parameter: The -Filter parameter allows you to specify a filter to include only certain files or directories based on their names. By using the -Recurse parameter along with -Filter, you can exclude folders from being copied. Here’s an example:
Copy-Item -Path "C:\Source\*" -Destination "C:\Destination" -Recurse -Filter "*.txt"

In this case, only files with the “.txt” extension will be copied recursively from the “C:\Source” directory to the “C:\Destination” directory. Folders will be excluded implicitly because they don’t match the filter.

  • Combining Get-ChildItem and Copy-Item: You can use the Get-ChildItem cmdlet to retrieve a list of files and directories, filter out the folders you want to exclude, and then pipe the result to Copy-Item. Here’s an example:
Get-ChildItem -Path "C:\Source" -Recurse | Where-Object { $_.PSIsContainer -eq $false } | Copy-Item -Destination "C:\Destination"

This command retrieves all files and directories recursively from the “C:\Source” directory, filters out the directories using the Where-Object cmdlet, and then copies only the files to the “C:\Destination” directory.

You can further refine the Where-Object filter to exclude specific folders based on their names. For example:

Get-ChildItem -Path "C:\Source" -Recurse | Where-Object { $_.PSIsContainer -eq $false -and $_.Directory -notmatch "Temp|Backup" } | Copy-Item -Destination "C:\Destination"

This command excludes folders named “Temp” or “Backup” from being copied.

These are a few ways to exclude folders when using Copy-Item in PowerShell.

Conclusion

The Copy-Item cmdlet in PowerShell is used to copy files and directories. I have explained how to use the PowerShell Copy-Item cmdlet using various examples.

You may also like:

Leave a Comment

Power Apps functions free pdf

30 Power Apps Functions

This free guide walks you through the 30 most-used Power Apps functions with real business examples, exact syntax, and results you can see.

Download User registration canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App