Copy a file with PowerShell

How to copy a file to another folder with windows PowerShell copy script? There are several efficient options to copy Windows files from the PS 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.

Copy files to another folder with PowerShell Copy-Item command

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.

To open the Windows 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.

PS command to copy a file from one folder to another existing folder keeping the same name and extension:

Copy-Item -Path C:\folder\file.txt -Destination C:\newfolder\file.txt

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

Example powershell script to copy all files with filter on name

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
Copy files with the Windows Powershell Copy-Item comand.
Copy file in Windows Powershell

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.

Explicitly enable copy confirmation prompt with a PS script

Use the PowerShell confirmation option: -Confirm

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.

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.