How can I copy a directory, overwriting its contents if it exists using Powershell?

Solution 1:

So the problem is that this:

Copy-Item -Force -Recurse foo bar

only works if bar does not exist and this:

Copy-Item -Force -Recurse foo/* bar

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

New-Item -Force -Type Directory bar
Copy-Item -Force -Recurse foo/* bar

Solution 2:

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

copy-item .\source\* .\destination -force -recurse