Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the Copy-Item cmdlet correctly to copy piped files

I am building a small script which should copy all .zip files to a special folder called F:\tempzip.

I tried it with Copy-Item cmdlet, but I didn't manage to do it. The script should copy all files from this folder (recursively) which are ".zip".

This is the part of the script I am talking about:

get-childitem F:\Work\xxx\xxx\xxx -recurse `
   | where {$_.extension -eq ".zip"}       `
   | copy-item F:\tempzip

What do I have to add?

like image 828
RayofCommand Avatar asked Jul 31 '13 14:07

RayofCommand


People also ask

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.

How do I copy multiple files from one folder to another in PowerShell?

Use Copy-Item Command to Copy Files Recursively in PowerShell. We usually run into situations where we have many subfolders in the parent folder, which files we'd like to copy over. Using the -Recurse parameter on Copy-Item will look in each subfolder and copy all files and folders in each recursively.

Which of these cmdlets are related to copy operation?

Copy-Item cmdlet is used to copy a file by passing the path of the file to be copied and destination path where the file is to be copied.


1 Answers

For whatever reason, the Copy-Item recursion didn't accomplish what I wanted, as mentioned here, and how it is documented to work. If you have a bunch of *.zip or *.jpg files in arbitrarily deep subfolder hierarchies, and you want to copy them to a single place (one flat folder, elsewhere), I had better luck with a piped command involving Get-ChildItem. Say you are currently in the folder containing the root of your search:

Get-ChildItem -Recurse -Include *.zip | Copy-Item -Destination C:\Someplace\Else

That command will copy all the files and not duplicate the folder hierarchies.

like image 59
Greg Avatar answered Sep 22 '22 08:09

Greg