Copy-Item: How to Use it to Copy Files via PowerShell?

PowerShell is a powerful command-line shell and scripting language used to automate various tasks. Moreover, users can also use PowerShell to copy files or folders from the source to the destination. Copy-Item is a PowerShell cmdlet that allows users to copy files and directories from one location to another.

This cmdlet is capable of copying items from local and remote locations and over the network. It also provides various parameters that users can use to write a script on how it will copy items. It’s a versatile command that can handle a wide range of copying tasks, making it an essential part of any PowerShell user’s toolkit. This write-up will explain how to use PowerShell to copy files and folders. So, read this write-up to the end and know how to use it.

Understanding its Basic Syntax

The basic syntax of the PowerShell Copy-Item cmdlet is:

Copy-Item -Path <SourcePath> -Destination <DestinationPath>
  • Path: Path of the source or destination.
  • Source: Location of the file or folder you want to copy.
  • Destination: Location of the target where you are going to copy the file or folder.

How to use the PowerShell Copy-Item cmdlet?

Users can use the Copy-Item cmdlet for various tasks, including copying single and multiple items, handling errors, and many more. Here, we will explain each one by one.

1. Copying a single file:

If you need to copy only a single file from one location to another, the command will be:

Copy-Item -Path "C:\SourceFolder\example.txt" -Destination "C:\DestinationFolder"

This command copies (example.txt) a file from the Source Folder to the Destination Folder. That is how you can use the Copy-Item cmdlet of PowerShell to copy a single file from the source location to the destination.

Copying a single file via copy-item

2. Copying multiple files:

To use PowerShell to copy multiple files from the source, you need to separate each file in the command. You can also copy all at once if the items or files are of a specific type.

To copy multiple files separately:

Copy-Item -Path "C:\SourceFolder\example.txt", "C:\SourceFolder\example 2.txt", "C:\SourceFolder\example 3.txt" –Destination "C:\DestinationFolder"

To copy multiple files at once if they are of a specific type:

Copy-Item -Path "C:\SourceFolder\.txt" -Destination "C:\DestinationFolder"

It will copy all .txt files from the source folder to the destination folder at once.

Copying multiple files using copy commond

3. Copying a folder and its content:

We will use the -Recurse parameter to copy a folder and its content:

Copy-Item -Path "C:\SourceFolder" -Destination "C:\DestinationFolder" –Recurse

The -Recurse parameter will copy all content and the folder from the source to the destination.

Copying a folder and its content3

4. Overwriting Existing Files:

Copy-Item cmdlet overwrites the file in the destination if the file has the same name in the destination by default. However, you can control overwriting existing files by using the Test-Path cmdlet.

if (-Not (Test-Path "C:\DestinationFolder\example.txt")) {Copy-Item -Path "C:\SourceFolder\example.txt" -Destination "C:\DestinationFolder"}

This command will ensure that the file does not already exist in the destination; if it does, it will not overwrite the existing file.

Overwriting Existing Files4

5. Preserving File Attributes:

We need to include the –Force parameter in the command to preserve file attributes when copying from one location to another.

Copy-Item -Path "C:\SourceFolder\example.txt" -Destination "C:\DestinationFolder" –Force

This command ensures that the hidden or read-only files will copy without any errors. It is beneficial when you are working with the protected directories.

Preserving File Attributes5

6. Using PowerShell Variable for Dynamic Paths:

Using the variables in the command, you can make the script better organized and readable, particularly when working with multiple files.

  • $source = “C:\SourceFolder\example.txt”
  • $destination = “C:\DestinationFolder”
Copy-Item -Path $source -Destination $destination
Using PowerShell Variable for Dynamic Paths6

7. Handling Errors Delicately:

The Try-Catch cmdlet allows you to delicately handle errors when copying files from one location to another.

Try {Copy-Item -Path “C:\SourceFolder\example.txt” -Destination “C:\DestinationFolder”

Write-Host “File copied successfully.}

Catch {Write-Host “An error occurred: $_”}

It also makes sure that the script is powerful and recovers easily from unexpected issues, like missing files or folders, etc.

Handling Errors Delicately with copy-item command

Advanced Usage of the PowerShell Copy-Item Cmdlet

Here, we will share how you can copy files or folders over a network and how you can sort items or files or folders using the Copy-Item PowerShell cmdlet.

1. Copying files over a Network:

To copy files over a network, you need to provide the UNC path as the destination. Here is how to do it.

Copy-Item -Path "C:\SourceFolder\example.txt" -Destination "\\NetworkShare\Folder"

You need the required permissions to copy files to the server or shared drive.

2. Excluding specific files or folders:

You need to use additional logic with Get-ChildItem cmdlet to exclude specific files or folders.

Get-ChildItem -Path "C:\SourceFolder" -Recurse | Where-Object { $_.Name -ne "exclude.txt" } | ForEach-Object {Copy-Item -Path $_.FullName -Destination "C:\DestinationFolder"}
Excluding specific files or folders8

This command will allow you to filter unwanted items. It is beneficial for complex tasks, like backing up selective items.

What are the common parameters for Copy-Item?

The common parameters for the Copy-Item PowerShell cmdlet are as follows:

  • -Recurse: To copy all items along with the folder and subfolders.
  • -Force: To copy hidden files and overwrite read-only files.
  • -Filter: Filter for specific file selection.
  • -Verbose: To provide detailed information about the operation.

Example of –Verbose parameter:

Example of –Verbose parameter9

Final Remarks

The Copy-Item cmdlet of the PowerShell is helpful for copying files or folders from one location to another. Along with that, it can copy items to remote locations and over a network. In this write-up, we have shared a detailed guide on how users can use the Copy-Items PowerShell cmdlet to copy single files, multiple files, a folder and its content, and handle errors gracefully. This cmdlet automates tasks and provides you with the flexibility and functionality to even do complex tasks.


People also Read:
  1. How to Use “Add-Mailboxpermission” in Exchange Online
  2. Connect to Exchange Online PowerShell with MFA? Quick Guide
  3. How to Mount an Exchange Database On Different Server?
  4. How to Manage Outlook External Email Warning in Office 365?
  5. PowerShell vs Cmd: Difference & Which to Use & When?
Vishesh Kumar
Scroll to Top