-1

I want to fetch a specific file recursively which may exists in sub-folders and copy it to destination folder.

I tried executing below command:

Get-ChildItem "D:\Source Directory" | Copy-Item -Destination "D:\Target Directory" -Recurse -filter file.doc

The command copied more files then the one fetched.

Any better solution?

svevis
  • 21
  • 2
  • 1
    Your -Flter and -Recurse both need to be on the Get-ChildItem command not the Copy-Item command. You'll also run into a problem trying to copy multiple files of the same name to a single directory. Some rethinking is in order. – RetiredGeek Mar 25 '21 at 13:50
  • Thanks for the input. Get-ChildItem -Path "D:\Source Directory" -Recurse -Filter "file.doc" -File | Copy-Item -Destination "D:\Target Directory" is working – svevis Mar 25 '21 at 14:53

1 Answers1

0

Get-ChildItem -Path "D:\Source Directory" -Recurse -Filter "file.doc" -File | Copy-Item -Destination "D:\Target Directory"

svevis
  • 21
  • 2