6

I want to apply multiple filter in PowerShell script as of now I am able to add only single filter like below

Get-Childitem "D:\SourceFolder" -recurse -filter "*kps.jpg" 
    | Copy-Item -Destination "D:\DestFolder"

From the above query I able to to copy only one file kps.jpg from SourceFolder to DestFolder, how can I pass multiple names to copy more than one file?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
R15
  • 13,982
  • 14
  • 97
  • 173
  • Does this answer your question? [How to properly -filter multiple strings in a PowerShell copy script](https://stackoverflow.com/questions/18616581/how-to-properly-filter-multiple-strings-in-a-powershell-copy-script) – halt9k Mar 01 '23 at 02:40

1 Answers1

14

To filter on more than one condition, use the Include parameter instead of the -Filter.

Get-Childitem "D:\SourceFolder" -Recurse -Include "*kps.jpg", "*kps.png", "*kps.bmp" | 
Copy-Item -Destination "D:\DestFolder"


Note: The Include only works if you also specify -Recurse or have the path end in \* like in Get-Childitem "D:\SourceFolder\*"


Note2: As Lee_Daily commented, -Filter would work faster that -Include (or -Exclude) parameters. Filters are more efficient than other parameters, because the provider applies them when the cmdlet gets the objects. Otherwise, PowerShell filters the objects after they are retrieved. See the docs

The downside of -Filter is that you can only supply one single filename filter whereas -Include allows for an array of wildcard filters.

Theo
  • 57,719
  • 8
  • 24
  • 41
  • i vaguely recall reading that the `-Include` & `-Exclude` parameters do their filtering in the cmdlet while the `-Filter` parameter has the provider do the filtering. is that correct - and if so, is it worth mentioning in your notes that it will be slower because of that? – Lee_Dailey Mar 15 '19 at 15:37
  • @Lee_Dailey I have added this info as a second note. Thanks for your input. – Theo Mar 15 '19 at 15:51
  • kool! you are most welcome ... and thanks for the link! [*grin*] – Lee_Dailey Mar 15 '19 at 15:54