I can never seem to get this right.
I have an existing folder c:\MyApps\Websites\MySite that already has an existing website running in it. I’ve downloaded the latest bits which are located under c:\temp\MySite\artifacts.
when I try to run this
$source "c:\temp\MySite"
$destination "c:\MyApps\Websites\MySite"
copy-item $source $destination -recurse -force
if c:\MyApps\Websites\MySite already exists then it tries to put it in c:\MyApps\Websites\MySite\artifacts, but if it doesn’t already exist it copies it properly. Not sure what’s going on here. Any suggestions?
Just use the
robocopycommand. It’s designed for this kind of thing.robocopy $source $destination /EIt’s shipped with Windows 7, so it’s an internal command, so it’s still a ‘Powershell’ way, imho, but if you’re wanting to use the copy command, then you are very close, but your current implementation grabs the
sourcefolder and puts it insidetarget(ie the result would beC:\target\source\files, notC:\target\files). When you want to make the inside oftargetlook like the inside ofsource, you need to do:cp C:\source\* C:\target -r -foNotice the
*, this grabs the contents of thesourcefolder. If you wanttargetto be cleared first, just dorm -r -fo C:\target\*before the copy.Powershell doesn’t handle long paths, so you will need to use robocopy anyway if you have a long filename or deep folders.