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
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