How To Copy Files With PowerShell | ITPro Today: IT News, How-Tos, Trends, Case Studies, Career Tips, More

2022-10-11 05:43:14 By : Ms. Angela Zhang

IT Pro Today is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

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.

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.

Related: How to Use PowerShell to Navigate the Windows Folder Structure

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.

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:\Users\Brien\Documents folder, as shown in Figure 1. Let’s use PowerShell to copy this document to a folder named C:\Temp.

Figure 1. The Important Document file resides in C:\Users\Brien\Documents.

In this case, the command used to copy Important Document.txt to the C:\Temp folder in is:

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.

Figure 2. PowerShell has copied the document to the C:\Temp folder.

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:\Users\Brien\Documents 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:

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:

You can see what this process looks like in Figure 3.

Figure 3. Replacing the filename with *.* causes PowerShell to copy every file in the folder.

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:\Users\Brien folder contains a number of subfolders, many of which contain files.

Figure 4. The C:\Users\Brien 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:

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.

Figure 5. The -Recurse parameter causes subfolders to be copied.

Figure 6 shows the copy results.

Figure 6. The folder structure was successfully copied to the C:\Temp folder.

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.

More information about text formats