Candy Chiu
Candy Chiu

Reputation: 6679

powershell Get-Item how to denote destination is a folder to be created?

I am trying to copy all files from the source directory to a destination directory which should be created if not existed by using Copy-Item.

First try: Copy-Item 'C:\sourcePath\reports' 'C:\destPath\reports' -Force -Recurse

All is well. However, if I run the command again, powershell instead of copying the content in C:\sourcePath\reports, creates an additional reports subfolder under C:\destPath\reports. I end up with C:\destPath\reports\reports.

Other tries:

Copy-Item 'C:\sourcePath\reports*' 'C:\destPath\reports' -Force -Recurse This one doesn't create the destination path C:\destPath\reports

Copy-Item 'C:\sourcePath\reports**' 'C:\destPath\reports' -Force -Recurse This one copies the content into a reports file, not folder.

Does anyone have any idea how to get this working? This

Upvotes: 1

Views: 414

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200203

Use robocopy. It was designed for this kind of task.

robocopy C:\sourcePath\reports C:\destPath\reports /s

Upvotes: 3

Related Questions