[Powershell]: 2nd Execution of Copy-Item creates subfolder (5.1.17763.1007)

Viewed 70

After searching for a wile I need to post my question here: I want to do a simple task:

copy-item -path "C:\Folder Copied" -destination "C:\Folder Copied_New" -recurse

Assuming the dir "Folder Copied_New" doenst exist in "C:", PS will create a folder, and copy the folder (and its content) "C:\Folder Copied" to "Folder Copied_New"

HOWEVER, if you execute the command a second time, the following happens:

Powershell created: "C:\Folder Copied_New\Folder Copied" (content "Test.txt" was also copied to this newly created folder...

The 3rd time you execute the command, it ll say that the folder already exists...

So my question: After I run the command a 2nd time, PS should throw an error, that "Folder Copied_New". How do I do that?

I tried copying and renaming the new folder, using and NOT using "" in the paths, but nothing worked. I do think of using -Testpath, but I thought I ask the community for a simplier (BestPractice) approache.

Thanks in advance for reading and advising!

Well explained in another question but same issue

2 Answers

Not an answer but to long to write in comments. Can you run following script and show us the output?

$root = "$($env:TEMP)\test"
$source = "$($root)\Logfiles"
$destination = "$($root)\Drawings\Logs"

# Verify folders don't exist yet
if (Test-Path $source) {Throw}
if (Test-Path $destination) {Throw}

# Set up
New-Item $source, $destination -ItemType Directory -Force | Out-Null # Create folders
New-Item "$($source)\testfile" -ItemType File -Force | Out-Null # Create a testfile

# Show files/folders before copy
Write-Output "Before copy"
Get-ChildItem $root -Recurse | select fullname

# Copy first time
Copy-Item -path $source -destination $destination -Recurse -ErrorAction Continue
Write-Output "After first time copy"
Get-ChildItem $root -Recurse | select fullname

# Copy second time
Copy-Item -path $source -destination $destination -Recurse -ErrorAction Continue
Write-Output "After second time copy"
Get-ChildItem $root -Recurse | select fullname

# Copy third time
Copy-Item -path $source -destination $destination -Recurse -ErrorAction Continue
Write-Output "After third time copy"
Get-ChildItem $root -Recurse | select fullname

# Clean up
Remove-Item $source, $destination -Force -Recurse | Out-Null

On my computer, I don't observe any abnormal behavior. The source folder get's copied to destination after the first time. The second run does not alter anything, neither does the third run.

enter image description here

That behavior is the default:

Let's say you want to copy and rename an item, so you are doing this:

Copy-Item -path source.txt -Destination destination.txt

You expect a file named destination.txt with the content of source.txt.

If you want to copy a file but without renaming it you do this:

Copy-Item -path source.txt -Destination C:\test

You expect the file source.txt to apear in C:\test. If C:\test didn't exist it will be created.

Now let's try to copy a folder while the destination does not exist.

Copy-Item -path C:\test -Destination D:\toast 

The destination didn't exist so you created it by copying the source folder to the destination. The content of the destination will be the same as the source. The folder D:\toast is the same as C:\test but it got renamed in the process.
However if you provide a destination path where the source object (folder) is going to be located, it will be located in there.

 Copy-Item -path C:\test -Destination D:\toast 

D:\toast did exist from our previouse action so a copy of C:\test will be created in there: D:\toast\test

Related