Home » PowerShell » PowerShell Copy-Item – Copy Files to Other Location

PowerShell Copy-Item – Copy Files to Other Location

Copy-Item cmdlet in PowerShell copies the file from one location to another location. It copies files and directories in the file system and registry keys and entries in the registry drive.

PowerShell Copy-Item doesn’t delete or cut the items while copying to another location. You can use Copy-Item to copy files to a remote computer.

In this article, we will discuss how to copy files to another location using the PowerShell Copy-Item cmdlet.

PowerShell Copy-Item

Copy-Item copies the files from the source to the destination.

Syntax

Copy-Item
    [-Path] <String[]>
    [[-Destination] <String>]
    [-Container]
    [-Force]
    [-Filter <String>]
    [-Include <String[]>]
    [-Exclude <String[]>]
    [-Recurse]
    [-PassThru]
    [-Credential <PSCredential>]
    [-WhatIf]
    [-Confirm]
    [-FromSession <PSSession>]
    [-ToSession <PSSession>]
    [<CommonParameters>]

Parameters

-Path: Specifies a path to the items to copy. String array and wildcard characters are permitted.

-Destination: Specifies the path to the other location where files will be copied. The default destination is the current directory.

-Recurse: Specifies to recursively copy files from the directory and subdirectories.

-Filter: Specify a filter to qualify the Path parameter.

Copy Files in PowerShell using the Copy-Item

Use the PowerShell Copy-Item cmdlet to copy the file from the source location to the destination location.

Let’s consider, that you have a file in the directory D:\PS\ and want to copy it to another location D:\PS\Config

Copy-Item -Path D:\PS\calc.txt -Destination D:\PS\Config\

In the above PowerShell script. the Copy-Item uses the Path parameter to specify the source location of the file item to copy.

Copy-Item uses the Destination parameter to specify the location path to which the file item will be copied.

It will copy the file to the destination location.

Note here, that we have specified the Destination path without a filename, hence file will be copied with the source file name.

You can rename the file while copying to Destination by providing the file name in the path.

The following PowerShell script copies the file to the destination location with a new name for the file with the Destination path.

Copy-Item -Path D:\PS\calc.txt -Destination D:\PS\Config\Calculation_R1.txt

Copy Folder Content Recursively to Destination

Using the PowerShell Copy-Item cmdlet, it copies the folder content recursively from the source location to the destination location.

Let’s consider an example to copy the content of the D:\PS\Config directory to the destination location D:\PS\Config-Backup

Copy-Item uses the Path parameter to specify the source location and Destination parameter for another existing directory path.

It copies all the files from the source directory recursively to the destination folder.

Copy-Item -Path D:\PS\Config\* -Destination D:\PS\Config-Backup -Recurse    

In the above PowerShell script, Copy-Item uses Recurse parameter to copy subdirectories and files from the source location to another location.

Note here that, it doesn’t copy the source directory, in our example Config.

PowerShell Copy Folder and Files Recursively

To copy the source folder and files, subfolders recursively to the destination location, use the PowerShell Copy-Item cmdlet.

Let’s consider an example, to copy the contents of the D:\PS\Config directory to another location D:\PS\Config-Backup recursively.

In the following PowerShell script, the Copy-Item uses the Path and Destination parameters to specify the source and destination directory respectively.

It uses Recurse parameter to copy the folder and files recursively to another location.

Copy-Item -Path D:\PS\Config -Destination D:\PS\Config-Backup -Recurse 

The output of the above PowerShell script to copy directory contents to other locations copies the D:\PS\Config folder and its files to location D:\PS\Config-Backup

Copy Files to Remote Computer

To copy files from the local computer to a remote computer, create a session with the remote computer’s name and credentials.

Copy-Item cmdlet has a ToSession parameter to specify the session name.

Let’s consider an example, to copy files to a remote computer named INCORP-EU-117.

Create a session with the remote computer INCORP-EU-117 with the credentials of ShellGeek\Admin and store the results in the $session variable.

Use the Copy-Item command to copy files to a remote computer using the ToSession parameter, source, and destination directory path.

# Create a session with remote computer using New-Session
$Session = New-PSSession -ComputerName "INCORP-EU-117" -Credential "ShellGeek\Admin"

# Use Copy-Item to copy files to a remote computer
Copy-Item "D:\PS\Config\" -Destination "C:\Config-BackUp\" -ToSession $session -Recurse

In the above PowerShell script, the Copy-Item cmdlet copies the file content from the source location to the remote computer recursively.

It copies subfolders with their file structure to a remote computer.

Cool Tip: How to copy files from Windows to Linux in PowerShell!

Copy Folders and Files of the Remote Computer to the Local Computer

To copy folders and files of a remote computer to the local folder, using the Copy-Item cmdlet, create a session with the remote computer and use the FromSession parameter in the Copy-Item command.

Let’s consider an example to copy the directory and files from a remote computer named INCORPP-EU-117.

Create a session using the New-Session cmdlet with a remote computer INCORP-EU-117 with credentials ShellGeek\Admin and store the results in the $session variable.

Use the Copy-Item cmdlet, it uses the remote computer path and local computer path.

It uses the FromSession parameter to specify the session created with a remote computer.

# Create a session with remote computer using New-Session
$Session = New-PSSession -ComputerName "INCORP-EU-117" -Credential "ShellGeek\Admin"

# Use Copy-Item to copy files to a remote computer
Copy-Item "C:\Log\Config-Backup" -Destination "D:\PS\Config" -FromSession $session -Recurse

In the above PowerShell script, the Copy-Item cmdlet copies the entire directory and its files from a remote computer to the local computer.

Use Filter to Copy Files

PowerShell Copy-Item cmdlet has an Include parameter to filter items to be copied to another location.

Let’s consider an example, the D:\PS\Config folder contains the files as given below:

  • D:\PS\Config\app-1.ps1
  • D:\PS\Config\app-1.config
  • D:\PS\Config\app-2.config
  • D:\PS\Config\app-3.config
  • D:\PS\Config\Get-PI.ps1

To copy the files that start with the name app should be copied to another location, use the Copy-Item cmdlet with the Include parameter.

# Copy the files that start with name app*
Copy-Item -Path D:\PS\Config\* -Destination D:\PS\Config-Backup -Include app*

# Get the directory content
Get-ChildItem -Path D:\PS\Config-Backup\*  

In the above PowerShell script, the Copy-Item cmdlet uses the Path parameter to specify the source location and the Destination parameter to specify another location path.

Copy-Item has an Include parameter where it filters file name starting with an app*

It copies files with the name of the file starting with an app* to the destination path.

The output of the above PowerShell to use filter to copy files is:

PowerShell Copy File using Filter
PowerShell Copy File using Filter

Cool Tip: How to copy files newer than the date in PowerShell!

Conclusion

I hope the above article on how to use the Copy-Item cmdlet in PowerShell to copy files to the destination location is helpful to you.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.