Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get-ChildItem and Copy-Item explanation

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?

like image 502
Dejan Dakić Avatar asked Feb 13 '14 09:02

Dejan Dakić


People also ask

What is the use of Get-ChildItem?

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.

What is difference between Get item and Get-ChildItem?

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.

How does copy item work?

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.

Does copy item overwrite?

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.


1 Answers

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 }
like image 82
websch01ar Avatar answered Oct 07 '22 17:10

websch01ar