How to copy Windows files to another folder with PowerShell?
There are several efficient options to copy Windows files with the PowerShell command prompt. In PowerShell scripting, use the Copy-Items command in a Windows PowerShell prompt. It is also possible to copy from the GUI, but this option is very manual. Indeed, it is always possible to copy files and folders using the interface provided by Windows. But it is more efficient to make large and specific copies.
Table of Contents
Copy files with the PowerShell and Copy-Item
Indeed, Windows Shell scripting is much more efficient than manual copying. For example, to copy 500’000 text files with very specific extensions or names. And to make sure that they were all copied, without any error.
Or with strict error checking and recording. Manually, this operation would take days. The PowerShell Copy-Item command is the best option available.
1. Open the PowerShell command prompt
Let’s start the tutorial about hoy copy a file with Windows PowerShell. From the Windows 10 start menu, for example, type “Windows PowerShell” in the search field and click on the icon.
2. Copy a file to another folder
PS command to copy a file from one folder to another existing folder keeping the same name and the same extension.
Copy-Item -Path C:\folder\file.txt -Destination C:\newfolder\file.txt
3. Script to copy and rename a file
By the way, with Power Shell, to modify the name of the copy, simply change the name of the destination file.
Copy-Item -Path C:\folder\file.txt -Destination C:\folder\newfile.txt
4. PowerShell script to copy all files with a filter
That is, with a specific extension or a specific name before the separator. This sample script copies all text files, with a TXT extension, into the newfolder folder. And allows overwriting files if they already exist with the -Force option.
Copy-Item -Path C:\folder\file*.txt -Destination C:\newfolder -Force
Other Scripting options are available for the Copy-Item command, with for example:
- The first: -Force to overwrite the destination file.
- Or: -Recurse to copy recursively from source or destination, depending on where the keyword is in the command.
- The -Filter option to separate the filter from the copy source location.
Note that this list of options is not exhaustive.
5. Explicitly enable a copy confirmation prompt
Indeed, use these options to better manage and control file copies and avoid overwriting important, potentially unsaved files. This last copy PowerShell script example instructs the system to ask for confirmation before overwriting the files. Use the PowerShell confirmation option: -Confirm.
Copy-Item -Path C:\folder\file.txt -Destination C:\folder\newfile.txt -Confirm
In conclusion, this first article in the Scripting with Windows PowerShell series shows Scripting PS examples for copying one or more files in Windows. To take it a step further, here ‘s how to copy files and folders with a cmd script.