PowerShell: How to Copy Content of Folder to Another Folder


You can use the following syntax in PowerShell to copy the content of one folder to another folder:

$SourceDir = "c:\users\bobbi\data1\first\*"
$DestDir = "c:\users\bobbi\data1\second"

Copy-Item -Force -Path $SourceDir -Dest $DestDir

This particular example copies all of the files from the folder named first to another folder named second.

Note that we use the -Force parameter to specify that any files in the destination folder with the same name should be overwritten by the new file being copied from the source folder.

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

Example: How to Copy Content of Folder to Another Folder in PowerShell

Suppose that we have a folder named first. We can use the following syntax to view the files in this folder:

$SourceDir = "c:\users\bobbi\data1\first\*" 
Get-ChildItem $SourceDir

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

We can see that this folder contains three files:

  • team_A.txt
  • team_B.txt
  • team_C.txt

And suppose we have another folder named second:

We can use the following syntax to view the files in this folder:

$DestDir = "c:\users\bobbi\data1\second" 
Get-ChildItem $DestDir

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

We can see that this folder only contains one text file:

  • team_A.txt

Suppose that we would like to copy all files from the folder named first to the folder named second and overwrite any existing files in the second folder with the same name.

We can use the following syntax to do so:

$SourceDir = "c:\users\bobbi\data1\first\*"
$DestDir = "c:\users\bobbi\data1\second"

Copy-Item -Force -Path $SourceDir -Dest $DestDir

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

PowerShell copy content of folder to another folder

When we use the Get-ChildItem cmdlet to view the updated contents of the second folder, we can see that all files from the first folder have been copied into it.

The existing file named team_A.txt has also been overwritten by the new team_A.txt file from the first folder.

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 Copy and Rename a File
PowerShell: How to Use Copy-Item and Create New Directory
PowerShell: How to Use Copy-Item and Exclude Folders

Leave a Reply