48

a, b, and c are files in a folder ~\Source\x\, and b, d, and f are files in a folder ~\Destination\x\. I want files a and c to be copied to ~\Destination\x\, and for ~\Destination\x\b to be replaced with ~\Source\x\b.

~\Destination\x\ should have a, b, c, d, and f.

1
  • yeah i was able to copy from one folder to other, but was unable to replace the files Commented Mar 30, 2016 at 14:00

1 Answer 1

82

Copy-Item with Force looks like what you are looking for.

[string]$sourceDirectory  = "C:\Source\*"
[string]$destinationDirectory = "C:\Destination\"
Copy-item -Force -Recurse -Verbose $sourceDirectory -Destination $destinationDirectory

Copy-item never delete extra files or folders in destination, but with -force it will overwrite if file already exists

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Eric, its not overwriting, i have a "Me.txt" file(containg text 'overwritten') in a Sub-Folder(A1)in folder(Source) .also, a "Me.txt" file(containg text 'overwrite me') in a Sub-Folder(A1)in folder(Destination)..
@Deepak Try my code again. I fixed up a couple of mistakes I had before.
+1. I don't know if it will help anyone but if you're embedding a PS script in Octopus Deploy and using OD variables within this code (or any PS code with string operations), the OD variables has to be wrapped with the double quotes otherwise OD's PS doesn't compile it as a string i.e [string]$sourceDirectory = "#{MyODVariable}*"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.