PowerShell copier des fichiers avec Copy-Item

PowerShell and Copy-Item to copy files

I. Presentation

In this tutorial, we will learn how to use the “Copy-Item” PowerShell command which is useful for copying a file from one folder to another folder, but also a set of data. We’ll see how to use this cmdlet through several examples, as it comes in handy for copying data. These examples make it possible to carry out simple actions, but which respond to frequent cases! This is an alternative to the historical command “xcopy“, but also to the excellent “robocopy“.

II. Copy a file with PowerShell

Let’s start simple with copying a file from folder A to folder B. For this demo, I’m working in the “C:\TEMP\PS\” folder, where I create “FolderA”, “FolderB”, and a file named “it-connect-1.txt” in “FolderA”.

New-Item -ItemType Directory -Path "C:\TEMP\PS\" -Name "DossierA"
New-Item -ItemType Directory -Path "C:\TEMP\PS\" -Name "DossierB"
New-Item -ItemType File -Path "C:\TEMP\PS\DossierA\" -Name "it-connect-1.txt"

Now, we will move this file from “FolderA” to “FolderB”:

Copy-Item -Path "C:\TEMP\PS\DossierA\it-connect-1.txt" -Destination "C:\TEMP\PS\DossierB\"

The file is copied (and not moved):

PowerShell - Copy-Item - Copier un fichier

In the previous example, we keep the same name between the source and the destination. However, you can make the file change name in the destination. For example, let it be called “it-connect-2.txt”:

Copy-Item -Path "C:\TEMP\PS\DossierA\it-connect-1.txt" -Destination "C:\TEMP\PS\DossierB\it-connect-2.txt"

You can also shorten this command, using “Copy” which is the alias of “Copy-Item” is by specifying in order, source and destination.

Copy "C:\TEMP\PS\DossierA\it-connect-1.txt" "C:\TEMP\PS\DossierB\"

III. Copy files by applying a filter

For this other practical case, we will see how to copy files by applying a filter in order to include or exclude only files with a specific extension. To have a little more material, I created some TXT files and JPG files in my “DossierA” folder:

PowerShell - Copy-Item - Copy with filter

I decide to copy from “FolderA” to “FolderB” only the “.JPG” files. Which give a filter with the “-Include” parameter and at the level of the source path, we also add the wildcard character (“*”) to include all the contents of this folder (which we filter using -Include):

Copy-Item -Path "C:\TEMP\PS\DossierA\*" -Destination "C:\TEMP\PS\DossierB\" -Include *.jpg

Note that in the previous example, based on “-Include“, it is not not possible to add “-Recurse” and therefore to include the files corresponding to the filter and present in the subfolders. The “-Include” filter only applies to the top level folder. On the other hand, the parameter “-Resource” works with exclusions.

In my folder, I have “TXT” files and “JPG” files, if I want to copy only JPG files, I have to exclude the “TXT” extension, like this:

Copy-Item -Path "C:\TEMP\PS\DossierA\*" -Destination "C:\TEMP\PS\DossierB\" -Exclude *.txt -Recurse

Adding “-resort” also allows you to copy files located in sub-folders, also creating the folder in the destination in order to reproduce the tree structure.

IV. Copy a file to a remote computer

The “Copy-Item” cmdlet does not contain a “-Computer” parameter to specify a destination computer, but it has a “-ToSession” parameter which allows data to be copied to a remote computer using a PowerShell session.

Here is an example below, knowing that the opposite is also possible: copy data from remote computer to local computerusing the “-FromSessionon the same principle.

This implies toenable Windows Remote Management (WinRM) and of create a powershell session.

In this example, the session targets the “PC-01” computer and I authenticate with the “IT-Connect\florian” account.

$Session = New-PSSession -ComputerName "PC-01" -Credential "IT-Connect\florian"

Once the session is in place, you can copy a file to the remote computer by calling the session. Here, we copy “C:\Temp\PS\DossierA\it-connect-1.txt” to “C:\TEMP\” (local path on the target computer).

Copy-Item -Path "C:\Temp\PS\DossierA\it-connect-1.txt" -Destination "C:\TEMP\" -ToSession $Session

PowerShell - Copy-Item - Copy to remote computer

V. Copy an entire folder with PowerShell

Another case: copying an entire tree from one location to another. Thus, we can copy all the contents of “FolderA”, that is to say its files, as well as its subfolders and their content, to “FolderB”.

Copy-Item -Path "C:\TEMP\PS\DossierA\*" -Destination "C:\TEMP\PS\DossierB\" -Recurse

If we remove the “-Recurse” parameter, then we only copy the files located in the top-level directory. In other words, we ignore the subfolders.

VI. Copy multiple folders to a single location

If you want to copy the content of several source folders to the same destination (merge the content of the folders), it will be necessary to separate the source directories with a comma. As for the destination folder, it is not mandatory to create it: it can be created during the copy.

To move the contents of the “FolderA” and “FolderB” folders to “FolderC”, this gives:

Copy-Item -Path "C:\TEMP\PS\DossierA\*","C:\TEMP\PS\DossierB\*" -Destination "C:\TEMP\PS\DossierC\"

In the same spirit with recursion:

Copy-Item -Path "C:\TEMP\PS\DossierA\*","C:\TEMP\PS\DossierB\*" -Destination "C:\TEMP\PS\DossierC\" -Recurse

Beware of possible conflicts on names, which can generate some errors without preventing the rest of the copy.

VII. Conclusion

You are now able to copy files with PowerShell using the Copy-Item command! If you have an idea for an additional scenario, feel free to leave a comment on this article.

Powershell,Scripting,

#PowerShell #CopyItem #copy #files

Leave a Comment

Your email address will not be published. Required fields are marked *