Why does
gci $from -Recurse | copy-item -Destination $to -Recurse -Force -Container
not behave in the same way as
copy-item $from $to -Recurse -Force
?
I think it should be the same, but somehow it's not. Why?
The Get-ChildItem cmdlet gets the items in one or more specified locations. If the item is a container, it gets the items inside the container, known as child items. You can use the Recurse parameter to get items in all child containers and use the Depth parameter to limit the number of levels to recurse.
Differences Between Get-Item and Get-ChildItemGet-Item returns information on just the object specified, whereas Get-ChildItem lists all the objects in the container. Take for example the C:\ drive.
The Copy-Item cmdlet copies the entire contents from the remote C:\MyRemoteData\scripts folder to the local D:\MyLocalData folder using the session information stored in the $Session variable. If the scripts folder has files in subfolders, those subfolders are copied with their file trees intact.
Copy-Item simply overwrites the files and folders on the destination path and the copies newer files. For example, To copy files from the source folder C:\Test1 to the destination folder C:\Test2 below command is used and it simply overwrites the file without asking.
You are not looping over each item in the collection of files/folders, but passing the last value to the pipe. You need to use Foreach-item or % to the Copy-Item command. From there, you also do not need the second -recurse switch as you already have every item in the GCI.
try this:
gci $from -Recurse | % {copy-item -Path $_ -Destination $to -Force -Container }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With