0

I tried with -force but I still get error message

Copy-Item : An item with the specified name C:\folder2 already exists.

Copy-Item 'c:\folder1' -Destination 'c:\folder2' -force
2
  • Can you paste the error? $Error[0] Commented Nov 28, 2021 at 22:44
  • It works for me. But this won't copy any files in folder1. And on second run it will create folder1 inside folder2. Commented Nov 29, 2021 at 16:26

1 Answer 1

2

You can create the destination folder first and then copy everything inside the source folder by adding \* to the path:

# first create the destination folder if it does not already exist
$null = New-Item -Path 'c:\folder2' -ItemType Directory -Force
# then copy all from the source folder to the destination
Copy-Item -Path 'c:\folder1\*' -Destination 'c:\folder2' -Recurse -Force

By using switch -Force on the New-Item command, the cmdlet returns either the object of a newly created folder or the object of an existing folder.
In both cases, we do not need that output, so we'll ignore it using $null =

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.