How to Copy Files from One Folder to Another in PowerShell?

Do you need to copy files from one folder to another folder? In PowerShell, it is easily possible. In this tutorial, I will show you how to copy files from one folder to another folder in PowerShell using various methods.

To copy a file from one folder to another in PowerShell, you can use the Copy-Item cmdlet. For example, Copy-Item -Path “C:\Source\file.txt” -Destination “C:\Destination” will copy file.txt from the Source to the Destination folder. For multiple files or directories, include wildcards or use the -Recurse parameter to copy entire directory contents, preserving the structure.

Copy Files from One Folder to Another in PowerShell using Copy-Item

The simplest way to copy files from one folder to another in PowerShell is by using the Copy-Item cmdlet. This cmdlet is designed to copy files, folders, and even registry keys from one location to another. Here’s a basic example of how to use Copy-Item to copy a single file:

Copy-Item -Path "C:\Source\file.txt" -Destination "C:\Destination"

This command will copy file.txt from the C:\Source directory to the C:\Destination directory. If the destination file already exists, PowerShell will overwrite it by default.

After I executed the script using VS code, you can see the output in the screenshot below:

Copy Files from One Folder to Another in PowerShell

Copy Multiple Files from One Folder to Another

To copy multiple files from one folder to another in PowerShell, you can use wildcards. For example, to copy all .txt files from one folder to another, you could use the following command:

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

This command will copy all files with the .txt extension from the C:\Source directory to the C:\Destination directory.

Recursive Copy

If you want to copy all the contents of a directory in PowerShell, including subdirectories, you can use the -Recurse parameter:

Copy-Item -Path "C:\Source\*" -Destination "C:\Destination" -Recurse

This command will copy all files and folders from C:\Source to C:\Destination, maintaining the directory structure.

Preserving Permissions

To preserve file permissions when copying, use the -Force parameter:

Copy-Item -Path "C:\Source\file.txt" -Destination "C:\Destination" -Force

This will ensure that the file’s original security permissions are retained in the new location.

Advanced Copying Techniques

Copying Files with Filtering

PowerShell allows you to copy files based on certain criteria or filters. For instance, if you want to copy files that have been modified in the last 7 days, you could do something like this:

Get-ChildItem -Path "C:\Source" -Recurse | 
Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(-7) } |
Copy-Item -Destination "C:\Destination"

In this script, Get-ChildItem retrieves all items in the source directory, Where-Object filters them based on the last write time, and Copy-Item copies the filtered items to the destination folder.

Combining Copy-Item with New-Item

Sometimes, you need to ensure that the destination directory exists before copying a file from one folder to another folder in PowerShell. You can combine Copy-Item with New-Item to do this:

New-Item -Path "C:\Destination" -ItemType Directory -Force
Copy-Item -Path "C:\Source\file.txt" -Destination "C:\Destination"

The New-Item cmdlet creates the destination directory if it doesn’t exist, and the -Force parameter ensures that it doesn’t throw an error if the directory already exists.

Using Robocopy

For more robust file copying, especially for larger files or a large number of files, you can use Robocopy (Robust File Copy), a command-line utility that comes with Windows. Here’s how you can use it within a PowerShell script:

robocopy C:\Source C:\Destination /E

The /E parameter tells Robocopy to copy subdirectories, including empty ones.

Error Handling While Copying from One folder to another in PowerShell

When copying files from one folder to another in PowerShell, handling errors is important. You can use the -ErrorAction parameter to specify how PowerShell should handle errors. For instance, to continue copying even if an error occurs, you can use:

Copy-Item -Path "C:\Source\*" -Destination "C:\Destination" -ErrorAction Continue

If you want to log errors to a file, you can redirect the error output:

Copy-Item -Path "C:\Source\*" -Destination "C:\Destination" -ErrorAction SilentlyContinue 2> "C:\log.txt"

Conclusion

Copying files from one folder to another is a common requirement that PowerShell can easily accomplish. You can use PowerShell’s basic Copy-Item cmdlet or more advanced techniques like filtering and Robocopy.

In this PowerShell tutorial, I have explained how to copy files from one folder to another in PowerShell using different methods with examples.

You may also like: