How to Copy Files Using Powershell on Windows 11/10/8/7🔥

Jerry updated on May 04, 2024 to Backup and Recovery | How-to Articles

To copy files using PowerShell, you can use the Copy-item cmdlet. The copy-item command helps you copy files from one location to another in various situations. This article details 8 cases to make use of PowerShell to copy files.

Command-line programs are handy for managing your files and folders on the PC. While there are many command-line functions, the most used one is copying files. With Copy Files, you can copy files from the source to the destination. PowerShell is a more advanced version of the CMD and lets you copy files faster than it. The post from EaseUS details the PowerShell Copy-item cmdlet and how to copy files using PowerShell. Hurry up and learn!

Can You Copy the File with PowerShell

PowerShell is a powerful command-line tool featuring an incredible shell, scripting languages, and a configuration framework. While PowerShell is available by default on Windows, you can download it for macOS and Linux. The copy-item command helps you copy files from a source to a destination. While copying the files, if the destination has the same file, it will overwrite it.

Other command shells tend to delete or cut the files once they copy the content, but PowerShell does not delete or cut. Also, the flexibility of copy-pasting the files using the cmdlet depends on the PowerShell provider, which determines the terms. For example, users can copy files and directories in a files system drive and entries and registry keys in the registry drive.

If you want to learn how to use PowerShell to move files from one folder to another, you can refer to the following article.

Powershell Move Files from One Folder to Another

This article discusses some PowerShell commands to perform various operations to move files from one folder to another. Read more >>

Backup Files to Different Locations Instead of Copying

While dealing with data, there can be data loss situations when transferring or copying files. Many command line tools delete the files in the source folder when they try to copy to the destination. Under such circumstances, you may lose data. Accidental deletion of data is common with command line tools when performing copy operations.

Under such circumstances, you can try professional software to help you. A professional tool like EaseUS Todo Backup will secure your data and help you restore it in accidents. It is a free backup and recovery program for Windows PCs.

It can help you automatically back up files or create system images for hard disk failure recovery. With a few clicks, EaseUS creates backups and offers flexible storage options for files, folders, hard drives, and systems. You can store the backups on the PC, external drive, or the cloud. AS one of the best file sync software, it can help you sync files to different locations without copying.

With advanced algorithms and features, you can achieve backup and restoration efficiently. Download EaseUS Todo Backup to protect your data.

Step 1. To initiate a backup, click the Select backup contents option.

Step 2. There are four data backup categories, File, Disk, OS, and Mail, click File.

Step 3. On the left, both local and network files will be shown. You may extend the directory to pick the backup files.

Step 4. Follow the on-screen instructions and choose a location to store the backup.

Click Options to encrypt the backup with a password in Backup Options, then click Backup Scheme to specify the backup schedule and start the backup at a certain event. There are also other additional choices you may choose to tailor the backup process to your requirements.

Step 5. The backup may be stored on a local disk, EaseUS' online service, or NAS.

Step 6. EaseUS Todo Backup enables customers to back up data to a third-party cloud drive as well as its own cloud drive.

If you want to back up data to a cloud drive provided by a third party, choose Local Drive, scroll down to add Cloud Device, and then add and log in to your account.


You can also choose the EaseUS cloud drive. Sign up for an EaseUS Account, then click EaseUS Cloud and log in.

Step 7. Click Backup Now to begin the backup process. The backup job will be presented as a card on the left side of the panel after it has been finished. Right-click the backup job to handle it further.

Share this article on your social media to help more people copy files using PowerShell.

 

How to Copy Files with PowerShell on Windows 11/10/8/7

The copy-item cmdlet helps you copy files under various situations. Below are some common examples of copying files using PowerShell's copy item.

  1. Case 1. Copy Files to a Specified Directory
  2. Case 2. Copy Files to an Existing Directory
  3. Case 3. Copy File to a New Directory
  4. Case 4. Copy File to a Specified Directory and Rename It
  5. Case 5. Copy File to a Remote Computer
  6. Case 6. Copy Folder to Remote Computer
  7. Case 7. Copy Remote File to Local Computer
  8. Case 8. Copy Contents of Remote Folder to Local Computer

Case 1. Copy Files to a Specified Directory

A common situation requiring a copy item is when you want to copy files to a specified directory. We assume you want to copy the mongodb.log.txt files to the Logs directory in the C drive. The steps to do this:

Step 1. Click on Start, and type PowerShell. Run PowerShell with admin privileges.

Step 2. Enter the copy-item cmdlet below in the PowerShell and execute it.

  • Copy-Item "F:UserDataMyDocumentsmongodb.log.txt" -Destination "C:Logs"

Case 2. Copy Files to an Existing Directory

We have a "C:Logs" directory, and you want to copy its content to the existing "C:BetaDev" directory. The command will not copy the Logs directory but all the subdirectories, including the file trees. The container parameter is True by default, which preserves the directory structure.

  • Copy-Item -Path "C:Logs*" -Destination "C:BetaDev" -Recurse

