Get-Child and Copy-Item

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of DMish DMish 1 year, 5 months ago.

  • Author
    Posts
  • #28072
    Profile photo of DMish
    DMish
    Participant

    So I wrote the following code to get txt files, pipe it, and then copy them to a destination.

    Get-ChildItem -Path "C:\Source" -Recurse |
    Where {$_.FullName -Like '*.txt*'} |
    Copy-Item $_.FullName -Destination "C:\Destination"

    The problem is that the script would not work this way, so I kept looking around and found an example of a script similar to mine but they use % and {} brackets at the copy-item command. I'm trying to understand what the difference means.

    Get-ChildItem -Path "C:\Source" -Recurse |
    Where {$_.FullName -Like '*.txt*'} |
    % {Copy-Item $_.FullName -Destination "C:\Destination"}
  • #28076
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    If you are just searching for text (.txt) files, you should filter with Get-ChildItem:

    Get-ChildItem -Path C:\Test\* -Recurse -Include *.txt -File | Copy-Item -Destination C:\Temp -WhatIf

    Copy-Item accepts pipeline input, so there is no need to do a foreach loop either.

  • #28082
    Profile photo of DMish
    DMish
    Participant

    Yea that works. Thanks! One question though, I'm going to copy some really big files, does anyone know if PowerShell have a Multi-Threading feature just like Robocopy does?

You must be logged in to reply to this topic.