Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a directory Source with some files in it, which I would like to copy to a folder Destination. Destination may exist, and it may have files in it already. Any files with the same name as those in Source should be overwritten.

If I run this in Powershell:

Copy-Item Source Destination -Force -Recurse
Copy-Item Source Destination -Force -Recurse
Copy-Item Source Destination -Force -Recurse

Then the first line creates the folder .\Destination and copies .\Source into it, which is what I'd like to repeat for the next time. However, the second line copies .\Source into the new .\Destination folder (creating .\Destination\Source) instead, then the third line overwrites .\Destination\Source again.

How can I make it act like in the first case all the time? That is, overwrite .\Destination instead of copying into it?

share|improve this question
Can you clarify? you want to copy the contents of "source" in to "destination"? and not copy the folder "source" into "destination"? – BroScience Jan 31 '13 at 15:25
Yes, that's correct. The "destination" folder may not exist, so I want it to be created if it doesn't exit. – Douglas Jan 31 '13 at 17:22
up vote 4 down vote accepted

So the problem is that

cp -r -fo foo bar

only works if bar does not exist and

cp -r -fo foo/* bar

only works if bar exists. So to work around, you need to make sure bar exists before doing anything

md -f bar
cp -r -fo foo/* bar
share|improve this answer

Steven Penny's answer doesn't delete original content of the target directory, just appending to it. I needed to completely replace the target folder with content of the source and created 2 functions:

function CopyToEmptyFolder($source, $target )
    DeleteIfExistsAndCreateEmptyFolder($target )
    Copy-Item $source\* $target -recurse -force
function DeleteIfExistsAndCreateEmptyFolder($dir )
    if ( Test-Path $dir ) {
           Get-ChildItem -Path  $dir -Force -Recurse | Remove-Item -force -recurse
           Remove-Item $dir -Force

    New-Item -ItemType Directory -Force -Path $dir
share|improve this answer

Let's say that you Have the following structure of Directories

  • root

    • folder_a
      • a.txt
      • b.txt
      • c.txt
    • folder_b
      • a.txt
      • b.txt

    In The root folder you can achieve the results you want by the following sequence of commands:

    $files = gci ./folder_b -name
    cp ./folder_a/*.txt -Exclude $files ./folder_b

Only c.txt will be copied

share|improve this answer

If you only want to copy contents of the "source" folder use

copy-item .\source\* .\destination -force -recurse
share|improve this answer
This only works if destination already exists. – Steven Penny Apr 16 '14 at 23:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .