Search code examples
powershellpowershell-3.0copy-paste

Multiple filter in Get-ChildItem


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?


Solution

  • 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.