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

When piping items to copy-item you need to tell it that "F:\tempzip" is the destination path.

| Copy-Item -Destination F:\tempzip

You can also cutout piping to the where operator by using Get-ChildItem's parameter -filter.

Get-Childitem "C:\imscript" -recurse -filter "*.zip" | Copy-Item -Destination "F:\tempzip"

Edit: Removal of unnecessary foreach loop and updated explanation.


It's a lot simpler than that. Copy-Item has its own -Recurse switch. All you have to do is:

Copy-Item F:\Work\xxx\xxx\xxx\*.zip F:\tempzip -Recurse

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.