How To Copy Files With PowerShell
PowerShell’s Copy-Item cmdlet is the primary command for copying files. Learn how to copy individual files, multiple files, and entire file structures.
October 10, 2022
One of the most basic yet useful things you can do in PowerShell is copy a file from one location to another. In this article, I will show you how to use PowerShell’s Copy-Item cmdlet for all your file copying needs.
What is the Copy-Item Cmdlet?
Copy-Item is the primary cmdlet used in PowerShell for copying files. At its simplest, the Copy-Item cmdlet only requires you to specify a source and a destination for the file you want to copy.
The source is the path and name of the file that you want to copy, while the destination is the folder that you want to copy the file into.
How Do You Use the Copy-Item Cmdlet?
For the sake of demonstrating the Copy-Item cmdlet, I have created a text file named Important Document.txt. That document is stored in the C:UsersBrienDocuments folder, as shown in Figure 1. Let’s use PowerShell to copy this document to a folder named C:Temp.
PowerShell Copy 1
Figure 1. The Important Document file resides in C:UsersBrienDocuments.
In this case, the command used to copy Important Document.txt to the C:Temp folder in is:
Copy-Item -Path “C:UsersBrienDocumentsImportant Document.txt” -Destination “C:Temp”
Figure 2 displays the process of copying the file. Although PowerShell does not give you a visual indication that the process has succeeded, you can check yourself. In this example, I first navigated to the C:Temp folder. I then used the Get-ChildItem cmdlet to display that folder’s contents, which confirmed that the file was indeed copied.
PowerShell Copy 2
Figure 2. PowerShell has copied the document to the C:Temp folder.
Working With PowerShell Wildcards
In the example above, I needed to copy one specific file. In the real world, you commonly need to copy multiple files, not just one. Fortunately, PowerShell makes this easy to do.
PowerShell uses the asterisk symbol as a wildcard. That means that if you want to copy all the files in a specific location, just substitute an asterisk for the filename.
When working with wildcards, keep in mind that Windows systems normally append a three-character extension to filenames. In the example above, the Important Document file had an extension of .TXT, indicating that the document is a text file. When you are working with wildcards, you can use an asterisk in place of the filename, the extension, or both. Let me show you a few examples.
Suppose that the C:UsersBrienDocuments folder that I used earlier has many documents in it, not just one. Some of those documents are text files (with a .TXT extension), while others are Microsoft Word documents (with a .DOCX extension). With that in mind, let’s imagine that we want to copy only the text files, not the Word documents. The easiest way to accomplish this would be to use this command:
Copy-Item -Path “C:UsersBrienDocuments*.txt” -Destination “C:Temp”
This command is essentially the same command that I showed you earlier, except I have replaced the filename (Important Document.txt) with *.txt. This tells PowerShell to copy any file in the specified folder that has a .TXT extension.
What if we wanted to copy every file in the source path, regardless of its type? To do so, we would use the same command as above, but replace *.txt with *.*. Hence, the command would become:
Copy-Item -Path “C:UsersBrienDocuments*.*” -Destination “C:Temp”
You can see what this process looks like in Figure 3.
PowerShell Copy 3
Figure 3. Replacing the filename with *.* causes PowerShell to copy every file in the folder.
How To Copy Subfolders
Occasionally, you may find that the files you need to copy do not all reside in the same folder. A folder may contain files along with one or more subfolders that also need to be copied. In those situations, you can still use the Copy-Item cmdlet, but you will need to append the -Recurse parameter.
The -Recurse parameter tells PowerShell to apply the command to the current folder and to all subfolders.
In Figure 4, you can see that the C:UsersBrien folder contains a number of subfolders, many of which contain files.
PowerShell Copy 4
Figure 4. The C:UsersBrien folder contains several subfolders.
If I wanted to copy the entire folder structure to the C:Temp folder, I could do so by using this command:
Copy-Item -Path ”C:UsersBrien*” -Destination “C:Temp” -Recurse
Notice that I had to use * instead of *.*. The reason for this is that folder names generally do not include extensions. Incidentally, when you copy folders in this way, you will receive an error message for any folders for which you lack the necessary permissions.
Figure 5 shows the command that I used to copy this entire folder structure to the C:Temp folder. Notice the errors that I received. These errors stem from the use of logical folder names (for instance My Music rather than just Music) and not from a permissions problem.
PowerShell Copy 5
Figure 5. The -Recurse parameter causes subfolders to be copied.
Figure 6 shows the copy results.
PowerShell Copy 6
Figure 6. The folder structure was successfully copied to the C:Temp folder.
Conclusion
PowerShell’s Copy-Item cmdlet is the go-to command for copying files. This simple command can copy individual files, multiple files, and even entire file structures.
About the Author
You May Also Like