Powershell: How to Use Equivalent of xcopy


In a Linux environment, you can use the xcopy command to copy files and subfolders from one folder to another.

For example, you can use the following syntax to copy all files and subfolders from a folder named data1 to a folder named data2, using //s to specify that subfolders should be included.

 xcopy "c:\users\bobbi\data1" "c:\users\bobbi\data2" //s

To replicate this behavior in PowerShell, we can use the Copy-Item cmdlet with the following syntax:

Copy-Item "c:\users\bobbi\data1\*" -Destination "c:\users\bobbi\data2" -Recurse -Force -Verbose

Here is what the various commands do:

  • -Recurse: Specifies that all subfolders and their contents should be copied as well
  • -Force: Specifies that existing items with the same name in the destination folder should be overwritten
  • -Verbose: Specifies that PowerShell should output the names of the files being copied

The following example shows how to use this syntax in practice.

Example: How to Use Equivalent of xcopy in PowerShell

Suppose that we have a folder with the following path:

  • c:\users\bobbi\data1

We can use the Get-ChildItem -Recurse command to view all files in this folder:

We can see that this folder contains three text files.

Suppose that we would like to copy each of these files to a new folder with the following path:

  • c:\users\bobbi\data2

Assume that this new folder is currently empty.

We can use the following syntax to copy each of the files from the data1 folder to the data2 folder:

Copy-Item "c:\users\bobbi\data1\*" -Destination "c:\users\bobbi\data2" -Recurse -Force -Verbose 

The following screenshot shows how to use this syntax in practice:

PowerShell xcopy equivalent

By using the -verbose argument, we are able to see the names of the files being copied to the new folder.

When we use the Get-ChildItem cmdlet once again to view the contents of the data2 folder, we can see that each of the files from the data1 folder have been copied into it.

Note: You can find the complete documentation for the Copy-Item cmdlet in PowerShell here.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Use ‘cut’ Equivalent
Powershell: How to Use ‘touch’ Equivalent
PowerShell: How to Use Equivalent of mkdir -p

Leave a Reply