0

I am new to Powershell, and am testing out the functionality of Copy-Item according to this documentation. I am trying to copy files and folders from a subfolder into it's containing folder, but it is not working. What command do I need to use?

Here is my sample file structure

C:\
    Example
    |
    |__Top
        |
        |__Middle
            |
            |__Bottom

I tried to copy all the files and subfolders from Middle into Top using

Copy-Item "C:\Example\Top\Middle" -Destination "C:\Example\Top" -Recurse

but received this error

Copy-Item : An item with the specified name C:\Example\Top\Middle already exists.
At line:1 char:1
+ Copy-Item "C:\Example\Top\Middle" -Destination "C:\Example\Top" -Recu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceExists: (C:\Example\Top\Middle:String) 
[Copy-Item], IO
   Exception
    + FullyQualifiedErrorId : 
DirectoryExist,Microsoft.PowerShell.Commands.CopyItemCommand

Copy-Item : Cannot overwrite the item C:\Example\Top\Middle\InTheMiddle.txt 
with itself.
At line:1 char:1
+ Copy-Item "C:\Example\Top\Middle" -Destination "C:\Example\Top" -Recu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: 
(C:\Example\Top\Middle\InTheMiddle.txt:String) [Co
   py-Item], IOException
    + FullyQualifiedErrorId : 
CopyError,Microsoft.PowerShell.Commands.CopyItemCommand

Copy-Item : An item with the specified name C:\Example\Top\Middle\Bottom already         
exists.
At line:1 char:1
+ Copy-Item "C:\Example\Top\Middle" -Destination "C:\Example\Top" -Recu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceExists: 
(C:\Example\Top\Middle\Bottom:String) [Copy-It
   em], IOException
    + FullyQualifiedErrorId : 
DirectoryExist,Microsoft.PowerShell.Commands.CopyItemCommand

Copy-Item : Cannot overwrite the item 
C:\Example\Top\Middle\Bottom\InTheBottom.txt with
itself.
At line:1 char:1
+ Copy-Item "C:\Example\Top\Middle" -Destination "C:\Example\Top" -Recu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: 
(C:\Example\Top\...InTheBottom.txt:String) [Copy-I
   tem], IOException
    + FullyQualifiedErrorId : 
CopyError,Microsoft.PowerShell.Commands.CopyItemCommand

Powershell Version 5.1.17134.112 Running as Administrator

5
  • The error literally explains the problem to you. The Middle folder already exists in the Top folder. Commented Jul 9, 2018 at 19:53
  • I am trying to copy the contents of the middle folder into the top folder, not create a new folder or override the old one. How do I do that? Commented Jul 9, 2018 at 19:55
  • 2
    Then you want C:\Example\Top\Middle\*.* Commented Jul 9, 2018 at 19:56
  • Okay, that Copied the files inside Middle but not the folders. How do I copy all subfolders? Are there any other similar codes and where can I look them up? Commented Jul 9, 2018 at 19:59
  • 1
    @Aposhian I am trying to copy the contents of the middle folder No. Your code literally says "copy the folder 'Middle' to its parent folder 'Top'". Where it already exists, hence the error. Commented Jul 9, 2018 at 20:01

2 Answers 2

2

You trying to copy child folder to its parent folder. To copy content of "Middle" folder you need to use this command:

Copy-Item "C:\Example\Top\Middle\*" -Destination "C:\Example\Top" -Recurse
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. Is there somewhere where I can read about the difference between * and *.* or any additional codes?
1

I believe this will accomplish what you are looking for as the simplest example. It would need tweaked depending on your scenario.

-Force would need applied to overwrite.

$source = "C:\Example\Top\Middle\*"

$destination = "C:\Example\Top\"

Copy-Item -Path $source -Destination $destination -Include *.*

2 Comments

What does -Include *.* do? Is that for hidden files?
It allows the flexibility to be able to narrowly tailor the files you would like to copy by the name disregarding the extension and vice versa

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.