fbpx

How To Copy File Using Copy-item In PowerShell? 6 Best Use Case

The PowerShell Copy-Item cmdlet is the most often used method in the PowerShell world for moving a copy of a file or folder from point A to point B in your PowerShell script. This cmdlet gives us the ability to recurse through files in a folder while copying a file and a folder.

The Copy-Item cmdlet in PowerShell can be used to move a file from one place to another. The destination could be a faraway computer or another local folder. In addition to files, we can also copy entire folders, including their contents and subfolders. The PowerShell provider cmdlets include this cmdlet. It is a general-purpose cmdlet that is identified by the Item noun.

Method To Use Copy-item In PowerShell

  1. Basic Usage Of Copy-item In PowerShell (copy file and folder)
  2. PowerShell Copy Multiple Files
  3. Copy and Merge Multiple Folders
  4. PowerShell copy item recurse
  5. Filter Files to Copy (include and exclude parameter)
  6. Copy File to Remote Computer with PowerShell

1. Basic Usage Of Copy-item In PowerShell (copy file and folder)

Copy-Item is a command in PowerShell that allows you to copy files and directories from one location to another. It’s a versatile command that can copy single or multiple files, as well as entire directories.

Here’s an example of how to use Copy-Item:

Copy-Item -Path "C:\Folder1\File1.txt" -Destination "C:\Folder2

This command will copy File1.txt from Folder1 to Folder2.

You can also use wildcards to copy multiple files at once:

Copy-Item -Path "C:\Folder1\*.txt" -Destination "C:\Folder2"

This command will copy all files with the .txt extension from Folder1 to Folder2. There are many other options and parameters you can use with Copy-Item to customize your file-copying operations in PowerShell.

2. PowerShell Copy Multiple Files

With PowerShell, you may either choose several files in the path or use filters to transfer numerous files at once. You must use commas to divide each file in order to achieve this. You will need to use a folder for the destination.

Copy-Item -Path D:\Power\sample.txt,D:\Power\sample100.txt -Destination D:\ABC
Copy-Item -Path D:\Power\sample.txt,D:\Power\sample100.txt -Destination D:\ABC

3. Copy and Merge Multiple Folders

The Copy-Item cmdlet has the ability to copy and combine numerous directories at once, which you generally won’t use very often but which can be very useful. To the cmdlet, we can add several source routes and a single destination. In this manner, the content from all sources will be copied to a single spot.

Copy-Item -Path D:\Power\sample.txt,D:\Power\sample100.txt -Destination D:\ABC

4. PowerShell Copy Item Recurse

The Copy-Item cmdlet also allows us to move a whole folder to a new place. However, it’s crucial to use the right ending in the source and destination paths when transferring folders.

It will positively look in each subdirectory and copy all files and directories in each recursively using the Recurse argument on Copy-Item.

c:\temp    d:\temp    "Creates only the folder temp"
c:\temp\    d:\temp -Recurse    "Copies folder inc subfolders and all content"

Use a wildcard in the source path to copy only the files and empty subfolders from the file location.

We must use the -recurse argument when using PowerShell to copy all material, including subfolders. This transfers the contents of the subfolder to the new location as well:

Copy-Item -Path D:\Power\* -Destination D:\ABC -recurse
Copy-Item -Path D:\Power\* -Destination D:\ABC -recurse
copied folders in new location

5. Filter Files to Copy (include and exclude parameter)

The Include and Exclude arguments allow us to specify which files to copy and which not to copy. The filter is matched against folder names in your source path as well as the complete file name (including extension).

Include  Parameter

In order to copy only files with the.txt extension, use the include filter as follows:

Copy-Item -Path D:\Power\* -Destination D:\ABC -include *.txt
Copy-Item -Path D:\Power\* -Destination D:\ABC -include *.txt

Keep in mind that the wildcard is always necessary if you wish to filter on a certain name component. That the -Recurse argument won’t function in this situation is good to know. Txt files in subfolders won’t be copied if you have any. 

Exclude parameter

The process of excluding files is the same. The names of the files and folders that we don’t wish to copy can be specified down to the last letter. Remember that a subfolder’s entire contents will be duplicated if the exclusion string doesn’t match it.

Copy-Item -Path D:\Power\* -Destination D:\ABC -exclude *.txt
Copy-Item -Path D:\Power\* -Destination D:\ABC -exclude *.txt
files are copied to new location

6. Copy The File to Remote Computer with PowerShell

PowerShell can also be used to copy files to and from a distant machine. There are two ways to accomplish this. The first is by using the New-PSDrive cmdlet to establish a temporary network drive. Or by opening a fresh PowerShell connection to a distant machine first.

You must either have access to the remote computer for this to operate or establish a PSSession using the credentials of the remote computer.

$Session = New-PSSession -ComputerName "joinadmin5" -Credential "joinadmin5\30od!@nD@"

We can copy stuff to any location on the other machine using the sessions that are established and kept in the $session variable. In this scenario, the destination path is the local path on the remote computer, so you can simply use D:\Newfolder as the destination path to copy files to the remote machine’s temporary folder.

Copy-Item -Path D:\temp\local\* -Destination D:\New folder -ToSession $Session -Recurse
Copy-Item -Path D:\temp\local\* -Destination D:\New folder -ToSession $Session -Recurse

A similar technique may be used with PowerShell to copy files from a remote computer to your local machine. Instead of using the -ToSession parameter in this situation, we merely need to use the -FromSession parameter.

Copy-Item -Path D:\temp\local\* -Destination D:\New folder -FromSession $Session -Recurse
Copy-Item -Path D:\temp\local\* -Destination D:\New folder -FromSession $Session -Recurse