Tip: To include the Logs directory and all the subdirectories and files, remove the * symbol, and you are good to go.

  • Copy-Item -Path "C:Logs" -Destination "C:BetaDev" -Recurse

Case 3. Copy File to a New Directory

A frequent situation is when you want to copy files to a new directory. Let's see an example of copying the "C:Logs" contents to a new directory GameLogs in the "C:Drawings" directory.

If you want to contain the source directory name, you need to copy it to an existing directory as Case 2 or rename the new directory as the source directory.

  • Copy-Item -Path "C:Logs" -Destination "C:DrawingsGameLogs" -Recurse

Tip: To copy all the contents of the source, including the tree structures and subdirectories, add * to the command. Replace the "C:Logs" with "C:Logs*".

Case 4. Copy File to a Specified Directory and Rename It

If you want to copy files to a specified directory and rename them, the command below will help you. For example, run the command below to copy the Desktop-Widgets.ps1 file from "\Server01Share" directory to "\Server12ScriptArchive" directory.

  • Copy-Item "\Server01ShareDesktop-Widget.ps1" -Destination "\Server12ScriptArchiveGet-Widget.ps1.txt"

Upon execution, the PowerShell Copy-item cmdlet will rename the Desktop-Widget.ps1 to Get-Widget.ps1.txt.

Case 5. Copy File to a Remote Computer

Since PowerShell is an advanced command line shell, it allows users to copy a file to a remote computer. This works as a session; you can copy the files to the remote system. Let us assume a remote computer named "Server01" with a credential of "ControsoUser01" and store the results in variable names $Session.

So, we will be copying the text.log file from D:MyFolder01 folder to the C:MyFolder01_C folder on the remote system using the session information stored in the $Session variable.

  • $Session = New-PSSession -ComputerName "Node01" -Credential "ContosoUser01"
  • Copy-Item "D:MyFolder01test.log" -Destination "C:MyFolder01_C" -ToSession $Session

The first command creates a session (connection) with the remote PC; the second helps you copy the files.

Case 6. Copy Folder to Remote Computer

Similarly, you can copy the entire folder to the remote PC using the PowerShell copy folder. Now, we will copy the "D:MyFolder02" folder to "C:MyFolder02_C" directory.

Note: Any files or subfolders are not copied to the remote computer without the recurse parameter. 

  • $Session = New-PSSession -ComputerName "Node02" -Credential "ContosoUser01"
  • Copy-Item "D:MyFolder02" -Destination "C:MyFolder02_C" -ToSession $Session

Share this article on your social media to help more people copy files using PowerShell.

 

Case 7. Copy Remote File to Local Computer

We will first create a connection between the local computer named "Node01" and credential "ContosoUser01" using PowerShell. Similar to the above process, you can do vice versa to copy remote files to a local PC.

The command to copy the "test.log" file from the remote PC directory "C:MyRemoteFile" to the local directory "D:MyLocalFile."

  • $Session = New-PSSession -ComputerName "Node01" -Credential "ContosoUser01"
  • Copy-Item "C:MyRemoteFiletest.log" -Destination "D:MyLocalFile" -FromSession $Session

Case 8. Copy Contents of Remote Folder to Local Computer

Assume we want to copy the "C:MyRemoteDataFolder" entire folder in the remote PC to the "D:MyLocalDataFolder" folder.

  • $Session = New-PSSession -ComputerName "Node01" -Credential "ContosoUser01"
  • Copy-Item "C:MyRemoteDataFolder" -Destination "D:MyLocalDataFolder" -FromSession $Session

To Wrap Things Up

That comes to an end about how to copy files using PowerShell. Copying files from one point to another using Command-Line tools is handy. PowerShell offers robust copy methods. The post details how to copy files using PowerShell in various situations. We have listed 8 common situations where PowerShell can help you copy files.

Copy commands using command-line tools can potentially cause data loss. The commands, upon failing, result in data ambiguity. Make your data backup now with EaseUS Todo Backup to protect your data. It can help you back up and sync files to different locations and recover them whenever needed. Just download and try it!

FAQs on Copying Files Using PowerShell

Here are some frequently asked questions about copying files using PowerShell.

1. How to delete a file or folder using PowerShell?

PowerShell offers a Remove-item cmdlet to remove files or folders. You can use the Remove-Item -path C:\Temp\file.txt to remove any file and specify a folder to remove the folder. Additionally, you can use the * symbol to delete multiple files or folders at once.

2. What is the difference between copy and xcopy in PowerShell?

The copy command helps copy files or data in PowerShell, whereas the Xcopy is used to copy multiple files and directory structures from one to another.

3. How to copy multiple files from one folder to another using PowerShell?

To copy multiple files with copy-item, you need to separate all the files with a comma. Take a look at this example to understand better: Copy-Item .\PowerShellcopycommands.jpg,.\multiplefiles.xml -Destination D:\CopyContent\ .