Just use the robocopy command. It is designed for this kind of thing.
robocopy $source $destination /E
It comes with Windows 7, therefore it is an internal command, therefore it is still โPowershellโ, imho, but if you want to use the copy command, you are very close, but your current implementation captures source and puts it inside target (i.e. E. The result will be C:\target\source\files , not C:\target\files ). If you want the inside of target look like inside source , you need to do:
cp C:\source\* C:\target -r -fo
Note * , this captures the contents in the source folder. If you want to clear the target first, just do rm -r -fo C:\target\* before the copy.
Powershell does not handle long paths, so you will need to use robocopy if you have a long file name or deep folders.
source share