0

I am copying folders with

Copy-Item $source $target -Force -Recurse -Container -ErrorAction Stop

If $target folder already exists, sub folder will be created under $target with same name and files will be copied there. How do I prevent this from happening?

1 Answer 1

2

Check if $target already exists, and add a trailing \* to $source if it does, so that only the contents of the folder are copied, not the folder itself. Like this:

if (Test-Path -LiteralPath $target) {
  $source += '\*'
}
Copy-Item $source $target -Force -Recurse -Container -ErrorAction Stop
Sign up to request clarification or add additional context in comments.

Comments

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.