0

I am new to powershell and still learning. I copy files from local directory to remote server directory. It works fine but looking for an option to copy only few files say 5 from a shared path to remote(the source directory has thousands of file and I dont need them all for my test)

Copy-Item -Path "C:\source\Documents\" -Destination "C:\Program Files\target" -ToSession $session

How can I limit the number of files to be copied? Thanks

4
  • What does this have to do with c#? Which 5 files? Do you have a rule, or is it the first 5 that the file system hands you? Remember, in Powershell, the pipeline is important. So you find a command that lists the files, pipe the output to a command that picks the 5 you want, and then pipe the output to the copy command Commented Mar 6, 2021 at 20:50
  • Does this answer your question? Windows script to Copy X number of files from one folder to another Commented Mar 6, 2021 at 21:09
  • [1] use Get-ChildItem to get a list of files. [2] sort them by whatever criteria suits your needs. [3] use Select-Object and either the -First or the -Last parameter to grab however many files you want. [grin] Commented Mar 6, 2021 at 21:18
  • Thanks for answering. The files in source directory are all of same filetype. I randomly would like to copy the top 5 files. Also I am writing a program in c# where I use powershell to copy the files to remote server so I tagged both. Commented Mar 6, 2021 at 22:23

2 Answers 2

1

You have two options here. One is to use -Filter parameter in Copy-Item cmdlet if you can do your filtering with the FileSystem filter language. Use also parameter -Recurse if your files can be placed in subdirectories (those subdirectories are copied with their file trees intact). But you cannot use it if you want say first 5 matches.

Example:

Copy-Item -Path "C:\source\Documents\" -Destination "C:\Program Files\target" -ToSession $session -Recurse -Filter "File*.txt"

In the case you need select only first 5 files I would find (using Get-ChildItem cmdlet) them with your criteria first and copy it after that. You can do something like that (files will be copied to destination without directories)

  1. Using the FileSystem filter language. This code will find your files (only files) in path C:\source\Documents\ filter it by name with wildcard (File*.txt), select first 5 and copy it to destination on remote computer from $session variable

     dir "C:\source\Documents\" -Filter "File*.txt" -Recurse -File | select -First 5 | Copy-Item -Destination "C:\Program Files\target" -ToSession $session
    
  2. Using regex pattern for filtering your files. This code will find your files (only files) in path C:\source\Documents\, filter it by regex pattern (use -match if you want to have it case sensitive) -> FileXXX.txt, where XXX are numbers 0-9, select first 5 files and copy it to destination on remote computer from $session variable

      dir "C:\source\Documents\" -Recurse -File | ? {$_.Name -imatch "^File\d{3}.txt$"} | select -First 5 | Copy-Item -Destination "C:\Program Files\target" -ToSession $session
    

*If you want to copy hidden files, use -Force parameter.

Sign up to request clarification or add additional context in comments.

Comments

0

Try Use this Copy-Item (gci "C:\source\Documents" | select -first 5).Fullname -Destination "C:\Program Files\target" -ToSession $session

1 Comment

Thanks alot. This worked :) I know I only asked for this but how do I add this as Parameter to PSCommand in c# code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.