How To Copy Files With PowerShell

One of the most basic and 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 to copy files. At its simplest, the Copy-Item cmdlet only requires you to specify a source and destination for the file you want to copy.

That source is the path and name of the file you want to copy while the target is the folder where you want to copy the file.

How do you use the Copy-Item cmdlet?

To demonstrate the Copy-Item cmdlet, I created a text file called Important Document.txt. This document will be saved in the C:\Users\Brien\Documents folder as shown in Figure 1. Use PowerShell to copy this document to a folder named C:\Temp.

Brien Posey\Users\Brien\Documents

Illustration 1. The Important Document file is located in C:\Users\Brien\Documents.

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

Copy-Item -Path “C:\Users\Brien\Documents\Important Document.txt” -Destination “C:\Temp”

Figure 2 shows the process of copying the file. Although PowerShell doesn’t give you a visual indication that the operation was successful, you can verify it yourself. In this example, I first navigated to the C:\Temp folder. I then used the Get-ChildItem cmdlet to view the contents of that folder, which confirmed that the file was indeed copied.

Brien Posey\Temp folder

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

Working with PowerShell placeholders

In the example above, I needed to copy a specific file. In the real world, you often need to copy multiple files, not just one. Luckily, PowerShell makes this easy.

PowerShell uses the asterisk symbol as a wildcard. So if you want to copy all files to a specific location, just replace the filename with an asterisk.

When working with wildcards, keep in mind that Windows systems typically append a three-letter extension to filenames. In the example above, the Important Document file had a .TXT extension, indicating that the document is a text file. When working with wildcards, you can use an asterisk in place of the filename, the extension, or both. Let me show you some examples.

Suppose the C:\Users\Brien\Documents folder I used earlier contains many documents, not just one. Some of these documents are text files (with a .TXT extension), while others are Microsoft Word documents (with a .DOCX extension). With this in mind, let’s imagine that we only want to copy the text files, not the Word documents. The easiest way to achieve this would be to use this command:

Copy-Item -Path “C:\Users\Brien\Documents\*.txt” -Destination “C:\Temp”

This command is essentially the same command I showed you earlier, except that I replaced the file name (Important Document.txt) with *.txt. This will tell PowerShell to copy all files with a .TXT extension to the specified folder.

What if we wanted to copy every file in the source path regardless of its type? To do this, we would use the same command as above, but replace *.txt with *.*. Therefore the command would become:

Copy-Item -Path “C:\Users\Brien\Documents\*.*” -Destination “C:\Temp”

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

Brien PoseyPowerShell screenshot shows that replacing the filename with *.* will result in PowerShell copying every file in the folder

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

How to copy subfolders

Occasionally, the files you need to copy may not all be in the same folder. A folder can contain files with one or more subfolders that also need to be copied. In these situations, you can still use the Copy-Item cmdlet, but you must append the -Recurse parameter.

The -Recurse parameter tells PowerShell to apply the command to the current folder and all subfolders.

In Figure 4 you can see that the C:\Users\Brien folder contains a number of subfolders, many of which contain files.

Brien PoseyThe \Users\Brien folder contains several subfolders

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 it with this command:

Copy-Item -Path ”C:\Users\Brien\*” -Destination “C:\Temp” -Recurse

Note that I had to use * instead of *.*. This is because folder names generally don’t include extensions. By the way, if you copy folders in this way, you will get an error message for all folders for which you do not have the necessary permissions.

Figure 5 shows the command I used to copy this entire folder structure to the C:\Temp folder. Notice the errors I got. These errors are from using logical folder names (e.g. My Music instead of just Music) and not a permissions issue.

Brien PoseyScreenshot shows -Recurse parameter causes subfolders to be copied

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

Figure 6 shows the copy results.

Brien Posey\Temp folder

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

Conclusion

PowerShell’s Copy-Item cmdlet is the entry-level command for copying files. This simple command can copy single files, multiple files, and even entire file structures.

Leave a Reply

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