Description of Get-ChildItem and Copy-Item

Why

gci $from -Recurse | copy-item -Destination  $to -Recurse -Force -Container

doesn't behave the same way

copy-item $from $to -Recurse -Force

?

I think it should be the same thing, but for some reason it is not. Why?

+3
source share
5 answers

You do not iterate over each item in the file / folder collection, but pass the last value to the channel. You must use the Foreach-item or% for the Copy-Item command. From there, you also don't need a second -recurse switch, since you already have every element in GCI.

try the following:

gci $from -Recurse | % {copy-item -Path $_ -Destination  $to -Force -Container }
+8
source

Here's what worked for me

Get-ChildItem -Path .\ -Recurse -Include *.txt | %{Join-Path -Path $_.Directory -ChildPath $_.Name } | Copy-Item -Destination $to
+1
source

:

Get-ChildItem 'c:\source\' -Recurse | % {Copy-Item -Path $_.FullName -Destination 'd:\Dest\' -Force -Container }
0

. :

gci -Path $from -Recurse | copy-item -Destination  $to -Force -Container
-1

, :

gci -Path $from -Recurse | % { $_ | copy-item -Destination  $to -Force -Container}

Just do foreach and drag each item again.

-1
source

All Articles